More documents and projects by Elsior Moreira Alves Junior.
In this chapter we’ll get you into the right frame of mind for developing meaningful visualizations with R. You’ll understand that as a communications tool, visualizations require you to think about your audience first. You’ll also be introduced to the basics of ggplot2 - the 7 different grammatical elements (layers) and aesthetic mappings.
In this video we made the distinction between plots for exploring and plots for explaining data. Which of the following are exploratory plots typically NOT?
To get a first feel for ggplot2, let’s try to run some basic ggplot2 commands. Together, they build a plot of the mtcars dataset that contains information about 32 cars from a 1973 Motor Trend magazine. This dataset is small, intuitive, and contains a variety of continuous and categorical variables.
Instructions
Load the ggplot2 package using library(). It is already installed on DataCamp’s servers.
# Load the ggplot2 package
library(ggplot2)
Use str() to explore the structure of the mtcars dataset.
# Explore the mtcars data frame with str()
str(mtcars)
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec: num 16.5 17 18.6 19.4 17 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$ carb: num 4 4 1 1 2 1 4 2 2 4 ...
Hit Submit Answer. This will execute the example code on the right. See if you can understand what ggplot does with the data.
# Execute the following command
ggplot(mtcars, aes(x = cyl, y = mpg)) +
geom_point()
The plot from the previous exercise wasn’t really satisfying. Although cyl (the number of cylinders) is categorical, it is classified as numeric in mtcars. You’ll have to explicitly tell ggplot2 that cyl is a categorical variable.
Instructions
Change the ggplot() command by wrapping factor() around cyl. Hit Submit Answer and see if the resulting plot is better this time.
# Load the ggplot2 package
library(ggplot2)
# Change the command below so that cyl is treated as factor
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_point()
We’ll use several datasets throughout the courses to showcase the concepts discussed in the videos. In the previous exercises, you already got to know mtcars. Let’s dive a little deeper to explore the three main topics in this course: The data, aesthetics, and geom layers.
The mtcars dataset contains information about 32 cars from 1973 Motor Trend magazine. This dataset is small, intuitive, and contains a variety of continuous and categorical variables.
You’re encouraged to think about how the examples and concepts we discuss throughout these data viz courses apply to your own data-sets!
INstructions
ggplot2 has already been loaded for you. Take a look at the first command. It plots the mpg (miles per gallon) against the weight (in thousands of pounds). You don’t have to change anything about this command.
# A scatter plot has been made for you
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
In the second call of ggplot() change the color argument in aes() (which stands for aesthetics). The color should be dependent on the displacement of the car engine, found in disp.
# Replace ___ with the correct column
ggplot(mtcars, aes(x = wt, y = mpg, color = disp)) +
geom_point()
In the third call of ggplot() change the size argument in aes() (which stands for aesthetics). The size should be dependent on the displacement of the car engine, found in disp.
# Replace ___ with the correct column
ggplot(mtcars, aes(x = wt, y = mpg, size = disp)) +
geom_point()
In the previous exercise you saw that disp can be mapped onto a color gradient or onto a continuous size scale.
Another argument of aes() is the shape of the points. There are a finite number of shapes which ggplot() can automatically assign to the points. However, if you try this command in the console to the right:
ggplot(mtcars, aes(x = wt, y = mpg, shape = disp)) +
geom_point()
It gives an error. What does this mean?
Disclaimer: The content of this RMarkdown document came from a course called Data Visualization with ggplot2 - part 01 developed by Datacamp Inc.