ggplot: Answers

Warning

Make sure that you try the exercises yourself first before looking at the answers

Explore the data set using dim(), str() and help(), which variables are continuous, which variables are discrete? Is this data set ready for plotting with ggplot?

Explore the data set using dim(), str() and help(), which variables are continuous, which variables are discrete? Is this data set ready for plotting with ggplot?

dim(diamonds)
str(diamonds)
help(diamonds)

Use ggplot to plot a scatterplot of the relationship between the diamonds’ carat and their price

Use ggplot to plot a scatterplot of the relationship between the diamonds’ carat and their price

ggplot() + geom_point(data=diamonds, aes(x=carat, y=price))

#or

ggplot(diamonds) + geom_point(aes(x=carat, y=price))

#or

ggplot(diamonds, aes(x=carat, y=price)) + geom_point()

Cut, color, and clarity are factors, and therefore discrete. The others are numeric continuous variables.

Make all dots darkblue and set the alpha value to 0.1

Make all dots darkblue and set the alpha value to 0.1

ggplot(diamonds, aes(x=carat, y=price)) + geom_point(color="darkblue", alpha=0.1)

Visualize the influence of the color of a diamond on its price by mapping the diamond color to the color aesthetic

Visualize the influence of the color of a diamond on its price by mapping the diamond color to the color aesthetic

#The color of the dots will be overwritten if we specify it statically  
#in the geom_point function itself
ggplot(diamonds, aes(x=carat, y=price, color=color)) + geom_point(alpha=0.1)

Use a ggplot barplot to visualize diamond clarity depending on color, map diamond color to x and diamond clarity to fill

Use a ggplot barplot to visualize diamond clarity depending on color, map diamond color to x and diamond clarity to fill

ggplot(diamonds, aes(x=color, fill=clarity)) + geom_bar()

Create a boxplot of the carat of a diamond based on its clarity and add whiskers using stat_boxplot

Create a boxplot of the carat of a diamond based on its clarity and add whiskers using stat_boxplot

ggplot(diamonds, aes(x=clarity, y=carat)) + 
    stat_boxplot(geom="errorbar", width=0.5) + 
    geom_boxplot()

Add a geom_point layer to the previous plot mapping the diamonds price to the color

Add a geom_point layer to the previous plot mapping the diamonds price to the color

ggplot(diamonds, aes(x=clarity, y=carat)) + 
    stat_boxplot(geom="errorbar", width=0.5) + 
    geom_boxplot() + 
    geom_point(aes(color=price))

Create a histogram of the price of the diamonds and separate the histograms into facets using diamond color, choose a good binwith or number of bins

Create a histogram of the price of the diamonds and separate the histograms into facets using diamond color, choose a good binwith or number of bins

ggplot(diamonds, aes(x=price)) + 
    geom_histogram(binwidth = 100) + 
    facet_grid(color ~ .)

Create a grid of facets of the same histogram by comparing both color and cut

Create a grid of facets of the same histogram by comparing both color and cut

ggplot(diamonds, aes(x=price)) + 
    geom_histogram(binwidth = 100) + 
    facet_grid(color ~ cut)

Use aggregate(diamonds, by = list(cut = diamonds$cut, color = diamonds$color), mean) to calculate the mean of all variables by cut and color. Create a heatmap of the mean prices by cut and color using geom_tile

Use aggregate(diamonds, by = list(cut = diamonds$cut, color = diamonds$color), mean) to calculate the mean of all variables by cut and color. Create a heatmap of the mean prices by cut and color using geom_tile

#Aggregate uses a function (in this case mean) to aggregate all variables 
#in a given data.frame by a list of variables given in "by"
mean.price <- aggregate(diamonds, by = list(cut = diamonds$cut, color = diamonds$color), mean)

ggplot(mean.price, aes(x=cut, y=color, fill=price)) + 
    geom_tile()

Change the title of the heatmap to “Average prices”

Change the title of the heatmap to “Average prices”

ggplot(mean.price, aes(x=cut, y=color, fill=price)) + 
    geom_tile() + 
    labs(title="Average prices")

Change the gradient of the fill scale using ‘scale_fill_gradient2’. Have it go from darkblue to white to darkred, set the midpoint to 4500

Change the gradient of the fill scale using ‘scale_fill_gradient2’. Have it go from darkblue to white to darkred, set the midpoint to 4500

ggplot(mean.price, aes(x=cut, y=color, fill=price)) + 
    geom_tile() + 
    labs(title="Average prices") + 
    scale_fill_gradient2(low="darkblue", mid="white", high="darkred", midpoint = 4500)

Choose and add a theme to the heatmap, or create a theme yourself using the options listed at http://ggplot2.tidyverse.org/reference/theme.html

Choose and add a theme to the heatmap, or create a theme yourself using the options listed at http://ggplot2.tidyverse.org/reference/theme.html

ggplot(mean.price, aes(x=cut, y=color, fill=price)) + 
    geom_tile() + 
    labs(title="Average prices") + 
    scale_fill_gradient2(low="darkblue", mid="white", high="darkred", midpoint = 4500) + 
    theme_minimal()