跟进:如何在 R 中制作旭日图?
Follow up of : How to make a sunburst plot in R?
我是 R 的新手,我会直接在评论中问这个问题,但我还没有名气 :D
基本上,我想做一个类似 dmp 的朝阳图,在这个线程中建议:How to make a sunburst plot in R or Python?
但是,我的数据框看起来更像这样:
df <- data.frame(
'level1'=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'),
'level2'=c('AA', 'BB', 'CC', 'AA', 'BB', 'CC', 'AA', 'BB'),
'value'=c(12.5, 12.5, 75, 50, 25, 25, 36, 64))
所以当我绘制朝阳图时如下:
ggplot(df, aes(y=value)) +
geom_bar(aes(fill=level1, x=0), width=.5, stat='identity') +
geom_bar(aes(fill=level2, x=.25), width=.25, stat='identity') +
coord_polar(theta='y')
ggplot 将 level2 组合在一起(因此将所有 AA 加在一起,然后将所有 BB 和所有 CC 相加)而不是将每个都留在它们的 level1 中。我该如何预防?
非常感谢您,
纳斯
您可以尝试将行 ID 列添加到数据框中并将其明确用作分组变量。这可以防止 ggplot()
按 fill
美学对条形进行分组:
library(dplyr)
ggplot(df %>% mutate(id = seq(1, n())),
aes(y = value, group = id)) +
geom_col(aes(fill = level1, x = 0), width = .5) +
geom_col(aes(fill = level2, x = .25), width = .25) +
coord_polar(theta = 'y')
我是 R 的新手,我会直接在评论中问这个问题,但我还没有名气 :D
基本上,我想做一个类似 dmp 的朝阳图,在这个线程中建议:How to make a sunburst plot in R or Python?
但是,我的数据框看起来更像这样:
df <- data.frame(
'level1'=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'),
'level2'=c('AA', 'BB', 'CC', 'AA', 'BB', 'CC', 'AA', 'BB'),
'value'=c(12.5, 12.5, 75, 50, 25, 25, 36, 64))
所以当我绘制朝阳图时如下:
ggplot(df, aes(y=value)) +
geom_bar(aes(fill=level1, x=0), width=.5, stat='identity') +
geom_bar(aes(fill=level2, x=.25), width=.25, stat='identity') +
coord_polar(theta='y')
ggplot 将 level2 组合在一起(因此将所有 AA 加在一起,然后将所有 BB 和所有 CC 相加)而不是将每个都留在它们的 level1 中。我该如何预防?
非常感谢您,
纳斯
您可以尝试将行 ID 列添加到数据框中并将其明确用作分组变量。这可以防止 ggplot()
按 fill
美学对条形进行分组:
library(dplyr)
ggplot(df %>% mutate(id = seq(1, n())),
aes(y = value, group = id)) +
geom_col(aes(fill = level1, x = 0), width = .5) +
geom_col(aes(fill = level2, x = .25), width = .25) +
coord_polar(theta = 'y')