4.5 Exercises
Draw a boxplot of
hwy
for each value ofcyl
, without turningcyl
into a factor. What extra aesthetic do you need to set?Modify the following plot so that you get one boxplot per integer value of
displ
.ggplot(mpg, aes(displ, cty)) + geom_boxplot()
When illustrating the difference between mapping continuous and discrete colours to a line, the discrete example needed
aes(group = 1)
. Why? What happens if that is omitted? What’s the difference betweenaes(group = 1)
andaes(group = 2)
? Why?How many bars are in each of the following plots?
ggplot(mpg, aes(drv)) + geom_bar() ggplot(mpg, aes(drv, fill = hwy, group = hwy)) + geom_bar() library(dplyr) mpg %>% arrange(hwy) %>% mutate(id = seq_along(hwy)) mpg2 <-ggplot(mpg2, aes(drv, fill = hwy, group = id)) + geom_bar()
(Hint: try adding an outline around each bar with
colour = "white"
)Install the babynames package. It contains data about the popularity of babynames in the US. Run the following code and fix the resulting graph. Why does this graph make me unhappy?
library(babynames) dplyr::filter(babynames, name == "Hadley") hadley <-ggplot(hadley, aes(year, n)) + geom_line()