【发布时间】:2020-07-04 18:10:44
【问题描述】:
这是我向here 提出的类似问题的扩展/破解。
我在ggplot 中创建了一个图表,我想用一个矩形覆盖facet_grid 的标题。
使用geom_rect 我设法将矩形分布在每个方面。但是,如何将矩形分布在标题上?
预期图表:
示例数据和脚本:
library(tidyverse)
df <- head(mtcars, 5)
plot <- df %>%
ggplot(aes(gear, disp)) +
geom_bar(stat = "identity") +
facet_grid(~am + carb,
space = "free_x",
scales = "free_x") +
ggplot2::theme(
panel.spacing.x = unit(0,"cm"),
axis.ticks.length=unit(.25, "cm"),
strip.placement = "outside",
legend.position = "top",
legend.justification = "center",
legend.direction = "horizontal",
legend.key.size = ggplot2::unit(1.5, "lines"),
# switch off the rectangle around symbols
legend.key = ggplot2::element_blank(),
legend.key.width = grid::unit(2, "lines"),
# # facet titles
strip.background = ggplot2::element_rect(
colour = "black",
fill = "white"),
panel.background = ggplot2::element_rect(
colour = "white",
fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
plot +
geom_rect(aes(xmin=2.4, xmax=2.7, ymin=400, ymax=300),
color="black", fill="white") +
geom_text(aes(x = 2.5, y = 400, label="world"), size=2)
【问题讨论】:
-
不清楚你想要什么。您可以在 Word/PPT/Paint 中手动绘制预期的输出吗?
-
我想最简单的方法是单独创建每个情节,并与拼凑而成。
-
我现在才在经历了这么多黑客攻击之后,突然想起了
ggnomics包。查看我的第二个答案stackoverflow.com/a/60832084/7941188
标签: r ggplot2 facet-grid