使用 inset_element 添加图片时,我所有的 ggplot 注释都消失了?

发布于 2025-01-12 03:54:04 字数 1650 浏览 2 评论 0原文

我正在制作一个包含数据曲线、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

otolith_illustration.png

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!
enter image description here
enter image description here

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

otolith_illustration.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静待花开 2025-01-19 03:54:04

您可以使用 annotate_custom 代替 inset_element

library(png)
library(ggplot2)
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))
oto_ill <- readPNG('otolith_illustration.png', native = TRUE)
oto_grob <- grid::rasterGrob(oto_ill, interpolate=TRUE)

o18.plot2 <- ggplot(data=o18, aes(x=Sample, y = O18)) +
  geom_line(colour='NA') +
  geom_smooth(method="loess", span=0.2) +  
  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()) +
  annotation_custom(oto_grob, xmin=1, xmax=6, ymin=0, ymax=3.5) +
  theme_classic()

o18.plot2

输入图片此处描述

You can use annotate_custom instead of inset_element.

library(png)
library(ggplot2)
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))
oto_ill <- readPNG('otolith_illustration.png', native = TRUE)
oto_grob <- grid::rasterGrob(oto_ill, interpolate=TRUE)

o18.plot2 <- ggplot(data=o18, aes(x=Sample, y = O18)) +
  geom_line(colour='NA') +
  geom_smooth(method="loess", span=0.2) +  
  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()) +
  annotation_custom(oto_grob, xmin=1, xmax=6, ymin=0, ymax=3.5) +
  theme_classic()

o18.plot2

enter image description here

肩上的翅膀 2025-01-19 03:54:04

以下代码有效,使用拼凑并将 oto_grob 移动到绘图的最后一行代码。

library(png)
oto_ill <- readPNG('otolith_illustration.png', native = TRUE)
oto_grob <- grid::rasterGrob(oto_ill, interpolate=TRUE)
#make the plot with annotations
library(patchwork)
o18
o18.plot2 <- ggplot(data=o18, aes(x=Distance, y = O18)) +
geom_line(colour='NA') +
  labs(y=expression(delta^18*O), x = "Distance from Core (mm)") +
theme_classic()+
geom_smooth(method="loess", span=0.2) +
   annotate(geom="text", x=.19, y=2.49, label="otolith", size=4, 
            color='black',parse=TRUE) +
   annotate(geom="text", x=.35, y=2.4, label="1st winter" , size=4, 
            color='black',parse=FALSE) +
   annotate(geom="text", x=.57, y=2.6, label="2nd winter", size=4, 
            color='black',parse=FALSE) +
   annotate("segment", x = .35, xend = .35, y = 2.3, yend = 2.1, 
          colour = "black", size=3, alpha=0.8, arrow=arrow()) +
   annotate("segment", x = .57, xend = .57, y = 2.5, yend = 2.3, 
          colour = "black", size=3, alpha=0.8, arrow=arrow()) +
  annotate(geom="text", x=.15, y=.05, label="larval stage", size=4, 
           color='black',parse=FALSE) +
  annotate(geom="text", x=.663, y=.05, label="catch", size=4, 
           color='black',parse=FALSE) +
annotate("segment", x = .2, xend = .63, y = .05, yend = .05, 
         colour = "black", size=3, alpha=0.8, arrow=arrow())+
  inset_element(oto_grob, left =0.068, right =0.35, bottom = 0.4, top =.9,
                align_to = 'full')
o18.plot2

The following code worked, using patchwork and moving the oto_grob to the final line of code for the plot.

library(png)
oto_ill <- readPNG('otolith_illustration.png', native = TRUE)
oto_grob <- grid::rasterGrob(oto_ill, interpolate=TRUE)
#make the plot with annotations
library(patchwork)
o18
o18.plot2 <- ggplot(data=o18, aes(x=Distance, y = O18)) +
geom_line(colour='NA') +
  labs(y=expression(delta^18*O), x = "Distance from Core (mm)") +
theme_classic()+
geom_smooth(method="loess", span=0.2) +
   annotate(geom="text", x=.19, y=2.49, label="otolith", size=4, 
            color='black',parse=TRUE) +
   annotate(geom="text", x=.35, y=2.4, label="1st winter" , size=4, 
            color='black',parse=FALSE) +
   annotate(geom="text", x=.57, y=2.6, label="2nd winter", size=4, 
            color='black',parse=FALSE) +
   annotate("segment", x = .35, xend = .35, y = 2.3, yend = 2.1, 
          colour = "black", size=3, alpha=0.8, arrow=arrow()) +
   annotate("segment", x = .57, xend = .57, y = 2.5, yend = 2.3, 
          colour = "black", size=3, alpha=0.8, arrow=arrow()) +
  annotate(geom="text", x=.15, y=.05, label="larval stage", size=4, 
           color='black',parse=FALSE) +
  annotate(geom="text", x=.663, y=.05, label="catch", size=4, 
           color='black',parse=FALSE) +
annotate("segment", x = .2, xend = .63, y = .05, yend = .05, 
         colour = "black", size=3, alpha=0.8, arrow=arrow())+
  inset_element(oto_grob, left =0.068, right =0.35, bottom = 0.4, top =.9,
                align_to = 'full')
o18.plot2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文