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
  1. 1
    Hello there! Today, we will be learning how to make visualizations with R’s popular ggplot2 package. You can create simple visualizations with base R, but we are c…
  2. 2
    When you learn grammar in school you learn about the basic units to construct a sentence. The basic units in the “grammar of graphics” consist of: + The data or the actual information you wish t…
  3. 3
    The first thing you’ll need to do to create a ggplot object is invoke the ggplot() function. Conceptualize this step as initializing the “canvas” of the visualization. In this step, it’s also stand…
  4. 4
    Before we go any further, let’s stop to understand when the data gets bound to the visualization: + Data is bound to a ggplot2 visualization by passing a data frame as the first argument in the gg…
  5. 5
    In the context of ggplot, aesthetics are the instructions that determine the visual properties of the plot and its geometries. Aesthetics can include things like the scales for the x and y axes, t…
  6. 6
    Before we teach you how to add aesthetics specific to a geom layer, let’s create our first geom! As mentioned before, geometries or geoms are the shapes that represent our data. In ggplot, there a…
  7. 7
    In the previous exercises, we added geoms to the plot and explored the idea of layers inheriting the original aesthetic mappings of the canvas. Sometimes, you’ll want individual layers to have thei…
  8. 8
    We’ve reviewed how to assign data-driven aesthetic mappings at the canvas level and at the geom level. However, sometimes you’ll want to change an aesthetic based on visual preference and not data….
  9. 9
    So far, we’ve reviewed how to add geometries to represent our data. We’ve also learned how to modify aesthetic values in our plot- whether those aesthetics are data-driven or assigned manually. Ano…
  10. 10
    We’ve gone over each of the basic units in the grammar of graphics: data, geometries, and aesthetics. Let’s extend this new knowledge to create a new type of plot: [the bar chart](https://ggplot2.t…
  11. 11
    You’ve completed the introduction to ggplot lesson! You’re ready to follow the general pattern for creating a visualization: 1. Determine what relationship you wish to explore in your data 2. Find…

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