【问题标题】:How to plot a Stacked and grouped bar chart in ggplot?如何在ggplot中绘制堆叠和分组条形图?
【发布时间】:2018-03-17 18:15:26
【问题描述】:

我有一个如下的数据框:

id    month     type    count
___  _______   ______   ______
1      1          1       10
1      1          2       09
1      1          3       26
1      2          1       60
1      2          2       90
2      2          3       80
2      1          1       10
2      1          2       09
2      1          3       26
2      2          1       60
2      2          2       90
2      2          3       80
3      1          1       10
3      1          2       09
3      1          3       26
3      2          1       60
3      2          2       90
3      2          3       80

我认为可视化的最佳方式是堆叠组栏,如下所示:

所以我尝试了

ggplot(df,aes(x=id,y=count,fill=month))+geom_bar(stat="identity",position=position_dodge())+geom_text(aes(label=count),size=3)

这给出了一个与我的预期有点不同的情节。感谢任何帮助。

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    假设您要将id绘制为x轴,并排表示月份,并堆叠不同类型,您可以按月拆分数据帧,并为每个月添加一个条形层,将x移动以第二个月的条数为单位,以便将它们分开:

    barwidth = 0.35
    
    month_one <- filter(df, month == 1) %>% 
        group_by(id) %>% arrange(-type) %>% 
        mutate(pos = cumsum(count) - count / 2)   # calculate the position of the label
    
    month_two <- filter(df, month == 2) %>% 
        group_by(id) %>% arrange(-type) %>% 
        mutate(pos = cumsum(count) - count / 2)
    
    ggplot() + 
        geom_bar(data = month_one, 
                 mapping = aes(x = id, y = count, fill = as.factor(type)), 
                 stat="identity", 
                 position='stack', 
                 width = barwidth) + 
        geom_text(data = month_one, 
                  aes(x = id, y = pos, label = count )) + 
        geom_bar(data = filter(df, month==2), 
                 mapping = aes(x = id + barwidth + 0.01, y = count, fill = as.factor(type)), 
                 stat="identity", 
                 position='stack' , 
                 width = barwidth) + 
        geom_text(data = month_two, 
                  aes(x = id + barwidth + 0.01, y = pos, label = count )) + 
        labs(fill  = "type")
    

    给予:


    dput(df)
    structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), month = c(1L, 1L, 1L, 2L, 2L, 
    2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), type = c(1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L), count = c(10L, 9L, 26L, 60L, 90L, 80L, 10L, 9L, 26L, 60L, 
    90L, 80L, 10L, 9L, 26L, 60L, 90L, 80L)), .Names = c("id", "month", 
    "type", "count"), class = "data.frame", row.names = c(NA, -18L
    ))
    

    【讨论】:

    • 非常感谢。所以我们必须分开月份,然后加入图表。这样可以吗
    • 您混合了stack 条和dodge 条。是否有办法自动绘制它对我来说并不明显。
    【解决方案2】:

    这个问题可以用facet_grid解决得更干净:

    library(tidyverse)
    read_tsv("tmp.tsv", col_types = "ccci") %>%  
    ggplot(aes(x=month, y=count, fill=type)) + geom_col() + facet_grid(.~id)
    

    请注意,您必须在 col_types 参数中将前三列指定为“字符”,否则它看起来不会那么好。用有意义的东西替换数字代码会更好(例如,将月份变成有序因子“一月”、“二月”而不是 1、2;类型和 id 类似的东西)。

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2011-10-14
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多