library(tidyverse)
library(palmerpenguins)
# Your R Code here
1 Exercises (Chapter 1)
How many rows are in
penguins
? How many columns?What does the
bill_depth_mm
variable in thepenguins
data frame describe? Read the help for?penguins
to find out.Make a scatterplot of
bill_depth_mm
vs.bill_length_mm
. That is, make a scatterplot withbill_depth_mm
on the y-axis andbill_length_mm
on the x-axis. Describe the relationship between these two variables.What happens if you make a scatterplot of
species
vs.bill_depth_mm
? What might be a better choice of geom?Why does the following give an error and how would you fix it?
library(tidyverse) ggplot(data = penguins) + geom_point()
What does the
na.rm
argument do ingeom_point()
? What is the default value of the argument? Create a scatterplot where you successfully use this argument set toTRUE
.Add the following caption to the plot you made in the previous exercise: “Data come from the
palmerpenguins
package.” Hint: Take a look at the documentation forlabs()
.Recreate the following visualization. What aesthetic should
bill_depth_mm
be mapped to? And should it be mapped at the global level or at the geom level?Run this code in your head and predict what the output will look like. Then, run the code in R and check your predictions.
ggplot( data = penguins, mapping = aes(x = flipper_length_mm, y = body_mass_g, color = island) + ) geom_point() + geom_smooth(se = FALSE)
Will these two graphs look different? Why/why not?
ggplot( data = penguins, mapping = aes(x = flipper_length_mm, y = body_mass_g) + ) geom_point() + geom_smooth() ggplot() + geom_point( data = penguins, mapping = aes(x = flipper_length_mm, y = body_mass_g) + ) geom_smooth( data = penguins, mapping = aes(x = flipper_length_mm, y = body_mass_g) )
Make a bar plot of
species
ofpenguins
, where you assignspecies
to they
aesthetic. How is this plot different?How are the following two plots different? Which aesthetic,
color
orfill
, is more useful for changing the color of bars?ggplot(penguins, aes(x = species)) + geom_bar(color = "red") ggplot(penguins, aes(x = species)) + geom_bar(fill = "red")
What does the
bins
argument ingeom_histogram()
do?Make a histogram of the
carat
variable in thediamonds
dataset that is available when you load thetidyverse
package. Experiment with different binwidths. What binwidth reveals the most interesting patterns?The
mpg
data frame that is bundled with theggplot2
package contains 234 observations collected by the US Environmental Protection Agency on 38 car models. Which variables inmpg
are categorical? Which variables are numerical? (Hint: Type?mpg
to read the documentation for the dataset.) How can you see this information when you runmpg
?Make a scatterplot of
hwy
vs.displ
using thempg
data frame. Next, map a third, numerical variable tocolor
, thensize
, then bothcolor
andsize
, thenshape
. How do these aesthetics behave differently for categorical vs. numerical variables?In the scatterplot of
hwy
vs.displ
, what happens if you map a third variable tolinewidth
?What happens if you map the same variable to multiple aesthetics?
Make a scatterplot of
bill_depth_mm
vs.bill_length_mm
and color the points byspecies
. What does adding coloring by species reveal about the relationship between these two variables? What about faceting byspecies
?Why does the following yield two separate legends? How would you fix it to combine the two legends?
ggplot( data = penguins, mapping = aes( x = bill_length_mm, y = bill_depth_mm, color = species, shape = species )+ ) geom_point() + labs(color = "Species")
Create the two following stacked bar plots. Which question can you answer with the first one? Which question can you answer with the second one?
ggplot(penguins, aes(x = island, fill = species)) + geom_bar(position = "fill")
ggplot(penguins, aes(x = species, fill = island)) + geom_bar(position = "fill")
Run the following lines of code. Which of the two plots is saved as
mpg-plot.png
? Why?ggplot(mpg, aes(x = class)) + geom_bar() ggplot(mpg, aes(x = cty, y = hwy)) + geom_point() ggsave("mpg-plot.png")
What do you need to change in the code above to save the plot as a PDF instead of a PNG? How could you find out what types of image files would work in
ggsave()
?