Created
May 15, 2017 17:31
-
-
Save aleszu/f909f4386557c30cc56d0305ce2555f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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