Aesthetics in the ggplot2 Package
Explore the role of aesthetics—an element of the grammar of graphics.
Plot layers in ggplot2
Any plot in the ggplot2
package is created using the ggplot()
function. It must have the following three components (layers) as specified by the layered grammar of graphics:
1. Data: The data to be used should be present in a data frame.
2. Aesthetics: These are represented by the aes()
function, which is responsible for positioning/mapping the variables on the plot.
3. Geometry: The geom
function directs the plot on how to present the data in R. For example, we will use the geom_line()
function to create a line chart. Many predefined geom
functions are available to make different types of plots.
In simple words, these three components can be seen as layers that, added on top of each other, will form the resulting plot. The following syntax is used to create all types of plots in the ggplot2
package:
Get hands-on with 1200+ tech skills courses.