Graphs in purrr

1. Graphs in purrr

Whether you are making graphs to examine your data, or for your next big presentation, being able to go from a list to a graph with ease is powerful. Let's dive in.

2. ggplot() refresher

You should have already learned about ggplot2 in a previous course, but let's review quickly. Each ggplot() graph requires a dataframe as input, so we cannot input a list. We always start a plot with the function ggplot(), and we can then add layers onto it, with a plus sign. To make a scatterplot, we start with the ggplot() function and then we add a plus sign after the ggplot() call and add on the geom function. In this case, we will use geom_point() since we want our data to be displayed as points.

3. Graphing and purrr

Since ggplot() requires a dataframe input, but our data is often stored in a list, we can use map_df() to transform the data from a list, into the dataframe we need. We can then pipe that dataframe directly into the ggplot() code; no need for intermediate objects. Here we will use our bird_measurements list, and pipe it into the map_df() function, where we pull out the wing_length and weight elements into their own columns by putting them in the data_frame() function. Then we pipe the output from map_df() into our ggplot() graph, and we can create a scatter plot where the x-axis is weight and the y-axis is wing_length. We will use geom_point() so that the data are graphed as points.

4. Let's purrr-actice!

Now it's your turn.