Skip to content

Instantly share code, notes, and snippets.

@LCHansson
Last active March 9, 2017 06:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LCHansson/4154827 to your computer and use it in GitHub Desktop.
Save LCHansson/4154827 to your computer and use it in GitHub Desktop.
ggplot format snippets
##############################################################################
## ##
## FORMATSNIPPETS.R ##
## ggplot format snippets ##
## ##
##############################################################################
#### LABELS ####
# Hide labels
labs(x="", y="")
# Custom labels
labs(x="År", y="Utbildningskategori (%)")
# Change looks of labels
theme(axis.title = element_text(family="serif", face="italic", size=rel(1.1)))
#### LEGEND ####
# Reverse legend order
guides(fill = guide_legend(reverse = TRUE))
# Hide legend
theme(legend.position = "none")
# Put legend on top/bottom/left/right
theme(legend.position="top")
# Put bottom-left corner of legend box in bottom-left corner of graph
theme(legend.justification=c(0,0), legend.position=c(0,0))
# Custom legend title and text
scale_fill_discrete(name="Experimental\nCondition",
labels=c("Control", "Treatment 1", "Treatment 2"))
#### SCALE ####
# Hide scale elements
theme(axis.text = element_blank(), axis.ticks = element_blank())
# Adjust angle of axis text
theme(axis.text.x = element_text(angle=45, hjust=1))
# Adjust style of axis numbers
scale_x_continuous(labels=percent) # comma_format(), scientific_format() etc; see package scales
#### TITLE ####
# Set title
ggtitle("Fördelning per utbildningsnivå, kvinnor")
#### CANVAS ####
# Flip X/Y coordinates
coord_flip()
# Set canvas limits
ylim(30, 100)
xlim(1,1.8)
# "Split" canvas (for bar stacking)
geom_hline(yinchtercept=0, colour="grey90")
#### THEMES ####
# Set BW theme
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment