添加grid.text到ggplot时出现错误。

3
我正在尝试在我的绘图中使用ggplot2grid.text来添加一个文本框。 单独使用绘图没有问题,但是当我添加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

提前感谢您!

1个回答

7
那是因为grid.text是grid的一部分,而不是ggplot。此外,grid.text只绘制某些内容,而不将其添加到ggplot对象的底层结构中。 您要寻找的是annotate。
ggplot(test, aes(order, foldchange))  + geom_point() +
annotate(geom = "text", label="a", x=.18, y=.9)

输入图像描述

此图是使用以下方法生成的:

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

因为x = 0.18没有被显示出来。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接