在ggplot 2中在各个方面注释我想用以下代码对情节的最后一个方面进行注释:library(ggplot2)p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()p <- p + facet_grid(. ~ cyl)p <- p + annotate("text", label = "Test", size = 4, x = 15, y = 5)print(p)但这段代码在每个方面都对文本进行了注释。如果你能指导我如何只从一个方面得到注释的文本,我将不胜感激。提前谢谢。
3 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
ann_text <- data.frame(mpg = 15,wt = 5,lab = "Text", cyl = factor(8,levels = c("4","6","8")))p + geom_text(data = ann_text,label = "Text")
jeck猫
TA贡献1909条经验 获得超7个赞
library(ggplot2)p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl) + theme(panel.spacing = unit(1, "lines"))p
dat_text <- data.frame( label = c("4 cylinders", "6 cylinders", "8 cylinders"), cyl = c(4, 6, 8))p + geom_text( data = dat_text, mapping = aes(x = -Inf, y = -Inf, label = label), hjust = -0.1, vjust = -1)
dat_text <- data.frame( label = c("4 cylinders", "6 cylinders", "8 cylinders"), cyl = c(4, 6, 8), x = c(20, 27.5, 25), y = c(4, 4, 4.5))p + geom_text( data = dat_text, mapping = aes(x = x, y = y, label = label))
dat_text <- data.frame( cyl = c(4, 6, 8, 4, 6, 8), am = c(0, 0, 0, 1, 1, 1))dat_text$label <- sprintf( "%s, %s cylinders", ifelse(dat_text$am == 0, "automatic", "manual"), dat_text$cyl)p + facet_grid(am ~ cyl) + geom_text( size = 5, data = dat_text, mapping = aes(x = Inf, y = Inf, label = label), hjust = 1.05, vjust = 1.5 )
你可以用 -Inf
和 Inf
若要将文本定位到面板的边缘,请执行以下操作。 你可以用 hjust
和 vjust
若要调整文本的合理性,请执行以下操作。 文本标签数据框架 dat_text
应该有一个与您的 facet_grid()
或 facet_wrap()
.
烙印99
TA贡献1829条经验 获得超13个赞
ann_text <- data.frame(mpg = 15,wt = 5, cyl = factor(8,levels = c("4","6","8")))p + geom_text(data = ann_text,label = "Text" )
ann_text <- data.frame(mpg = c(14,15),wt = c(4,5),lab=c("text1","text2"), cyl = factor(c(6,8),levels = c("4","6","8")))p + geom_text(data = ann_text,aes(label =lab) )
- 3 回答
- 0 关注
- 685 浏览
添加回答
举报
0/150
提交
取消