Finally got a chance to sit down with the data set this weekend. I added a factor column to practice faceting.
Below is the code and output showing 2015-16 tuition by four regions, shown in descending order:
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(RColorBrewer)
library(knitr)
#I added two factor columns prior to import to practice faceting
usTuition <- read_csv("us_avg_tuition-x.csv")
## Warning: Missing column names filled in: 'X16' [16]
## Parsed with column specification:
## cols(
## State = col_character(),
## `2004-05` = col_double(),
## `2005-06` = col_double(),
## `2006-07` = col_double(),
## `2007-08` = col_double(),
## `2008-09` = col_double(),
## `2009-10` = col_double(),
## `2010-11` = col_double(),
## `2011-12` = col_double(),
## `2012-13` = col_double(),
## `2013-14` = col_double(),
## `2014-15` = col_double(),
## `2015-16` = col_double(),
## Conf = col_character(),
## Union = col_integer(),
## X16 = col_character()
## )
#import brought in an empty, unnamed column at the end; remove this
usTuition <- select(usTuition, everything(), -X16)
#calculate the percentage change in tuition between first and last years
usTuition <- mutate(usTuition, PctChange = ((`2015-16` - `2004-05`) / `2004-05`))
#Load the fonts I use
quartzFonts(ANXTC = c("Avenir Next Condensed Regular", "Avenir Next Condensed Demi Bold", "Avenir Next Condensed Italic", "Avenir Next Condensed Demi Bold Italic"))
usTuition$Conf <- as.factor(usTuition$Conf)
#Plot design variables
myTitle <- "U.S. College Tuition by state, 2015-16"
mySubtitle <- ""
myXLabel <- "State"
myYLabel <- "Tuition"
myCaption <- "Data source: R4DS Tidy Tuesday | Guy Spurrier"
#make a plot showing highest tuitions by four custom regions I created
ggplot(usTuition) +
theme_minimal() +
geom_col(aes(reorder(State, `2015-16`), `2015-16`, fill = Conf)) +
coord_flip() +
facet_wrap(facets = "Conf", nrow = 2, ncol = 2, scales = "free_y") +
scale_fill_brewer(palette = "Set2", guide = FALSE) +
theme(text = element_text(family = "ANXTC"),
plot.title = element_text(size = 16, face = "bold"),
plot.subtitle = element_text(size = 13, face = "plain"),
plot.caption = element_text(size = 12, face = "plain"),
axis.title.x = element_text(size = 13, face = "plain", margin = margin(t = 10, b = 10)),
axis.title.y = element_text(size = 13, face = "plain", margin = margin(r = 10)),
axis.text.x = element_text(size = 12, face = "plain"),
axis.text.y = element_text(size = 12, face = "plain"),
strip.text = element_text(size = 14, face = "bold")
) +
labs(title = myTitle, subtitle = mySubtitle, x = myXLabel, y = myYLabel, caption = myCaption)
#Plot design variables
myTitle <- "Change in U.S. College Tuition by state from 2004-05 to 2015-16"
mySubtitle <- ""
myXLabel <- "State"
myYLabel <- "Tuition"
myCaption <- "Data source: R4DS Tidy Tuesday | Guy Spurrier"
#make a plot showing change in tuition between 2004-05 and 2015-16 by four custom regions
ggplot(usTuition) +
theme_minimal() +
geom_col(aes(reorder(State, PctChange), PctChange, fill = Conf)) +
coord_flip() +
facet_wrap(facets = "Conf", nrow = 2, ncol = 2, scales = "free_y") +
scale_fill_brewer(palette = "Set2", guide = FALSE) +
theme(text = element_text(family = "ANXTC"),
plot.title = element_text(size = 16, face = "bold"),
plot.subtitle = element_text(size = 13, face = "plain"),
plot.caption = element_text(size = 12, face = "plain"),
axis.title.x = element_text(size = 13, face = "plain", margin = margin(t = 10, b = 10)),
axis.title.y = element_text(size = 13, face = "plain", margin = margin(r = 10)),
axis.text.x = element_text(size = 12, face = "plain"),
axis.text.y = element_text(size = 12, face = "plain"),
strip.text = element_text(size = 14, face = "bold")
) +
labs(title = myTitle, subtitle = mySubtitle, x = myXLabel, y = myYLabel, caption = myCaption)