多组ggplot条形图+折线图

ggplot bar plot by multiple groups + line graph

我有这个数据集:

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()

您可以使用 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()

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

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

tmp设置为一个因素

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

然后

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

您可以使用 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()