R语言 如何在ggplot中添加面带之间的垂直线?

bjg7j2ky  于 2023-06-03  发布在  其他
关注(0)|答案(3)|浏览(211)

重复:

library(tidyverse)

df <- tibble::tibble(
  epoca = factor(c("H", "S", "S", "H", "S", "H", "S"), levels = c("S", "H")),
  ano = c("2019", "2018", "2019", "2021", "2022", "2022", "2021"),
  n = rep(3:1, c(3L, 2L, 2L)),
  etiqueta = rep(c("3", "2", "1"), c(3L, 2L, 2L)),
  ffvv = c(rep("a", 3), rep("b", 4))
)

df %>% 
  ggplot(aes(x = epoca, y = n, fill = epoca)) +
  geom_col(width = 0.8, position = position_dodge(0.8)) +
  geom_text(aes(x = epoca, label = etiqueta),
            position = position_dodge(0.9), vjust = 0, size = 8) +
  ggh4x::facet_nested(cols = vars(ffvv, ano),
                      switch = "x",
                      scales = "free_x",
                      space = "free_x") +
theme(strip.background = element_rect(fill = "white"),
      strip.text = element_text(size = 25, lineheight = 0.6),
      strip.placement = "outside",
      axis.text.x = element_text(angle = 0, size = 27, lineheight = unit(.8, "lines")))

输出:

所需输出:(勾选红线)

rkkpypqq

rkkpypqq1#

没有与您想要的线条相匹配的主题元素,因此您需要发挥创意。我更喜欢ggplot是独立的,所以与其在图上单独画线,我可能会把它们画成geom_path元素。这需要使用coord_cartesian并关闭裁剪和固定的y轴限制:

df %>% 
  ggplot(aes(x = epoca, y = n, fill = epoca)) +
  geom_col(width = 0.8, position = position_dodge(0.8)) +
  geom_text(aes(x = epoca, label = etiqueta),
            position = position_dodge(0.9), vjust = 0, size = 8) +
  ggh4x::facet_nested(cols = vars(ffvv, ano),
                      switch = "x",
                      scales = "free_x",
                      space = "free_x") +
  coord_cartesian(clip = "off", ylim = c(0, 3)) +
  geom_path(data = data.frame(epoca = rep("S", 6),
                              n = rep(c(-0.55, -0.15), 3),
                              ffvv = c("a", "a", rep("b", 4)),
                              ano = rep(c("2019", "2021", "2022"), each = 2)),
            position = position_nudge(x = -0.6)) +
  theme(strip.background = element_rect(fill = NA),
        strip.text = element_text(size = 25, lineheight = 0.6),
        strip.placement = "outside",
        axis.text.x = element_text(angle = 0, size = 27, 
                                   lineheight = unit(.8, "lines")))

ar7v8xwq

ar7v8xwq2#

另一个(手动)解决方案是使用{cowplot}:

library(cowplot)
ggdraw() + 
  draw_plot(g) +
  draw_line(x = c(0.42, 0.42),
            y = c(0.08, 0.21),
            lwd = 1.5,
            colour = "red")

其中g是您已经拥有的图,保存为变量。这不是一个完美的解决方案,因为它通常需要一点试错来获得正确的坐标。它使用了geom_path(),但这意味着你不必处理现有图的裁剪和坐标。

如果更多的是为了分离不同的方面,那么将colour = "red"添加到theme中的strip.background元素是一种替代方法:

m3eecexj

m3eecexj3#

另一种方法是使用geom_segment()

library(tidyverse)

df %>% 
  ggplot(aes(x = epoca, y = n, fill = epoca)) +
  geom_col(width = 0.8, position = position_dodge(0.8)) +
  geom_text(aes(x = epoca, label = etiqueta),
            position = position_dodge(0.9), vjust = 0, size = 8) +
  ggh4x::facet_nested(cols = vars(ffvv, ano),
                      switch = "x",
                      scales = "free_x",
                      space = "free_x") +
  theme(strip.background = element_rect(fill = "white"),
        strip.text = element_text(size = 25, lineheight = 0.6),
        strip.placement = "outside",
        axis.text.x = element_text(angle = 0, size = 27, lineheight = unit(.8, "lines")))+
  theme(axis.title.x = element_text(margin=margin(50,0,0,0))) +
  coord_cartesian(clip = "off", ylim = c(0, 3)) +
  geom_segment(aes(x = 3, y = 0, xend = 3, yend = -0.5), color = "red")

相关问题