Combining Data Wrangling and Plotting with ggplot2
Let's combine data wrangling with plotting.
We'll cover the following
Now that we’ve learned how to calculate our means and standard errors, let’s explore how to directly pipe that to a plotting function. Remember, everything in the tidyverse is designed to work together. Earlier, we made an object called RxP.summary
, which contained the means and standard errors for Age.DPO
. Why not just pipe this object directly to ggplot2? Since we’re piping the summarized object, we have to keep in mind the actual input to the plotting function. One more thing to keep in mind is that even though we pipe the object into the ggplot()
function, we still need to provide a data=
argument for it. For ggplot()
to know that the data object is receiving via the pipe function, we specify data=.
, just like we did before with the do()
function.
Let’s make a professional-looking bar graph!
Many people don’t like bar graphs, but they’re still quite common. Therefore, it’s helpful to go through how we make one in ggplot2. We’ll explain what all of the following code means line by line, so don’t worry! The first half of this code is precisely what we did earlier in the chapter to calculate our data’s means and standard errors, and the second half is the code for plotting.
Get hands-on with 1400+ tech skills courses.