Combining many plot types in ggplot2
The layer model of ggplot2
is a key feature of the library that allows users to create complex visualizations by building up layers of data, aesthetics, and geoms. Each layer represents a different aspect of the plot, and they are added on top of each other to create the final visualization. In this recipe, we’ll use the layer model to create a complex plot of data in the palmerpenguins
package. It may be helpful to inspect the data in R directly by printing it to the screen. Also, the package is well documented at https://allisonhorst.github.io/palmerpenguins/, should you wish to look more into how it was generated.
Getting ready
Install the ggplot2
and palmerpenguins
packages.
How to do it…
We can use the layer system to combine multiple plot types as follows:
- Create the base for the plot:
library(ggplot2)library(palmerpenguins)p <- ggplot(data = penguins) + aes(x = bill_length_mm, y = bill_depth_mm...