Introduction to Data Visualization with ggplot2
Run the hidden code cell below to import the data used in this course.
# Load the Tidyverse
library(tidyverse)
# Load the course datasets
diamonds <- read.csv("datasets/diamonds.csv")
iris <- read.csv("datasets/iris.csv")
iris_tidy <- read.csv("datasets/iris_tidy.csv")
iris_wide <- read.csv("datasets/iris_wide.csv")
recession <- read.csv("datasets/recession.csv")
fish <- read.csv("datasets/fish_species.csv")
fish_tidy <- read.csv("datasets/fish_tidy.csv")
fish_year <- read.csv("datasets/fish_year.csv")
Take Notes
Add a color aesthetic mapped to the displacement of the car engine: inside aes(), add a color argument equal to disp.
ggplot(mtcars, aes(wt, mpg, color = disp)) +
geom_point()
#This time, map disp to the size aesthetic
ggplot(mtcars, aes(wt, mpg, color = disp, size = disp)) +
geom_point()
str(diamonds)
# Map the color aesthetic to clarity
ggplot(diamonds, aes(carat, price, color = clarity)) +
geom_point() +
geom_smooth()
# Map the color aesthetic to clarity
ggplot(diamonds, aes(carat, price, color = clarity)) +
geom_point() +
geom_smooth()
geom_point() has an alpha argument that controls the opacity of the points. A value of 1 (the default) means that the points are totally opaque; a value of 0 means the points are totally transparent (and therefore invisible). Values in between specify transparency.
# Make the points 40% opaque
ggplot(diamonds, aes(carat, price, color = clarity)) +
geom_point(alpha = 0.4) +
geom_smooth()
Using the diamonds dataset, plot the price (y-axis) versus the carat (x-axis), assigning to plt_price_vs_carat. Using geom_point(), add a point layer to plt_price_vs_carat
# Draw a ggplot
plt_price_vs_carat <- ggplot(
# Use the diamonds dataset
diamonds,
# For the aesthetics, map x to carat and y to price
aes(carat, price)
)
# Add a point layer to plt_price_vs_carat
plot + geom_point()
# From previous step
plt_price_vs_carat <- ggplot(diamonds, aes(carat, price))
# Edit this to make points 20% opaque: plt_price_vs_carat_transparent
plt_price_vs_carat_transparent <- plt_price_vs_carat + geom_point(alpha = 0.2)
# See the plot
plt_price_vs_carat_transparent# From previous step
plt_price_vs_carat <- ggplot(diamonds, aes(carat, price))
# Edit this to map color to clarity,
# Assign the updated plot to a new object
plt_price_vs_carat_by_clarity <- plt_price_vs_carat + geom_point(aes(color = clarity))
# See the plot
plt_price_vs_carat_by_clarity
ggplot(mtcars, aes(wt, mpg, color = fcyl)) +
# Set the shape and size of the points
geom_point(shape = 1, size = 4)
ggplot(mtcars, aes(wt, mpg, fill = fcyl)) +
# Change point shape; set alpha
geom_point(alpha = 0.6, shape = 21, size = 4)
Add a text layer, setting the label to the rownames of the dataset mtcars, and the color to "red".
ggplot(mtcars, aes(wt, mpg, color = fcyl)) +
# Add text layer with label rownames(mtcars) and color red
geom_text(label = rownames(mtcars), color = 'red')
Set the x-axis label to "Number of Cylinders", and the y-axis label to "Count" using the x and y arguments of labs(), respectively. Implement a custom fill color scale using scale_fill_manual(). Set the first argument to "Transmission", and values to palette. Modify the code to set the position to dodge so that the bars for transmissions are displayed side by side.
palette <- c(automatic = "#377EB8", manual = "#E41A1C")
# Set the position
ggplot(mtcars, aes(fcyl, fill = fam)) +
geom_bar() +
labs(x = "Number of Cylinders", y = "Count")
scale_fill_manual("Transmission", values = palette)