code,  data science,  recipe

Bar Plot w/ ggplot2

Quick reference for using ggplot2 with R. This package uses data from my covid data pipeline. Please see that post for more details on the data content.

Simple Barplot

library(tidyverse)
load("covid.rdata")

# get latest date so we can plot most recent
latest_date <- max(covid$date)

# simple barplot
covid %>% 
  filter(date == latest_date,
         state != "Puerto Rico") %>%
  ggplot(aes(x = region, y = total_cases)) + 
    geom_bar(stat = "identity")

This will result in a very basic plot that looks like this.

Better Barplot

We need this to look a little better, so here is how you add labels and adjust the scales so the numbers come out in a readable format.

library(tidyverse)
load("covid.rdata")
library(scales)

covid %>% 
  filter(date == latest_date,
         state != "Puerto Rico") %>% 
  ggplot(aes(x = region, y = total_cases)) + 
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = comma) +
  ggtitle("Coronavirus Cases By US Region") + 
  xlab("US Region") +
  ylab("Coronavirus Cases") 

Now we get a nicer looking plot that is ready to publish.

Horizontal Barplot

Now here is a spruced up Horizontal Boxplot.

covid %>% 
  filter(date == latest_date,
         state != "Puerto Rico") %>% 
  ggplot(aes(x = state, y = infection_rate, fill = region)) + 
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = comma) + 
  coord_flip() + 
  ggtitle("Coronavirus Cases By US State") + 
  xlab("US State") + 
  ylab("Coronavirus Infection Rate") + 
  theme_minimal()

And you will get something like this.

Where to Learn More

You can make most graphs with ggplot2 and it works pretty well with tidyverse. Here is where you can learn all the details you need for ggplot2:

ggplot2 Reference

Matt Campbell has over 20 years of experience in technology, psychometrics, and data analytics, including over 10 years working at the College Board and Educational Testing Service (ETS). He has earned a Master’s degree in Information Systems Management and a Bachelor’s degree in Quantitative Psychology. Matt is the published author of four Apress books including Objective-C Recipes, Swift Quick Syntax Reference, Objective-C Quick Reference, and Learn RStudio IDE, Quick, Effective, and Productive Data Science.

Leave a Reply

Your email address will not be published. Required fields are marked *