r - 在情节上方注释ggplot
问题描述
我最近尝试用 ggplot 上方的框来注释图表。这是我想要的:
我找到了一种使用 的方法grid
,但我觉得它太复杂了,我很确定有更好的方法可以做到这一点,更ggplot2
友好。这是示例和我的解决方案:
数据:
y2 <- 350
mesure_pol <- data.frame(x1 = c(1,4,7),
x2 = c(4,7,10),
politiquecat = c(1:3),
politique = c("Phase 1\n","Phase 2\n","Phase 3\n"),
y = c(y2,y2,y2)
)
mesure_pol$x_median <- (mesure_pol$x1 + mesure_pol$x2)/2
colorpal <- viridis::inferno(n=3,direction = -1)
阴谋
主要情节
p <- ggplot(data = mesure_pol) +
geom_rect(aes(xmin = x1,
xmax = x2,
ymin = 0,
ymax = 300,
fill = as.factor(politiquecat)),
fill = colorpal,
color = "black",
size = 0.3,
alpha = 0.2)+
theme(plot.margin=unit(c(60, 5.5, 5.5, 5.5), "points"))+
coord_cartesian(clip = 'off')
注释部分
这是我不满意的部分:
for (i in 1:dim(mesure_pol)[1]) {
text <- textGrob(label = mesure_pol[i,"politique"], gp = gpar(fontsize=7,fontface="bold"),hjust = 0.5)
rg <- rectGrob(x = text$x, y = text$y, width = stringWidth(text$label) - unit(3,"mm") ,
height = stringHeight(text$label) ,gp = gpar(fill=colorpal[i],alpha = 0.3))
p <- p + annotation_custom(
grob = rg,
ymin = mesure_pol[i,"y"], # Vertical position of the textGrob
ymax = mesure_pol[i,"y"],
xmin = mesure_pol[i,"x_median"], # Note: The grobs are positioned outside the plot area
xmax = mesure_pol[i,"x_median"]) +
annotation_custom(
grob = text,
ymin = mesure_pol[i,"y"], # Vertical position of the textGrob
ymax = mesure_pol[i,"y"],
xmin = mesure_pol[i,"x_median"], # Note: The grobs are positioned outside the plot area
xmax = mesure_pol[i,"x_median"])
}
有没有更简单/更好的方法来获得类似的结果?我试过了annotate
,label
但没有任何运气。
解决方案
实现所需结果的另一种方法是通过第二个 ggplot 进行注释,该 ggplot 可以通过例如粘合到主图patchwork
。
对于注释图,我基本上将您的代码用于主图,添加了一个geom_text
图层,摆脱了 axix 等,theme_void
并设置了与主图一致的限制。主要区别在于我将 y 轴限制为 0 到 1 的比例。除此之外,我改变了 xmin、xmax、ymin 和 ymax 值以在矩形周围添加一些空间(因此设置限制很重要)。
library(ggplot2)
library(patchwork)
y2 <- 350
mesure_pol <- data.frame(x1 = c(1,4,7),
x2 = c(4,7,10),
politiquecat = c(1:3),
politique = c("Phase 1\n","Phase 2\n","Phase 3\n"),
y = c(y2,y2,y2)
)
mesure_pol$x_median <- (mesure_pol$x1 + mesure_pol$x2)/2
colorpal <- viridis::inferno(n=3,direction = -1)
p <- ggplot(data = mesure_pol) +
geom_rect(aes(xmin = x1,
xmax = x2,
ymin = 0,
ymax = 300,
fill = as.factor(politiquecat)),
fill = colorpal,
color = "black",
size = 0.3,
alpha = 0.2)
ann <- ggplot(data = mesure_pol) +
geom_rect(aes(xmin = x1 + 1,
xmax = x2 - 1,
ymin = 0.2,
ymax = 0.8,
fill = as.factor(politiquecat)),
fill = colorpal,
color = "black",
size = 0.3,
alpha = 0.2) +
geom_text(aes(x = x_median, y = .5, label = politique), vjust = .8, fontface = "bold", color = "black") +
coord_cartesian(xlim = c(1, 10), ylim = c(0, 1)) +
theme_void()
ann / p +
plot_layout(heights = c(1, 4))
推荐阅读
- html - 选中网页上的复选框
- java - 在没有服务器应用程序/服务器的情况下重新加载配置数据
- sql - SQL 更新表中的所有条目,但在列上插入不同的值
- javascript - Javascript if 语句不适用于表单比较
- c# - 将 MessagePack 与 AspNet Core WebAPI 和控制台应用程序一起使用
- mysql - 第 1 行的错误 1046 (3D000):未选择数据库
- javascript - 使用变换和元素定位/大小
- sql - 在查询本身中过滤 [SQL] VS 提取大数据然后在处理每一行时过滤 [nodejs]?
- json - 为什么 .push() 替换 JSON 数组的最后一个对象?
- php - 循环遍历字符串中的主题标签并插入数据库