.html file as: YourName_512-91- O-2018.html and upload it to the “In-class Visualization Coding Exercise #3” assignment in Week #5 on Moodle.In class, we described nine visible aesthetics. Let’s apply them to a categorical variable - the cylinders in mtcars, cyl. These are the aesthetics you can consider within aes() for this part of the course x, y, color, fill, size, alpha, labels and shape. In the following exercises, so that we can consider the cyl column as categorical (factor) variable, wrap the factor() function around cyl.
Map mpg onto the x aesthetic, and cyl onto the y.
Reverse the mappings of the plot you constructed in part a.
Map wt onto x, mpg onto y, and cyl onto color.
Modify the previous plot by changing the shape argument of the geom to 1 and increase the size to 4. These are attributes that you should specify inside geom_point().
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
#part a
#Map mpg to x and cyl to y
#Replace ___ with the correct code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
#part b
#Reverse: Map cyl to x and mpg to y
#Replace ___ with the correct code
ggplot(mtcars, aes(x = cyl, y = mpg)) +
geom_point()
#part c
#Map wt to x, mpg to y and cyl to col
#Replace ___ with the correct code
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point()
#part d
#Change shape and size of the points in the above plot
#Replace ___ with the correct code
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl))+
geom_point(shape = 1, size = 4)
In class, you saw that you can use all the aesthetics as attributes. Let’s see how this works with the aesthetics you used in the previous exercises: x, y, color, fill, size, alpha, label and shape.
This time you’ll use these arguments to set attributes of the plot, not aesthetics. However, there are some pitfalls you’ll have to watch out for: these attributes can overwrite the aesthetics of your plot!
A word about shapes: In the exercise “All about aesthetics, part 2”, you saw that shape = 21 results in a point that has a fill and an outline. Shapes in R can have a value from 1-25. Shapes 1-20 can only accept a color aesthetic, but shapes 21-25 have both a color and a fill aesthetic. See the pch argument in par() for further discussion.
A word about hexadecimal colours: Hexadecimal, literally “related to 16”, is a base-16 alphanumeric counting system. Individual values come from the ranges 0-9 and A-F. This means there are 256 possible two-digit values (i.e. 00 - FF). Hexadecimal colours use this system to specify a six-digit code for Red, Green and Blue values (“#RRGGBB”) of a colour (i.e. Pure blue: “#0000FF”, black: “#000000”, white: “#FFFFFF”). R can accept hex codes as valid colours.
Overwrite the color of the points inside geom_point() to my_color. Notice how this cancels out the colors given to the points by the number of cylinders!
Starting with plot 2, map cyl to fill instead of col and set the attributes size to 10, shape to 23 and color to my_color inside geom_point().
library(ggplot2)
# Define a hexadecimal color
my_color <- "#4ABEFF"
#part a
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point()
#part b
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point(color =my_color)
#part c
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point( size = 10, shape = 23, color = my_color)
You saw how jittering worked in the video, but bar plots suffer from their own issues of overplotting, as you’ll see here. Use the “stack”, “fill” and “dodge” positions to reproduce the plot in the viewer.
The ggplot2 base layers (data and aesthetics) have already been coded; they’re stored in a variable cyl.am. It looks like this:
library(ggplot2)
cyl.am <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(am)))
Add a geom_bar() call to cyl.am. By default, the position will be set to “stack”.
Fill in the second ggplot command. Explicitly set position to “fill” inside geom_bar().
Fill in the third ggplot command. Set position to “dodge”.
The position = “dodge” version seems most appropriate. Finish off the fourth ggplot command by completing the three scale_ functions:
Next, values and labels are set to predefined values for you. These are the colors and the labels in the legend.
library(ggplot2)
cyl.am <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(am)))
#Replace _____ with the correct code
#part a
# The base layer, cyl.am, is available for you
# Add geom (position = "stack" by default)
cyl.am + geom_bar(position = "stack")
#part b
# Fill - show proportion
cyl.am +
geom_bar(position = "fill")
#part c
# Dodging - principles of similarity and proximity
cyl.am +
geom_bar(position = "dodge")
#part d
# Clean up the axes with scale_ functions
val = c("#E41A1C", "#377EB8")
lab = c("Manual", "Automatic")
cyl.am +
geom_bar(position = "dodge") +
scale_x_discrete("Cylinders" ) +
scale_y_continuous("Number" ) +
scale_fill_manual("Transmission",
values = val,
labels = lab)