Last active
January 1, 2016 19:19
-
-
Save bryanmayer/8189651 to your computer and use it in GitHub Desktop.
Using multiple colours and fontfaces for axes labels in ggplot
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
library(ggplot2) | |
fakedata = data.frame( | |
Bacteria = c("G. vaginalis", "BVAB", "L. crispatus"), | |
Count = log10(rlnorm(3*100, 13.5)) | |
) | |
#create functions to map fontface and colour to bacteria category | |
#just using sapply | |
fontFaceFunction = function(bacteriaList) | |
sapply(bacteriaList, USE.NAMES = F, | |
function(x) | |
if (x == "BVAB") return("plain") | |
else return("italic") | |
) | |
#sapply and switch | |
fontColourFunction = function(bacteriaList) | |
sapply(bacteriaList, USE.NAMES = F, | |
switch, BVAB = "red", 'L. crispatus' = "blue", "black") | |
ggplot(data = fakedata, aes(x = Bacteria, y = Count)) + | |
geom_boxplot() + | |
scale_y_continuous(expression(paste("log"[10], "(Count)",sep = ""))) + | |
scale_x_discrete("") + | |
theme( | |
text = element_text(family = "Times", size = 20), | |
axis.text.x = element_text( | |
angle = 45, hjust = 1, | |
face = fontFaceFunction(levels(fakedata$Bacteria)), | |
colour = fontColourFunction(levels(fakedata$Bacteria)) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment