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

Mar*_*tin 1 r ggplot2

是否可以在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
Run Code Online (Sandbox Code Playgroud)

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

bap*_*ste 7

这可能是最简单的方法,

gridExtra::grid.arrange(ggplot2::ggplot(), right = "this is a note")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

默认情况下文本旋转90度,以覆盖此使用textGrob显式,

gridExtra::grid.arrange(ggplot2::ggplot(), right = grid::textGrob("this is a note"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述