Get startedGet started for free

Visualize your results using the tidyverse

1. Visulize your results using Tidyverse

Let's use tidyr, dplyr, and ggplot2 to visualize the number of male and female mortgage borrowers by year.

2. Missingness by Year

Since functions in the bigmemory package take data as the first argument we can use the pipe operator to combine various steps when performing data analysis. The pipe operator takes the mortgage data set and passes it as the first argument to bigtable, tabulating on borrower_gender and year. The result is a table which is then converted into a data frame.

3. Missingness by Year

Now we can use the mutate() function from the dplyr package to create a new column in the data frame. The first argument is the data frame which is forwarded by the pipe operator, and the second argument is the column we want to create, called Category. We assign the new Category column to a character vector with strings indicating the different gender categories. At this point we have a short-formatted data frame.

4. Missingness by Year

To plot it with ggplot() we'll convert it to the long format using the tidyr package. The first argument is also a data frame, which is again forwarded by the pipe operator. Then we specify the names of the columns we want in the long data frame, Year and Count. To gather on all of the columns except Category, we use -Category. The columns of our new, long-formatted data frame will be the year-wise count for each category.

5. Missingness by Year

Finally, we will use the ggplot2 package to visualize the number of male and female borrowers per year. First, we set up the plot by piping the data frame into ggplot() and mapping the x-axis to Year, y-axis to Count, and then group and color by Category. To draw a line chart we will add a call to geom_line(). Note that we use the plus operator to add layers to ggplot() and not the pipe!

6. Plot

From the plot you can see that there are more male borrowers than female borrowers for every year.

7. Let's practice!

It's your turn to write some tidy code!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.