Generally, you do not need to print or plot a ggplot2 plot explicitly: the
default top-level print method will do it for you. You will, however, need
to call print()
explicitly if you want to draw a plot inside a
function or for loop.
Examples
colours <- list(~class, ~drv, ~fl)
# Doesn't seem to do anything!
for (colour in colours) {
ggplot(mpg, aes_(~ displ, ~ hwy, colour = colour)) +
geom_point()
}
#> Warning: `aes_()` was deprecated in ggplot2 3.0.0.
#> ℹ Please use tidy evaluation idioms with `aes()`
# Works when we explicitly print the plots
for (colour in colours) {
print(ggplot(mpg, aes_(~ displ, ~ hwy, colour = colour)) +
geom_point())
}