多个组的ggplot条形图+折线图

Posted

技术标签:

【中文标题】多个组的ggplot条形图+折线图【英文标题】:ggplot bar plot by multiple groups + line graph 【发布时间】:2022-01-14 09:57:15 【问题描述】:

我有这个数据集:

structure(list(team = c("bgb", "bgb", "bgb", "bgb", "bgb", "bgb", 
"bgb", "bgb", "bgb", "bgb", "bgb", "bgb", "bgb", "bgb", "bgb", 
"bgb", "bgb", "bgb", "bgb", "bgb", "bgb", "bgr", "bgr", "bgr", 
"bgr", "bgr", "bgr", "bgr", "bgr", "bgr", "bgr", "bgr", "bgr", 
"bgr", "bgr", "bgr", "bgr", "bgr", "bgr", "bgr", "bgr", "bgr", 
"chj", "chj", "chj", "chj", "chj", "chj", "chj", "chj", "chj", 
"chj", "chj", "chj", "chj", "chj", "chj", "chj", "chj", "chj", 
"chj", "chj", "chn", "chn", "chn", "chn", "chn", "chn", "chn", 
"chn", "chn", "chn", "chn", "chn", "chn", "chn", "chn", "chn", 
"chn", "chn", "chn", "chn", "chn", "lev", "lev", "lev", "lev", 
"lev", "lev", "lev", "lev", "lev", "lev", "lev", "lev", "lev", 
"lev", "lev", "lev", "lev", "lev", "mbj", "mbj", "mbj", "mbj", 
"mbj", "mbj", "mbj", "mbj", "mbj", "mbj", "mbj", "mbj", "mbj", 
"mbj", "mbj", "mbj", "mbj", "mbj", "mbj", "mbj", "mbj", "mbn", 
"mbn", "mbn", "mbn", "mbn", "mbn", "mbn", "mbn", "mbn", "mbn", 
"mbn", "mbn", "mbn", "mbn", "mbn", "mbn", "mbn", "mbn", "mbn", 
"mbn", "mbn", "mrb", "mrb", "mrb", "mrb", "mrb", "mrb", "mrb", 
"mrb", "mrb", "mrb", "mrb", "mrb", "mrb", "mrb", "mrb", "mrb", 
"mrb", "mrb", "mrb", "mrb", "mrb", "rwl", "rwl", "rwl", "rwl", 
"rwl", "rwl", "rwl", "rwl", "rwl", "rwl", "rwl", "rwl", "rwl", 
"rwl", "rwl", "rwl", "rwl", "rwl", "rwl", "rwl", "rwl"), tmp = c("P1", 
"P1", "P1", "P1", "P1", "P1", "P1", "P2", "P2", "P2", "P2", "P2", 
"P2", "P2", "P3", "P3", "P3", "P3", "P3", "P3", "P3", "P1", "P1", 
"P1", "P1", "P1", "P1", "P1", "P2", "P2", "P2", "P2", "P2", "P2", 
"P2", "P3", "P3", "P3", "P3", "P3", "P3", "P3", "P1", "P1", "P1", 
"P1", "P1", "P1", "P1", "P2", "P2", "P2", "P2", "P2", "P2", "P2", 
"P3", "P3", "P3", "P3", "P3", "P3", "P1", "P1", "P1", "P1", "P1", 
"P1", "P1", "P2", "P2", "P2", "P2", "P2", "P2", "P2", "P3", "P3", 
"P3", "P3", "P3", "P3", "P3", "P1", "P1", "P1", "P1", "P1", "P1", 
"P1", "P2", "P2", "P2", "P2", "P2", "P2", "P2", "P3", "P3", "P3", 
"P3", "P1", "P1", "P1", "P1", "P1", "P1", "P1", "P2", "P2", "P2", 
"P2", "P2", "P2", "P2", "P3", "P3", "P3", "P3", "P3", "P3", "P3", 
"P1", "P1", "P1", "P1", "P1", "P1", "P1", "P2", "P2", "P2", "P2", 
"P2", "P2", "P2", "P3", "P3", "P3", "P3", "P3", "P3", "P3", "P1", 
"P1", "P1", "P1", "P1", "P1", "P1", "P2", "P2", "P2", "P2", "P2", 
"P2", "P2", "P3", "P3", "P3", "P3", "P3", "P3", "P3", "P1", "P1", 
"P1", "P1", "P1", "P1", "P1", "P2", "P2", "P2", "P2", "P2", "P2", 
"P2", "P3", "P3", "P3", "P3", "P3", "P3", "P3"), day_s = structure(c(2L, 
4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 
3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 
6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 
2L, 4L, 5L, 3L, 1L, 6L, 7L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 
3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 
6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 
5L, 3L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 
6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 
2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 
5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 
1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L, 2L, 4L, 5L, 3L, 1L, 6L, 
7L, 2L, 4L, 5L, 3L, 1L, 6L, 7L), .Label = c("Mo", "Di", "Mi", 
"Do", "Fr", "Sa", "So"), class = c("ordered", "factor")), mpd = c(108, 
93, 92, 60, 98, 96, 30, 57, 58, 60, 47, 78, 65, 87, 67, 72, 76, 
27, 54, 63, 42, 96, 62, 73, 27, 17, 33, 45, 51, 69, 29, 29, 59, 
38, 17, 120, 59, 30, 30, 68, 30, 18, 68, 32, 71, 73, 81, 28, 
38, 90, 107, 60, 43, 38, 22, 5, 150, 120, 90, 120, 90, 113, 91, 
89, 69, 80, 114, 30, 56, 169, 186, 69, 95, 132, 75, 104, 60, 
189, 250, 139, 180, 58, 180, 117, 107, 50, 127, 162, 11, 130, 
58, 88, 82, 98, 75, 110, 158, 80, 18, 120, 120, 70, 89, 106, 
85, 103, 130, 50, 65, 84, 120, 84, 38, 100, 108, 30, 90, 50, 
63, 120, 80, 70, 90, 71, 28, 77, 98, 70, 60, 64, 62, 63, 71, 
34, 27, 51, 38, 104, 130, 90, 150, 105, 132, 66, 99, 23, 79, 
77, 51, 26, 71, 80, 78, 102, 38, 66, 42, 52, 119, 44, 41, 133, 
278, 51, 78, 55, 89, 71, 93, 56, 61, 79, 60, 150, 79, 52, 85, 
52, 118, 98, 62, 58, 60, 68, 87), rpd = c(6, 5, 5, 5, 6, 5, 5, 
5, 5, 7, 5, 6, 5, 6, 6, 6, 6, 5, 5, 4, 6, 7, 8, 7, 6, 6, 6, 6, 
9, 7, 6, 6, 7, 8, 5, 9, 6, 6, 7, 7, 6, 6, 7, 7, 6, 8, 7, 7, 7, 
9, 8, 9, 6, 8, 4, 3, 6, 6, 5, 2, 8, 8, 6, 6, 6, 5, 6, 6, 6, 7, 
6, 6, 6, 5, 8, 7, 6, 6, 6, 5, 4, 6, 9, 6, 7, 4, 8, 6, 5, 6, 6, 
4, 6, 8, 8, 6, 8, 8, 8, 10, 10, 8, 8, 6, 7, 6, 6, 4, 6, 6, 5, 
7, 9, 7, 7, 9, 8, 7, 7, 7, 6, 7, 7, 7, 5, 7, 6, 8, 5, 4, 6, 7, 
6, 6, 6, 7, 6, 8, 8, 8, 7, 8, 6, 7, 7, 6, 7, 7, 7, 6, 8, 7, 6, 
7, 5, 7, 7, 5, 7, 5, 5, 8, 11, 8, 7, 7, 6, 7, 6, 7, 6, 7, 7, 
7, 7, 8, 7, 7, 7, 8, 6, 10, 10, 7, 10)), row.names = c(NA, -185L
), groups = structure(list(team = c("bgb", "bgb", "bgb", "bgr", 
"bgr", "bgr", "chj", "chj", "chj", "chn", "chn", "chn", "lev", 
"lev", "lev", "mbj", "mbj", "mbj", "mbn", "mbn", "mbn", "mrb", 
"mrb", "mrb", "rwl", "rwl", "rwl"), tmp = c("P1", "P2", "P3", 
"P1", "P2", "P3", "P1", "P2", "P3", "P1", "P2", "P3", "P1", "P2", 
"P3", "P1", "P2", "P3", "P1", "P2", "P3", "P1", "P2", "P3", "P1", 
"P2", "P3"), .rows = structure(list(1:7, 8:14, 15:21, 22:28, 
    29:35, 36:42, 43:49, 50:56, 57:62, 63:69, 70:76, 77:83, 84:90, 
    91:97, 98:101, 102:108, 109:115, 116:122, 123:129, 130:136, 
    137:143, 144:150, 151:157, 158:164, 165:171, 172:178, 179:185), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -27L), .drop = TRUE), na.action = structure(c(`8` = 8L, 
`16` = 16L, `24` = 24L, `32` = 32L, `40` = 40L, `48` = 48L, `56` = 56L, 
`64` = 64L, `65` = 65L, `72` = 72L, `80` = 80L, `88` = 88L, `96` = 96L, 
`104` = 104L, `112` = 112L, `113` = 113L, `118` = 118L, `126` = 126L, 
`134` = 134L, `142` = 142L, `150` = 150L, `158` = 158L, `166` = 166L, 
`174` = 174L, `182` = 182L, `190` = 190L, `198` = 198L, `206` = 206L, 
`214` = 214L), class = "omit"), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

我想将变量 mpd 说明为条形,但通过“day_s”(周一至周日)和 tmp(阶段 1 至 3)进行区分。 如果它只是区分变量 day_s,这就是我得到的图:

ggplot(tab_tra)  + 
    geom_bar(aes(x=day_s, y=mpd), stat="identity")

但我希望在星期日之后它再次从星期一开始(P2 的星期一),然后是第三周。 x 轴基本上由三周(P1、P2 和 P3)组成。每周的酒吧应该有不同的颜色。例如,第一周的条形图是蓝色的,第二个是绿色的,第三个是红色的。 此外,我想添加一行,用单独的 y 轴说明变量“rpd”在这三周内的过程。

我还没有找到构建这个情节的正确方法。所以我希望有人可以帮助我。

在此先感谢,感谢任何形式的帮助。

干杯

更新:

我使用了@JKupzig 建议的方法。到目前为止它工作正常,但我无法添加折线图(见下文):

ggplot(tab_tra, aes(fill = tmp))  + 
    geom_bar(aes(x=day_s, y=mpd), stat="identity") +
    geom_line(aes(x=day_s, y=rpd*10))+
    scale_y_continuous(sec.axis = sec_axis(trans=~.*10, name= "rpd Axis"))+
    facet_grid(~ tmp)+
    theme_bw()

【问题讨论】:

当您使用 +geom_point(aes(x=day_s, y=rpd*10, group=tmp),stat="identity") 时,您会注意到 rpd 有多个值(由于不同的“团队”)。在条形图中,汇总了团队的值 - 您是否希望对折线图中的 rpd 值做同样的事情? 是的,我也想要汇总 rpd 值。 查看我的答案更新@psycho95 【参考方案1】:

您可以使用 facet_wrap 将星期排列在一起:

 ggplot(data, aes(fill=tmp))  + 
   geom_bar(aes(x=day_s, y=mpd, group=tmp) ,stat="identity") +
   facet_wrap(.~tmp) +
   theme_bw()

更新 要将 rpd 总结为线图,您可以执行以下操作:

    library(dplyr)

rpd_sum <- data %>% 
  group_by(tmp, day_s) %>%
  summarise(sum_rpd = sum(rpd)) %>%
  mutate(newClass = paste(tmp, day_s))

data$newClass <- paste(data$tmp, data$day_s)
dataNew <- merge(data, rpd_sum )  


ggplot(dataNew, aes(fill=tmp))  + 
  geom_bar(aes(x=day_s, y=mpd) ,stat="identity") +
  geom_line(aes(x=day_s, y=sum_rpd*10, group=tmp),stat="identity") +
  scale_y_continuous(sec.axis = sec_axis( trans=~./10, name="rpd Axis")) +
  facet_wrap(.~tmp) +
  theme_bw()

【讨论】:

谢谢@JKupzig,这正是我想要的。但是你也知道如何在这个图中添加折线图吗? ``` + geom_line()``` 看这里一​​个例子如何解决这个问题:***.com/questions/41764312/… 我在代码中添加了geom_line(aes(x=day_s, y=rpd), stat = "identity"),但没有任何反应 因为 rpd 的比例与 mpd 不同,所以您必须转换 rpd,例如geom_line(...aes(...y=rpd*10)... 并添加第二个 y 轴,例如 scale_y_continuous(sec.axis = sec_axis( trans=~.*10, name="rpd Axis")) 【参考方案2】:

简单地添加一个方面可能是最简单的解决方案。

ggplot(tab_tra)  + 
  geom_bar(aes(x=day_s, y=mpd), stat="identity") +
  facet_grid(~ tmp)

【讨论】:

【参考方案3】:

tmp设置为因子

tab_tra$tmp<- as.factor(tab_tra$tmp)

然后

ggplot(tab_tra)  + 
  geom_bar(aes(x=day_s, y=mpd, fill = tmp), stat="identity" )

【讨论】:

【参考方案4】:

您可以使用dodge()进行自定义

 ggplot(df, aes(fill=tmp))  + 
    geom_bar(aes(x=day_s, y=mpd, group=tmp),stat="identity", position = position_dodge(width = 0.9)) +
          theme_bw()

ggplot(df, aes(fill=tmp))  + 
geom_bar(aes(x=day_s, y=mpd, group=tmp),stat="identity", position = position_dodge2(width = 0.5, preserve = "single", padding = -0.5)) +
  theme_bw()

【讨论】:

以上是关于多个组的ggplot条形图+折线图的主要内容,如果未能解决你的问题,请参考以下文章

【R语言】--- ggplot2绘制折线图

绘制多个组的嵌套分类值的折线图 (ggplot2)

java生成饼状图,条形图,折线图的技术可以动态的显示

报表(帆软)设计——巧用组合图(折线图)

带折线图的条形图 - 使用非数字索引

从折线图开始的条形图