我有以下绘图代码
ggplot(na.omit(total), aes(x = day_after, y = use, group=t, na.rm=TRUE)) +
geom_line(aes(linetype=t, color=t, na.rm=TRUE))+
geom_point(aes(color=t, na.rm = TRUE)) +
scale_color_manual(values=c("gray48", "indianred4"), labels=c("Treatment","Control")) +
labs(x= "Days after beginning of deactivation period", y= "") +
theme(panel.grid.minor = element_blank(),
panel.background = element_blank()) +
theme(panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line( size=.1, color="grey"))+
theme(legend.key=element_blank()) +
guides(linetype = FALSE) +
annotate('segment',x = 0,xend = 0,y = 0,yend = 60,size = 3,colour = "grey",
alpha = 0.4) +
annotate('segment',x = 7,xend = 7,y = 0,yend = 60,size = 3,colour = "grey",
alpha = 0.4) +
annotate('segment',x = 39,xend = 39,y = 0,yend = 60,size = 3,colour = "grey",
alpha = 0.4)
new_plot <- plot + theme(legend.title=element_blank(), legend.text = element_text(size=10),
legend.position = "right")
我想要做的是在图的顶部在图外部的多个给定 (x,y) 值处添加垂直文本。 Annotate_custom 可以工作,但它没有旋转文本的选项。基本上我尝试了+annotation_custom(text_end,xmin=42,xmax=42,ymin=62,ymax=62) 这太棒了,只有我不能有像angle=90这样的选项。 有什么帮助吗??
您可以使用
annotation_custom
,只要您关闭剪辑即可。您可以使用自定义 grid::textGrob
来指定旋转。
我没有您的数据,但使用内置
mtcars
数据集的示例应该足以说明问题。
library(ggplot2)
ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
geom_vline(xintercept = c(200, 400), size = 10, alpha = 0.05) +
coord_cartesian(clip = "off") +
theme_bw() +
theme(plot.margin = margin(80, 20, 20, 20),
panel.border = element_blank()) +
annotation_custom(grid::textGrob("label 1", rot = 90),
xmin = 200, xmax = 200, ymin = 40) +
annotation_custom(grid::textGrob("label 2", rot = 90),
xmin = 400, xmax = 400, ymin = 40)
我已经尝试了上面的代码(并包含clip =“off”),但标签没有显示。有什么提示吗?