0

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

benson23
  • 16,369
  • 9
  • 19
  • 38
j-asano
  • 35
  • 4

2 Answers2

1

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

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • I tried this and for some reason the oto_grob isn't showing up on my plot still. I am getting the following error message : `geom_smooth()` using formula 'y ~ x' Warning messages: 1: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric, : Chernobyl! trL>n 20 2: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric, : Chernobyl! trL>n 20 3: In sqrt(sum.squares/one.delta) : NaNs produced 4: In stats::qt(level/2 + 0.5, pred$df) : NaNs produced 5: In max(ids, na.rm = TRUE) : no non-missing arguments to max; returning -Inf – j-asano Mar 05 '22 at 19:15
  • @j-asano This is a warning message, not an error (I get it too). You should tune the parameters `xmin`, `xmax`, `ymin`, `ymax` inside `annotation_custom`. Otherwise, you should share your `otolith_illustration.png` (maybe it is not the same that I used in my code). – Marco Sandri Mar 06 '22 at 12:36
0

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
j-asano
  • 35
  • 4