Exercise 1

Use the “mpg” dataset (built-in) to make a scatterplot of “city mpg” and “highway mpg” (two quantitative variables in the dataset). Change axes labels and give your plot a title. Change the points to dark blue triangles and change size to 6 and opacity to 0.3. Save this plot as an object “ex1”. Finally, export as a jpg using the ggsave() command. (Google this for more info). Turn in your jpg file (ex1.jpg) in the assignments folder on Canvas.

For help:

?mpg
describe(mpg)
?geom_point

Answer:

ex1 <- ggplot(mpg, aes(cty, hwy)) + 
  geom_point(color="darkblue", shape=2, size=6, alpha=0.3) +
  labs(x="City MPG", y="Highway MPG", title="Fuel Economy")
ex1

When you are done…
Why are some triangles darker than others? We can introduce random variation in our data using the ‘jitter’ command to reveal overlapping data points: geom_point(…., position=‘jitter’, …)

Exercise 2

Make a new scatterplot using the same variables, axes labels, and title. This time, use the jitter command described above. Additionally, make these filled triangles with size of 6 and opacity of 0.5. Lastly, make these triangles the same color red as the “Atlanta Falcons”. (Search online for the hex code or rgb value). Save this plot as an object “ex2”. Export as a jpg and turn in to the assignments folder on Canvas.

Answer:

ex2 <- ggplot(mpg, aes(cty, hwy)) +
    geom_point(shape=17, size=6, alpha=.5, 
               color="#A6192D", position='jitter') +
    labs(x="City MPG", y="Highway MPG", title="Fuel Economy")
ex2



Exercise 3

Use the “mtcars” dataset (built-in) to make a scatterplot of “weight” and “mpg”. Change axes labels and give your plot a title. Set the alpha of all points to 0.7.

Set the color of the points to the “mpg” variable (continuous). Also, set this color parameter so that high mpgs are colored green and low mpgs are colored blue. Title the legend “MPG”.

Set the size of the points to the “wt” variable (continuous). Also, to make the small points more readable, set the range of sizes to go from 3-10. Title this legend “Weight”.

When you’re done, save this as an object “ex3”, export as a jpg, and turn in to the assignments folder on Canvas.

For help:

?mtcars
describe(mtcars)
?geom_point

Answer:

ex3 <- ggplot(mtcars, aes(wt, mpg)) + 
  geom_point(alpha=0.7, aes(size=wt, color=mpg)) +
  scale_size(name="Weight", range=c(3,10)) +
  scale_color_gradient(name="MPG", low="blue", high="green") +
  labs(x="Weight in lbs (x 1000)", y="MPG", title="Fuel efficiency")
ex3

Exercise 4

Use the “mpg” dataset to make a scatterplot of “City MPG” and “Highway MPG”. Change axes labels and give your plot a title. Set the alpha of all points to 0.8 and the size of all points to 6. Use the position=‘jitter’ command to spread out your data (see exercise 2).

Because they are numbers, in this dataset “year” and “cyl” are both assumed to be continuous variables. However, we want to graph them as if they are categorical variables. To do this you will write the variables like so: “factor(year)” & “factor(cyl)”.

Set shape to year and color to cylinder. Remember to use the factor notation described above! HINT: aes(shape=factor(variable name), color=factor(variable name))

Set the title of these legends to “Year” and “Cylinders.” When you’re done, save this as an object “ex4”, export as a jpg, and turn in to the assignments folder on Canvas.

Answer:

ex4 <- ggplot(mpg, aes(cty, hwy)) + 
  geom_point(alpha=0.8, size=6, position='jitter', 
             aes(shape=factor(year), color=factor(cyl))) +
  scale_color_discrete(name="Cylinders") +
  scale_shape(name="Year") +
  labs(x="City MPG", y="Highway MPG", title="Fuel efficiency")
ex4



Exercise 5

Using the plot from ex1, add a smoothed trend line. Save this as an object “ex5”, export as a jpg, and turn in to the assignments folder on Canvas.

Answer:

ggplot(mpg, aes(cty, hwy)) + 
  geom_point(color="darkblue", shape=2, size=6, alpha=0.3) +
  labs(x="City MPG", y="Highway MPG", title="Fuel Economy") +
  geom_smooth()
## `geom_smooth()` using method = 'loess'

# Or ex1 + geom_smooth()


Exercise 6

Using the plot from ex2, facet wrap by the variable “cyl”. Save this as an object “ex6”, export as a jpg, and turn in to the assignments folder on Canvas.

Answer:

ggplot(mpg, aes(cty, hwy)) +
  geom_point(shape=17, size=6, alpha=.5, 
             color="#A6192D", position='jitter') +
  labs(x="City MPG", y="Highway MPG", title="Fuel Economy") +
  facet_wrap(~cyl)

# Or ex2 + facet_wrap(~cyl)

Exercise 7

Using the plot from ex3, label each point by their 1/4 mile time (qsec), if their time is less than 16 seconds. Save this as an object “ex7”, export as a jpg, and turn in to the assignments folder on Canvas.

Answer:

ggplot(mtcars, aes(wt, mpg)) + 
  geom_point(alpha=0.7, aes(size=wt, color=mpg)) +
  scale_size(name="Weight", range=c(3,10)) +
  scale_color_gradient(name="MPG", low="blue", high="green") +
  labs(x="Weight in lbs (x 1000)", y="MPG", title="Fuel efficiency") +
  geom_text(aes(label=ifelse(qsec<16, qsec, '')))

# Or ex3 + geom_text(aes(label=ifelse(qsec<16, qsec, '')))

Exercise 8

Using the plot from ex4, make the titles size 15 and in boldface. Save this as an object “ex8”, export as a jpg, and turn in to the assignments folder on Canvas.

Answer:

ggplot(mpg, aes(cty, hwy)) + 
  geom_point(alpha=0.8, size=6, position='jitter', 
             aes(shape=factor(year), color=factor(cyl))) +
  scale_color_discrete(name="Cylinders") +
  scale_shape(name="Year") +
  labs(x="City MPG", y="Highway MPG", title="Fuel efficiency") +
  theme(title=element_text(size=15, face="bold"))

# Or ex4 + theme(title=element_text(size=15, face="bold"))