英文:
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")))
Desired output: (check red lines)
英文:
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(.8, "lines")))
答案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")))
英文:
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 %>%
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")))
答案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 = "red")
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.
If it's more about separating the different facets, then adding colour = "red"
to your strip.background
element in the theme
is an alternative approach:
答案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")
英文:
An alternative is using 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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论