使用 inset_element 添加图片时,我所有的 ggplot 注释都消失了?
我正在制作一个包含数据曲线、png 图像插图以及注释的绘图。然而,每次我添加插图时,所有注释都会消失,我不确定我做错了什么。帮助将不胜感激!我仍然需要处理我的轴标签等,但我被困在这里!此外,如果有人有任何风格提示或建议,我会洗耳恭听!
o18 <- data.frame(Sample=1:20, O18 = c(.41,.3, .42,.22, .45,.61, 1.15, 1.77,2.05,
1.93,1.72,1.46,1.25, 1.09,1.38,1.96, 2.23,
1.93, 1.56,1.42))
library(png)
oto_ill <- readPNG('otolith_illustration.png', native = TRUE)
library(patchwork)
o18.plot2 <- ggplot(data=o18, aes(x=Sample, y = O18)) +
geom_line(colour='NA') +
theme_classic()+
geom_smooth(method="loess", span=0.2)+
inset_element(oto_ill, left =0.01, right =0.3, bottom = 0.4, top =1)+
annotate(geom="text", x=3, y=2.6, label="otolith", size=4, color='black',
parse=TRUE)+
annotate(geom="text", x=9, y=2.4, label="1st winter" , size=4, color='black',
parse=FALSE)+
annotate(geom="text", x=17, y=2.6, label="2nd winter", size=4, color='black',
parse=FALSE)+
annotate("segment", x = 9, xend = 9, y = 2.3, yend = 2.1, colour = "black", size=3, alpha=0.6, arrow=arrow())+
annotate("segment", x = 17, xend = 17, y = 2.5, yend = 2.3, colour = "black", size=3, alpha=0.6, arrow=arrow())
o18.plot2
I am working on a plot that will have the data curve, a png image inset, as well as annotations. However, every time I add my inset image all of my annotations disappear and I'm not sure what I am doing wrong. Help would be greatly appreciated! I still need to work on my axis labels and such but am stuck here! Additionally, if anyone has any style tips or recommendations I am all ears!
o18 <- data.frame(Sample=1:20, O18 = c(.41,.3, .42,.22, .45,.61, 1.15, 1.77,2.05,
1.93,1.72,1.46,1.25, 1.09,1.38,1.96, 2.23,
1.93, 1.56,1.42))
library(png)
oto_ill <- readPNG('otolith_illustration.png', native = TRUE)
library(patchwork)
o18.plot2 <- ggplot(data=o18, aes(x=Sample, y = O18)) +
geom_line(colour='NA') +
theme_classic()+
geom_smooth(method="loess", span=0.2)+
inset_element(oto_ill, left =0.01, right =0.3, bottom = 0.4, top =1)+
annotate(geom="text", x=3, y=2.6, label="otolith", size=4, color='black',
parse=TRUE)+
annotate(geom="text", x=9, y=2.4, label="1st winter" , size=4, color='black',
parse=FALSE)+
annotate(geom="text", x=17, y=2.6, label="2nd winter", size=4, color='black',
parse=FALSE)+
annotate("segment", x = 9, xend = 9, y = 2.3, yend = 2.1, colour = "black", size=3, alpha=0.6, arrow=arrow())+
annotate("segment", x = 17, xend = 17, y = 2.5, yend = 2.3, colour = "black", size=3, alpha=0.6, arrow=arrow())
o18.plot2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
annotate_custom
代替inset_element
。You can use
annotate_custom
instead ofinset_element
.以下代码有效,使用拼凑并将 oto_grob 移动到绘图的最后一行代码。
The following code worked, using patchwork and moving the oto_grob to the final line of code for the plot.