Chapter 16 Titles and text
Including titles in ggplot is rather straightforward (whether they are plot-titles, axes-titles, or legend-titles)
ggplot(mpg, aes(x=displ, y=cty, colour=drv)) +
geom_point() +
labs(x="Engine displacement (in litres)",
y="Mileage per galon of fuel",
title="Cars with higher engine displacement are less fuel efficient",
colour="Type of drive")
Let’s change some things (also including line-breaks) to get a better impression:
ggplot(mpg, aes(x=displ, y=cty, colour=drv)) +
geom_point() +
labs(x="Engine displacement (in litres)",
y="Mileage per galon of fuel",
title="Cars with higher engine displacement\nare less fuel efficient",
subtitle="Not very surprising",
colour=NULL)
16.1 theme()
We can also change the appearance of text of all the seperate elememts. We do this with the “element_text” function within the “theme()” argument. “theme()” is generally used to control the overal appearence of the graph (see Chapter 19 for more info) [I hope the code is self-explanatory]
ggplot(mpg, aes(x=displ, y=cty, colour=drv)) +
geom_point() +
labs(x="Engine displacement (in litres)",
y="Mileage per galon of fuel",
title="Cars with higher engine displacement\n are less fuel efficient",
colour="Type\nof\ndrive") +
theme(axis.title = element_text(face="bold"),
plot.title = element_text(face="bold", size=16, colour="grey"),
axis.text.y = element_text(angle=45),
legend.title = element_text(family ="serif", face="italic"),
legend.title.align=0.5,
legend.text = element_text(face="bold.italic", size=10))