15 April 2019
Two main points:
ggplot2 ggplot2 uses themes to control the overall appearance
ggplot(transport, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw()
My default is theme_bw()
ggplot2 ggplot(transport, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_classic()
ggplot(transport, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_void()
You can set a default theme for a workspace or session
theme_set(theme_bw())
Now all plots in the workspace will use theme_bw()
ggplot2 The theme() function is also where you set:
axis.text, legend attributes etc.?theme
ggplot2 Changing text within themes uses element_text()
?element_text
ggplot(transport, aes(x = gender, y = height, fill = gender)) +
geom_boxplot() +
theme_bw() +
theme(axis.text.x = element_text(
angle = 90, hjust = 1, vjust = 0.5
))
ggplot2 Changing backgrounds and outlines uses element_rect()
ggplot(transport, 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(transport, 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(transport, 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(transport, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw() + theme(legend.position = c(0.85, 0.15))
The plotting region is assumed to have width and height of 1
We can also edit axes, fills, outlines etc. using scale_...() layers
ggplot(transport, 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(transport, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw() + scale_y_continuous(expand = c(0, 0))
a = 0 multiplies the scale by 1 + ab = 0 adds \(\pm\)b to the axis extremaAlternatively, we can use expand_scale() to set expansion on either side.
ex <- expand_scale(mult = c(0, 0.05), add = c(0, 0)) ggplot(transport, aes(x = gender, y = height, fill = gender)) + geom_boxplot() + theme_bw() + scale_y_continuous(expand = ex)
expand_scale() adjusts bottom/top or left/right depending on your axis.ggplot(transport, 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(transport, 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(transport, 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
pdf and svg output are vector-based not pixel-basedtoothData, PCR and RealTimeData