R语言 树蜂子群内群的一致布局

wlwcrazw  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(106)

我有一个三级数据集,其中中间类别的值与最大类别不同,但其中最小类别的值在每个中间类别中是一致的。
我使用的是treemapifyr,我希望布局始终保持最小的类别之一,无论是在左侧还是在中间类别的顶部。目前,这些中间类别有时有横向分裂,有时有纵向分裂。
这份报告说明了这个问题:

library(tidyverse)
library(treemapifyr)

example_data <- 
  data.frame(Number = c(8.989663702,14.93294526,
                          5.727538109,11.47738226,5.962484342,5.528905863,
                          13.89280722,19.05866541,10.17157544,11.59719022,
                          17.93635842,2.034230053,13.38663681,8.729627328,
                          11.16785702,3.345153661,4.826922784,11.24138854),
                  Big = c("First","First","First",
                          "First","First","First","Second","Second",
                          "Second","Second","Third","Third","Third","Third",
                          "Third","Third","Third","Third"),
         Intermediate = c("A","A","B","B","C",
                          "C","D","D","E","E","F","F","G","G","H","H",
                          "I","I"),
                Small = c("Alpha","Beta","Alpha",
                          "Beta","Alpha","Beta","Alpha","Beta","Alpha",
                          "Beta","Alpha","Beta","Alpha","Beta","Alpha",
                          "Beta","Alpha","Beta")
   )
example_data %>% 
  group_by(Big, Intermediate) %>% 
  mutate(Fraction = 
           Number/sum(Number)) %>% 
  ggplot()+ 
  aes(area = Number,
      subgroup2 = Intermediate,
      subgroup = Big,
      group = Small) + 
  geom_treemap(aes(fill = Small)) +
  geom_treemap_subgroup2_border(colour = "gray50",
                                size = 2) +
  geom_treemap_subgroup_border(colour = "white") +
  geom_treemap_subgroup2_text(place = "centre") +
  geom_treemap_text(aes(label = round(Fraction, 2)))

如果可以对这些进行配置,使中间类别(在本例中为Intermediate)始终以相同的顺序并排或从上到下拆分,那就太好了。提前感谢!

yc0p9oo0

yc0p9oo01#

您可以应用fixedlayout而不是默认的squarified。所有元素应具有相同的布局,以确保边框和文本与图的其余部分对齐。我还选择将开始位置从bottomleft中的默认值更改为topleft。如果这对您的真实的数据不好,您可以尝试从任何角落开始。

library(tidyverse)
library(treemapify)

layout = 'fixed'
start = 'topleft'

example_data %>% 
  group_by(Big, Intermediate) %>% 
  mutate(Fraction = 
           Number/sum(Number)) %>% 
  ggplot()+ 
  aes(area = Number,
      subgroup2 = Intermediate,
      subgroup = Big,
      group = Small) + 
  geom_treemap(aes(fill = Small),
               layout = layout,
               start = start) +
  geom_treemap_subgroup2_border(colour = "gray50",
                                size = 2,
                                layout = layout,
                                start = start) +
  geom_treemap_subgroup_border(colour = "white",
                               layout = layout,
                               start = start) +
  geom_treemap_subgroup2_text(place = "centre",
                              layout = layout,
                              start = start) +
  geom_treemap_text(aes(label = round(Fraction, 2)),
                    layout = layout,
                    start = start)

创建于2023-09-29带有reprex v2.0.2

相关问题