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 standard to associate the data frame the rest of the visualization will use with the canvas. What do we mean by “the rest” of the visualization? We mean all the layers you’ll add as you build out your plot.

As we mentioned, at its heart, a ggplot visualization is a combination of layers that each display information or add style to the final graph. You “add” these layers to a starting canvas, or ggplot object, with a + sign. We’ll add geometries and aesthetics in the next exercises. For now, let’s stop to understand that any arguments inside the ggplot() function call are inherited by the rest of the layers on the plot.

Here we invoke ggplot() to create a ggplot object and assign the dataframe df, saving it inside a variable named viz:

viz <- ggplot(data=df) viz

Note: The code above assigns the value of the canvas to viz and then states the variable name viz after so that the visualization is rendered in the notebook.

Any layers we add to viz would have access to the dataframe. We mentioned the idea of aesthetics before. It’s important to understand that any aesthetics that you assign as the ggplot() arguments will also be inherited by other layers. We’ll explore what this means in depth too, but for now, it’s sufficient to conceptualize that arguments defined inside ggplot() are inherited by other layers.


Take a look at the animated diagram to the right.

  • Notice how initially, a ggplot object is created as a blank canvas. Notice that the aesthetics are then set inside the ggplot() function as an argument.
  • You could individually set the scales for each layer, but if all layers will use the same scales, it makes sense to set those arguments at the ggplot() “canvas” level.
  • When two subsequent layers are added, a scatter plot and a line of best fit, both of those layers are mapped onto the canvas using the same data and scales.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?