Skip to content

Instantly share code, notes, and snippets.

@aleszu
Created May 15, 2017 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleszu/f909f4386557c30cc56d0305ce2555f4 to your computer and use it in GitHub Desktop.
Save aleszu/f909f4386557c30cc56d0305ce2555f4 to your computer and use it in GitHub Desktop.
# How to make small multiples in R using geom_line()
library(ggplot2)
# Load the csv from previous tutorial
murders_separate <- read.csv(file="murders_separate.csv")
# View the dataset in RStudio (note the capital V)
View(murders_separate)
# Plot all cities to see change in murders between 2014 and 2015
ggplot(murders_separate, aes(x=year, y=murders, group=city)) +
geom_line()+
scale_x_continuous(breaks=c(2014,2015))
# Plot all cities and include legend
ggplot(murders_separate, aes(x=year, y=murders, group=city, color=city)) +
geom_line()+
scale_x_continuous(breaks=c(2014,2015))
# Plot cities with murders over 150 and include legend
ggplot(subset(murders_separate, murders > 150), aes(x=year, y=murders, group=city, color=city)) +
geom_line()+
scale_x_continuous(breaks=c(2014,2015))
# Stack small multiples with cities with murders over 200
ggplot(subset(murders_separate, murders > 200), aes(x=year, y=murders, group=city, color=city)) +
geom_line()+
facet_grid(city ~ .)+
scale_x_continuous(breaks=c(2014,2015))
# Create small multiples with cities with murders over 200
ggplot(subset(murders_separate, murders > 200), aes(x=year, y=murders, group=city, color=city)) +
geom_line()+
facet_wrap(~ city)+
scale_x_continuous(breaks=c(2014,2015))+
theme(axis.ticks = element_blank(),
axis.text.x = element_blank(),
legend.position = "none"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment