繁体   English   中英

r ggplot2-自定义文本而不是图例

[英]r ggplot2 - custom text instead of the legend

ggplot2中是否可以用自定义文本替换图例? 我知道注释,但是我不想写在图表上,而是在图表的旁边(或下面)-确切地说明图例的位置,并以一种相当简单的方式进行绘制。

例如在这个简单的图表中

library(data.table)
library(ggplot2)
library(ggrepel)

id <- c(1:10) 
x1 <- sample(1:10, 10, replace=T)
x2 <- sample(1:10, 10, replace=T)
x3 <- sprintf("Point.%d",id)
df<-data.frame(id,x1,x2,x3)
dt<-data.table(df)
setkeyv(dt,c("id"))

p<-ggplot(data=dt,aes(x1,x2))+geom_point()+geom_text_repel(aes(label=x3))+
ggtitle("Graph")+xlab("X")+ylab("Y")+theme_bw()
p

我想在图表旁边写一些内容(简短)。 恐怕在ggplot2中这可能不容易实现,因为它超出了它的目的-但这对我有很大帮助。

这可能是最简单的方法,

gridExtra::grid.arrange(ggplot2::ggplot(), right = "this is a note")

在此处输入图片说明

默认情况下,文字会旋转90度,以明确使用textGrob覆盖此文字,

gridExtra::grid.arrange(ggplot2::ggplot(), right = grid::textGrob("this is a note"))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM