如何在ggplot中在分面条之间添加垂直线?

huangapple go评论84阅读模式
英文:

How to add vertical lines between facet strips in ggplot?

问题

reprex:

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(0.8, "lines")))

Output:
如何在ggplot中在分面条之间添加垂直线?

Desired output: (check red lines)
如何在ggplot中在分面条之间添加垂直线?

英文:

reprex:

library(tidyverse)

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

df %&gt;% 
  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 = &quot;x&quot;,
                      scales = &quot;free_x&quot;,
                      space = &quot;free_x&quot;) +
theme(strip.background = element_rect(fill = &quot;white&quot;),
      strip.text = element_text(size = 25, lineheight = 0.6),
      strip.placement = &quot;outside&quot;,
      axis.text.x = element_text(angle = 0, size = 27, lineheight = unit(.8, &quot;lines&quot;)))

Output:
如何在ggplot中在分面条之间添加垂直线?

Desired output: (check red lines)
如何在ggplot中在分面条之间添加垂直线?

答案1

得分: 2

没有与您想要的线条匹配的主题元素,因此您需要创造性地解决这个问题。我更喜欢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")))

如何在ggplot中在分面条之间添加垂直线?

英文:

There are no theme elements that match with the lines you want, so you will need to get creative. I prefer a ggplot to be self-contained, so rather than drawing the lines over the plot separately, I would probably draw these in as geom_path elements. This requires using coord_cartesian with clipping off and a fixed y axis limit:

df %&gt;% 
  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 = &quot;x&quot;,
                      scales = &quot;free_x&quot;,
                      space = &quot;free_x&quot;) +
  coord_cartesian(clip = &quot;off&quot;, ylim = c(0, 3)) +
  geom_path(data = data.frame(epoca = rep(&quot;S&quot;, 6),
                              n = rep(c(-0.55, -0.15), 3),
                              ffvv = c(&quot;a&quot;, &quot;a&quot;, rep(&quot;b&quot;, 4)),
                              ano = rep(c(&quot;2019&quot;, &quot;2021&quot;, &quot;2022&quot;), 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 = &quot;outside&quot;,
        axis.text.x = element_text(angle = 0, size = 27, 
                                   lineheight = unit(.8, &quot;lines&quot;)))

如何在ggplot中在分面条之间添加垂直线?

答案2

得分: 2

另一种(手动)解决方案是使用 {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(),但这意味着您不必处理现有图的裁剪和坐标。

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

英文:

Another (manual) solution is using {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 = &quot;red&quot;)

where g is the plot you already have, saved as a variable. It's not a perfect solution, since it usually requires a bit-of-trial and error to get the coordinates right. It uses geom_path() under the hood, but means you don't have to mess with clipping and coordinates of existing plots.

如何在ggplot中在分面条之间添加垂直线?

If it's more about separating the different facets, then adding colour = &quot;red&quot; to your strip.background element in the theme is an alternative approach:

如何在ggplot中在分面条之间添加垂直线?

答案3

得分: 0

一个替代方法是使用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")

如何在ggplot中在分面条之间添加垂直线?

英文:

An alternative is using geom_segment():

library(tidyverse)

df %&gt;% 
  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 = &quot;x&quot;,
                      scales = &quot;free_x&quot;,
                      space = &quot;free_x&quot;) +
  theme(strip.background = element_rect(fill = &quot;white&quot;),
        strip.text = element_text(size = 25, lineheight = 0.6),
        strip.placement = &quot;outside&quot;,
        axis.text.x = element_text(angle = 0, size = 27, lineheight = unit(.8, &quot;lines&quot;)))+
  theme(axis.title.x = element_text(margin=margin(50,0,0,0))) +
  coord_cartesian(clip = &quot;off&quot;, ylim = c(0, 3)) +
  geom_segment(aes(x = 3, y = 0, xend = 3, yend = -0.5), color = &quot;red&quot;)

如何在ggplot中在分面条之间添加垂直线?

huangapple
  • 本文由 发表于 2023年6月2日 04:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定