rggplot2gridinext

Override aesthetics of custom plot


I am not sure exactly how to override aesthetic properties of a custom plot made with ggplot (in the context of iNEXT package). The only way I could think of right now was using the functionality of the grid package, though is really hackish. Maybe there is a easier way, like using guides or so from ggplot2, though I could't manage to make it work?

Below is an example where I just want to adjust the line width in the graph. Of course, I would like that to trickle down in the legend as well. So, below are my steps with grid, but any simpler solution is greatly appreciated (ideally something that doesn't need grid but just ggplot2, if possible).

library(iNEXT)
library(ggplot2)
library(grid)

# Some custom plot from the iNEXT package
data(spider)
out <- iNEXT(spider, q=0, datatype="abundance")

custom_plot <- ggiNEXT(out)
custom_plot

# Get the grobs
g <- grid.force(ggplotGrob(custom_plot))

# Check the list of names of grobs:
# grid.ls(g) 
# View(g$grobs)

# Get an idea about the grob paths
gpaths <- paste(gsub(pattern = "layout::", 
                     replacement = "", 
                     x = grid.ls(g, print = FALSE)$gPath), 
                grid.ls(g, print = FALSE)$name, 
                sep = "::")
gpaths[grepl("polyline", gpaths)]
#> [1] "panel.7-5-7-5::grill.gTree.114::panel.grid.minor.y..polyline.107"            
#> [2] "panel.7-5-7-5::grill.gTree.114::panel.grid.minor.x..polyline.109"            
#> [3] "panel.7-5-7-5::grill.gTree.114::panel.grid.major.y..polyline.111"            
#> [4] "panel.7-5-7-5::grill.gTree.114::panel.grid.major.x..polyline.113"            
#> [5] "panel.7-5-7-5::GRID.polyline.91"                                             
#> [6] "panel.7-5-7-5::geom_ribbon.gTree.101::geom_ribbon.gTree.95::GRID.polyline.93"
#> [7] "panel.7-5-7-5::geom_ribbon.gTree.101::geom_ribbon.gTree.99::GRID.polyline.97"

# Edit the width of the lines
g <- editGrob(grob = g, 
              gPath = gpaths[grepl("panel.7-5-7-5::GRID.polyline", gpaths)],
              gp = gpar(lwd = c(1,1,1,1)))
plot(g)

Created on 2020-07-22 by the reprex package (v0.3.0)


Solution

  • The answer you are looking for is under "Draw R/E curves by yourself" at https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html.

    Fortunately, the authors of the package have provided the function fortify() along with some code to copy verbatim, to achieve what you desire.

    You should copy the following from that section and change the lwd (line width) parameter in the geom_line() function call to your liking.

    df <- fortify(out, type=1) # Note the type parameter!
    
    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"))
     
    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") +
      theme(legend.position = "bottom", 
            legend.title=element_blank(),
            text=element_text(size=18),
            legend.box = "vertical")