Learn R: Fundamentals of Data Visualization with ggplot2
Learn the basics of how to create visualizations using the popular R package ggplot2.
StartKey Concepts
Review core concepts you need to learn to master this subject
Grammar of Graphics with ggplot2
Geom Aesthetics
ggplot2 Bar Chart
ggplot2 Aesthetics
ggplot2 Labels
ggplot() Initializes a ggplot Object
Grammar of Graphics with ggplot2
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.
In the visual above:
- The first line declares the data that will be used in the plot (
mtcars
) and creates the aesthetic mapping ofwt
tompg
. - 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
- 1Hello 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…
- 2When 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…
- 3The 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…
- 4Before 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…
- 5In 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…
- 6Before 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…
- 7In 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…
- 8We’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….
- 10We’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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory