将grid.text添加到ggplot时出错

Lin*_*ird 3 r ggplot2

我想使用grid.textggplot2一个文本框添加到我的阴谋.该图本身工作正常,但是当我添加grid.text命令时,我收到错误"不知道如何将o添加到绘图中".如果使用last_plot(),我仍然会收到错误,但字母显示在图表上 - 但是不会保存其余的图表.数据集和命令如下:

foldchange  order
1.583591249 1c
1.973012368 1c
1.339505031 1c
0.776845711 2c
1.004515622 2c
1.225864907 2c
13.27371225 3c
7.599476289 3c
10.74132453 3c
3.347536996 4c
4.286202467 4c
3.612756449 4c
17.40825874 5c
20.61039144 5c

ggplot(test, aes(order, foldchange))  + geom_point()  #this part works fine
+ grid.text(label="a", x=.18, y=.9) +  #this part gives me the error
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Luc*_*zer 7

那是因为grid.text是网格的一部分,而不是ggplot.此外,grid.text只绘制一些它不会将它添加到ggplot对象的底层结构的东西.您正在寻找注释.

ggplot(test, aes(order, foldchange))  + geom_point() +
annotate(geom = "text", label="a", x=.18, y=.9)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

该情节的制作如下:

ggplot(test, aes(order, foldchange))  + geom_point() +
annotate(geom = "text", label="a", x=5, y=.9)
Run Code Online (Sandbox Code Playgroud)

因为x = 0.18不会显示.