I am not sure exactly how to override aesthetic properties of a custom plot made with ggplot. 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)