.html file as: YourName_512-91- O-2018.html and upload it to the “Visualization Coding Exercise #4 (Homework #4)” assignment in Week #5 on Moodle.The color aesthetic typically changes the outside outline of an object and the fill aesthetic is typically the inside shading. However, as you saw in the last exercise, geom_point() is an exception. Here you use color, instead of fill for the inside of the point. But it is a little more subtle than that.
Which shape to use? The default geom_point() uses shape = 19 (a solid circle with an outline the same colour as the inside). Good alternatives are shape = 1 (hollow) and shape = 16 (solid, no outline). These all use the col aesthetic (don’t forget to set alpha for solid points).
A really nice alternative is shape = 21 which allows you to use both fill for the inside and col for the outline! This is a great little trick for when you want to map two aesthetics to a dot.
What happens when you use the wrong aesthetic mapping? This is a very common mistake! The code from the previous exercise is in the editor. Using this as your starting point complete the instructions.
Copy and paste the first plot’s code that we created in the section All About Aesthetics Part 1 in the In-Class Visualization Coding Exercise #3. Change the aesthetics so that cyl maps to fill rather than col.
Copy and paste the plot’s code from part a. In geom_point() change the shape argument to 21 and add an alpha argument set to 0.6.
Copy and paste the plot’s code from part b. In the ggplot() aesthetics, map am to col.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
# am and cyl are factors, wt is numeric
class(mtcars$am)
## [1] "numeric"
class(mtcars$cyl)
## [1] "numeric"
class(mtcars$wt)
## [1] "numeric"
# plot created from the previous in-class exercise
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point(shape = 1, size = 4)
#part a
ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_point(shape = 1, size = 4)
#part b
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point(shape = 21, size = 4, alpha = 0.6)
#part c
ggplot(mtcars, aes(x = wt, y = mpg, col = am)) +
geom_point(shape = 21, size = 4, alpha = 0.6)
Use ggplot() to create a basic scatter plot. Inside aes(), map wt onto x and mpg onto y. Typically, you would say “mpg described by wt” or “mpg vs wt”, but in aes(), it’s x first, y second. Use geom_point() to make three scatter plots:
-cyl on size -cyl on alpha -cyl on shape
Try this last variant: -cyl on label. In order to correctly show the test (for example, label), use geom_text().
library(ggplot2)
# Map cyl to size
ggplot(mtcars, aes(wt, mpg, size = cyl))+ geom_point()
# Map cyl to alpha
ggplot(mtcars, aes(wt, mpg, alpha = cyl))+ geom_point()
# Map cyl to shape
mtcars$cyl = as.factor(mtcars$cyl)
ggplot(mtcars, aes(wt, mpg, shape = cyl))+ geom_point()
# Map cyl to labels
ggplot(mtcars, aes(wt, mpg, label = cyl))+ geom_text()
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.
In this exercise you will set all kinds of attributes of the points!
We will continue to work with the mtcars data frame.
Add to the ggplot code: draw points with alpha set to 0.5.
Add to the ggplot code: draw points of shape 24 in the color yellow.
Add to the ggplot code: draw text with label rownames(mtcars) in the color red. Don’t use geom_point() here! You should get a scatter plot with the names of the cars instead of point
library(ggplot2)
#part a
# Expand to draw points with alpha 0.5
ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_point(alpha = 0.5, size = 4)
#part b
# Expand to draw points with shape 24 and color yellow
ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_point(shape = 24, color = 'yellow', size = 4)
#part c
# Expand to draw text with label rownames(mtcars) and color red
ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_text(label = rownames(mtcars), color = 'red')
In this exercise, you will gradually add more aesthetics layers to the plot. You’re still working with the mtcars dataset, but this time you’re using more features of the cars. For completeness, here is a list of all the features of the observations in mtcars:
mpg – Miles/(US) gallon cyl – Number of cylinders disp – Displacement (cu.in.) hp – Gross horsepower drat – Rear axle ratio wt – Weight (lb/1000) qsec – 1/4 mile time vs – V/S engine. am – Transmission (0 = automatic, 1 = manual) gear – Number of forward gears carb – Number of carburetors
Notice that adding more aesthetics to your plot is not always a good idea. Adding aesthetic mappings to a plot will increase its complexity, and thus decrease its readability.
In class, you saw and learned about aesthetics and attributes. Variables in a data frame are mapped to aesthetics in aes(). (e.g. aes(col = cyl)) within ggplot(). Visual elements are set by attributes in specific geom layers (geom_point(col = “red”)). Don’t confuse these two things - here you’re focusing on aesthetic mappings.
Draw a scatter plot of mtcars with mpg on the x-axis, qsec on the y-axis and factor(cyl) as colors.
Expand the plot in part a to include factor(am) as the shape of the points.
Expand the to include plot in part b to include the ratio of horsepower to weight (i.e. (hp/wt)) as the size of the points.
library(ggplot2)
#part a
ggplot(mtcars, aes(mpg, qsec, col = factor(cyl))) +
geom_point()
#part b
ggplot(mtcars,
aes(mpg, qsec,
col = factor(cyl),
shape = factor(am)
)) +
geom_point()
#part c
ggplot(mtcars,
aes(mpg, qsec,
col = factor(cyl),
shape = factor(am),
size = (hp/wt)
)) +
geom_point()
Type one and only one letter as your answer.
In class, you saw that all the visible aesthetics can serve as attributes and aesthetics, but I very conveniently left out x and y. That’s because although you can make univariate plots (such as histograms, which you will get to in week #7 of the course), a y-axis will always be provided, even if you did not ask for it.
In the base package you can make univariate plots with stripchart() (shown in the viewer) directly and it will take care of a fake y axis for us. Since this is univariate data, there is no real y axis.
You can get the same thing in ggplot2, but it’s a bit more cumbersome. The only reason you’d really want to do this is if you were making many plots and you wanted them to be in the same style, or you wanted to take advantage of an aesthetic mapping (for example, color).
library(ggplot2)
#ggplot(mtcars, aes(x = mpg)) + geom_point()
x is only one of the two essential aesthetics for geom_point(), which is why you get an error message.
library(ggplot2)
# part b
# Create jittered plot of mtcars, mpg onto x, 0 onto y
#Fill in ____ with the correct code
ggplot(mtcars, aes(x = mpg, y = 0)) +
geom_jitter()
library(ggplot2)
#part c
ggplot(mtcars, aes(x = mpg, y = 0)) +
geom_jitter() +
scale_y_continuous(limits = c(-2,2))