Key Concepts

Review core concepts you need to learn to master this subject

Grammar of Graphics with ggplot2

ggplot2 uses the basic units of the “grammar of graphics” to construct data visualizations in a layered approach.

The basic units in the “grammar of graphics” consist of:

  • The data or the actual information that is to be visualized.
  • The geometries, shortened to “geoms”, which describe the shapes that represent the data. These shapes can be dots on a scatter plot, bar charts on the graph, or a line to plot the data. Data are mapped to geoms.
  • The aesthetics, or the visual attributes of the plot, including the scales on the axes, the color, the fill, and other attributes concerning appearance.

Visualizations in ggplot2 begin with a blank canvas, which is just an empty plot with data associated to it. Geoms are “added” as layers to the original canvas, adding representations of the data to the visualization.

Diagram visualizing the inheritance between the canvas, the aesthetics defined with the ggplot function, and the subsequent layers.

In the visual above:

  • The first line declares the data that will be used in the plot (mtcars) and creates the aesthetic mapping of wt to mpg.
  • The second line creates the data point geom layer.
  • The third line creates the smoothed geom layer.

The key is that the aes() (aesthetic function) on line one maps the data onto each of the two geom layers

Intro to Visualization with R
Lesson 1 of 1

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo