Created
November 1, 2017 15:36
-
-
Save CSJCampbell/d92899b54aa02156db09965c185993fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#------------------------------------------------------------------------------# | |
# Working with scales, guides and themes | |
# Mango Solutions - EARL Workshop | |
#------------------------------------------------------------------------------# | |
library(tidyverse) | |
library(forcats) | |
funded <- read_csv("http://bit.ly/UKFundedSports0016") | |
basicPlot <- ggplot(data = mutate(funded, | |
SportGroup = fct_reorder(SportGroup, -Funding), | |
Location = fct_reorder(Location, Year))) + | |
geom_bar(aes( | |
x = Location, | |
y = Funding, | |
fill = SportGroup), | |
stat = "summary", | |
fun.y = "sum") | |
basicPlot | |
#------------------------------------------------------------------------------# | |
# Setting Labels | |
#------------------------------------------------------------------------------# | |
labelPlot <- basicPlot + | |
labs( | |
title = "Changes in Funding For Olympic Sports", | |
subtitle = "Funding Provided by UK Sport World Class Performance Programme", | |
x = "", y = "", | |
caption = "Data taken from uksport.gov.uk", | |
fill = "Event") | |
labelPlot | |
#------------------------------------------------------------------------------# | |
# Exercise | |
#------------------------------------------------------------------------------# | |
# 1. Create a simple line plot | |
# showing the funding over time | |
# with lines coloured by sport | |
# 2. Ensure the labels are added/updated | |
# where appropriate | |
#------------------------------------------------------------------------------# | |
# Defining Scales | |
#------------------------------------------------------------------------------# | |
labels <- c( | |
"Sydney\n(2000)", "Athens\n(2004)", | |
"Beijing\n(2008)", "London\n(2012)", | |
"Rio de Janeiro\n(2016)") | |
axisPlot <- labelPlot + | |
scale_y_continuous( | |
breaks = seq(0, 25e7, by = 5e7), | |
minor_breaks = NULL, | |
labels = scales::dollar_format("£")) + | |
scale_x_discrete(labels = labels) | |
axisPlot | |
cols <- c( | |
"grey", "darkRed", "orangeRed", | |
"steelBlue", "royalBlue", "navyBlue") | |
fillPlot <- axisPlot + | |
scale_fill_manual(values = cols) | |
fillPlot | |
#------------------------------------------------------------------------------# | |
# Exercise | |
#------------------------------------------------------------------------------# | |
# 1. Update the x-axis to show Olympic | |
# years in Month-Year format | |
# (e.g. Sep 2000) | |
# 2. Update the y-axis to have the "£" | |
# symbol in front of the funding values | |
#------------------------------------------------------------------------------# | |
# Updating Legends | |
#------------------------------------------------------------------------------# | |
legendPlot <- fillPlot + | |
theme( | |
legend.position = "bottom", | |
legend.justification = "left") | |
legendPlot | |
guidePlot <- legendPlot + | |
guides( | |
fill = guide_legend( | |
title.position = "top", | |
nrow = 1, | |
label.position = "bottom")) | |
guidePlot | |
#------------------------------------------------------------------------------# | |
# Exercise | |
#------------------------------------------------------------------------------# | |
# 1. Move the legend underneath the | |
# graphic, select an appropriate | |
# alignment for the legend | |
# 2. Override the aes values in the | |
# guide so that the size of the line | |
# is 4 (hint: override.aes) | |
# 3. Adjust the title position and number | |
# of columns based on your legend alignment | |
#------------------------------------------------------------------------------# | |
# Controlling the Theme | |
#------------------------------------------------------------------------------# | |
# search in ggplot2 for themes | |
objects("package:ggplot2", pattern = "theme_") | |
# collect current ggplot theme | |
theme_now <- theme_get() | |
# theme object containing theme information | |
class(theme_now) | |
length(theme_now) | |
head(names(theme_now)) | |
theme_now["line"] | |
# all further plots have theme specified | |
theme_set(theme_classic()) | |
guidePlot | |
# update theme | |
theme_update(axis.text.y = element_text(angle = 45), | |
panel.grid.major.y = element_line(colour = "lightgrey")) | |
guidePlot | |
theme_set(theme_now) | |
guidePlot | |
# add themes to plots | |
guidePlot + theme_bw() | |
# later layers override previous layers | |
guidePlot + theme_bw() + | |
theme(legend.position = "bottom") | |
# theme object | |
theme_new <- theme_classic() + | |
theme( | |
legend.position = "bottom", | |
legend.justification = "left") | |
guidePlot + theme_new | |
#------------------------------------------------------------------------------# | |
# Exercise | |
#------------------------------------------------------------------------------# | |
# 1. Update the theme to include major grid lines | |
# that are dashed | |
# 2. Centre the title and subtitle | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment