r - 将 grid.text 添加到 ggplot 时出错

标签 r ggplot2

我正在尝试使用 ggplot2 中的 grid.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

提前致谢!

最佳答案

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

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

enter image description here

这个情节是用:

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

因为 x = 0.18 不会显示。

关于r - 将 grid.text 添加到 ggplot 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12115701/

相关文章:

r - 使用 ggplot2 绘制 POSIXct 时间戳系列

r - 将子字符串中的字符分配给facet_grid labeller 的原始字符串

r - 使用 ggplot2 绘制 fitdist 图

r - ggplot2:如何交换 y 轴和右刻面带以及如何排序刻面值

r - 更新来自 javascript 的 Shiny 输入

java - 在 Shiny 中添加链接面板选项卡和各种顶级导航栏

r - 在 rmarkdown ioslides 中包含 block 引用

r - 如何在 R 中按名称获取最多嵌套列表值

r - 使用knitr从R转换为LaTeX时管道后的空间

python - ggplot2 不存在于 python 的 rpy2 中?