【问题标题】:Follow up of : How to make a sunburst plot in R?跟进:如何在R中制作旭日形图?
【发布时间】:2019-08-11 06:54:58
【问题描述】:

我是 R 新手,我会直接在 cmets 中问这个问题,但我还没有声誉: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 中。我该如何防止呢?

非常感谢您,

纳特

【问题讨论】:

    标签: r ggplot2 data-visualization sunburst-diagram


    【解决方案1】:

    您可以尝试在数据框中添加行 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')
    

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多