rggplot2legendinext

Trouble adding elements to legend (iNEXT / ggplot2)


I have made a rarefaction curves using the iNEXT package in R and added two horizontal lines for the asymptotes of both curves manually (the package does not do that, so I tried to utilize ggplot functions and it seemed to work), furthermore I extended the x/y axis. Unfortunately, my skills are apparently too limited to figure out how to add the asymptotes in the legend of the graph under, labelled as "Asymptotes: North and South" (or something like that). I would very much appreciate the help. I added my code below and the graph as a picture, if something is missing, let me know!

Best wishes and stay healthy!

### Rarefaction with iNEXT -> Species X Visits incidence_frequency data

# List for both regions
incidence_freq_north <- c(7,2,0,0,4,0,2,0,2,1,0,0,0,6,0,1,1,0,0,0)
incidence_freq_south <- c(23,0,0,1,9,2,0,4,1,1,1,2,6,1,4,1,0,8,2,7,1)

list_rarefaction_freq = list(North = incidence_freq_north, South = incidence_freq_south)

## create output file
out_freq <- iNEXT(list_rarefaction_freq, q=0, datatype="incidence_freq", endpoint=NULL,
             size=NULL, knots=400, se=TRUE, conf=0.95, nboot=400)

# Sample-size-based R/E curves, separating plots by "order"
ggiNEXT(out_freq, type=1, facet.var="order") +
  ylim(c(0,40)) + xlim(c(0,70)) + 
  theme_bw(base_size = 18) + 
  geom_hline(yintercept=24, linetype="solid", color = "darkslategray2") + 
  geom_hline(yintercept=10, linetype="solid", color = "coral1")

Graph as image


Solution

  • I think one possible solution will be to draw your ggiNEXT graph on your own (here they provide a tutorial to do it: https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html)

    Then, you can add a new dataframe corresponding to asymptope values and use new_color_scale function from ggnewscale package to add a new color scale on your graph:

    df <- fortify(out_freq, type =1)
    
    df.point <- df[which(df$method=="observed"),]
    df.line <- df[which(df$method!="observed"),]
    df.line$method <- factor(df.line$method, 
                             c("interpolated", "extrapolated"),
                             c("interpolation", "extrapolation"))
    
    df.asympote <- data.frame(y = c(24,10),
                              Asymptote = c("North","South"))
    
    
    library(ggnewscale)
    
    ggplot(df, aes(x=x, y=y, colour=site)) + 
      geom_point(aes(shape=site), size=5, data=df.point) +
      geom_line(aes(linetype=method), lwd=1.5, data=df.line) +
      geom_ribbon(aes(ymin=y.lwr, ymax=y.upr,
                      fill=site, colour=NULL), alpha=0.2) +
      labs(x="Number of individuals", y="Species diversity") +
      scale_color_discrete(name = "Site")+
      scale_shape_discrete(name = "Site")+
      scale_fill_discrete(name = "Site")+
      scale_linetype_discrete(name = "Method")+
      theme_bw(base_size = 18)+
      new_scale_color()+
      geom_hline(data = df.asympote, aes(yintercept = y, color = Asymptote))
    

    enter image description here

    Does it answer your question ?