So many plots
I imagine you're curious to find out how the distributions of some of the other variables in the data look like. Well, why don't we visualize all of them!
You'll also meet another new tidyverse toy, the pipe-operator: %>%
.
The pipe (%>%
) takes the result produced on it's left side and uses it as the first argument in the function on it's right side. Since the first argument of the tidyverse functions is usually data
, this allows for some cool chaining of commands.
We'll look at %>%
more closely in the next exercise. But now, let's draw some plots.
This exercise is part of the course
Helsinki Open Data Science
Exercise instructions
- Access the tidyverse libraries tidyr, dplyr and ggplot2
- Take a glimpse at the
alc
data - Apply the
gather()
function onalc
and then take a glimpse at the resulting data directly after, utilizing the pipe (%>%
). What does gather do? - Draw a plot of each variable in the
alc
data by first gathering the values into key-value pairs and then visualizing them with ggplot. Define the plots as bar plots by adding the elementgeom_bar()
, (using+
).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# alc is available
# access the tidyverse libraries tidyr, dplyr, ggplot2
library(tidyr); library(dplyr); library(ggplot2)
# glimpse at the alc data
# use gather() to gather columns into key-value pairs and then glimpse() at the resulting data
gather(alc) %>% glimpse
# draw a bar plot of each variable
gather(alc) %>% ggplot(aes(value)) + facet_wrap("key", scales = "free")