20 July 2016
Two main points:
ggplot2 ggplot2 uses themes to control the overall appearance
ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw()
My default is theme_bw()
ggplot2 ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_classic()
ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_void()
ggplot2 ggthemeslibrary(ggthemes)
ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_economist()
ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_excel()
ggplot2 The theme() function is where you set:
axis.text, legend attributes etc.?theme
ggplot2 Changing text within themes uses element_text()
?element_text
ggplot(data, aes(x = gender, y = height, fill = gender)) +
geom_boxplot() +
theme_bw() +
theme(axis.text = element_text(family = "Courier",
size = 15, angle = 30))
ggplot2 Changing backgrounds and outlines uses element_rect()
ggplot(data, aes(x = gender, y = height, fill = gender)) +
geom_boxplot() +
theme_bw() +
theme(legend.background = element_rect(fill = "yellow",
colour = "black"))
ggplot2 To remove all attributes use element_blank() in place of element_rect(), element_text() or element_line()
ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw() + theme(panel.grid = element_blank())
We can move the legend to multiple places:
ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw() + theme(legend.position = "bottom")
Or we can use co-ordinates to place it inside the plotting region
ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw() + theme(legend.position = c(0.8, 0.1))
We can also edit axes, fills, outlines etc. using scale_...() layers
ggplot(data, aes(x = gender, y = height, fill = gender)) +
geom_boxplot() +
theme_bw() +
scale_y_log10(limits = c(100, 200),
breaks = c(100, 125, 150, 175))
We can turn off or modify plot expansion
ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw() + scale_y_continuous(expand=c(0, 0))
ggplot(data, aes(x = gender, y = height, fill = gender)) +
geom_boxplot() +
theme_bw() +
scale_fill_manual(values = c("green", "blue"))
Colours can also be specified using hexadecimal codes
rgb(1, 0, 0)
[1] "#FF0000"
ggsave()The main image formats are jpeg, png and tiff
R can also export svg and pdfggplot2 has the function ggsave()?ggsave
The Plots Tab is the default graphics device
ggsave()ggplot(data, aes(x = gender, y = height, fill = gender)) +
geom_boxplot() +
theme_bw() +
scale_fill_manual(values = c("green", "blue"))
ggsave("HeightByGender.png")
ggsave defaults to 300dpiPlots Tabggsave()width and height attributesggsave("HeightByGender.png", width = 18, height = 18, units = "cm")
png(), jpeg(), pdf() etc.dev.off()Plots tabpng("HeightByGender.png", width = 18, height = 18, units = "cm",
res = 300)
ggplot(data, aes(x = gender, y = height, fill = gender)) +
geom_boxplot() +
theme_bw() +
theme(text = element_text(size = 16)) +
scale_fill_manual(values = c("green", "blue"))
dev.off()
Try to decide how big the plot will be in your final document
To make multiple plots/subplots, use the package grid
R objectprint() function, specifying the viewportplotHeight <- ggplot(data, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw() + guides(fill = FALSE) plotWeight <- ggplot(data, aes(x = gender, y = weight, fill = gender)) + geom_boxplot() + theme_bw()
Create the viewports and print
library(grid)
vp1 <- viewport(x = 0, y = 0, width = 0.4, just = c("left", "bottom"))
vp2 <- viewport(x = 0.4, y = 0, width = 0.6, just = c("left", "bottom"))
grid.newpage()
print(plotHeight, vp = vp1)
print(plotWeight, vp = vp2)
grid.text("A", 0.05, 0.95)
grid.text("B", 0.45, 0.95)