Options to control titles in ggplot are described below.
Chapter Content
- Data
- Basic Plot: No Title Aesthetics
- Title Aesthetics
Data
The diamonds dataset that ships with ggplot.
Basic Plot: No Titles
1 2 3 4 5 6 7 |
# Plot skeleton p0 <- ggplot(diamonds, aes(depth)) + xlim(58, 68) # Plot1: Histogram with facetting p1 <- p + geom_histogram(aes(y = ..density..), binwidth = 0.1, colour = "dark orange", fill = "orange") + facet_grid(cut ~ .) # Print graph object p1 |
Title Aesthetics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Plot2: Main title with line break p1 + labs(title = "Diamond Data\nDepth versus Cut") # Plot3: Main title with alternative line break and font formats p1 + labs(title = expression(atop(bold("Diamond Data"), atop(italic("Depth versus Cut"), "")))) + theme(plot.title = element_text(size = 25)) # Plot3: x and y axis title p1 + labs(x = "Diamond Depth", y = "Frequency %") # Plot4: x any y axis titles with text and title formats p1 + labs(x = "Diamond Depth (mm)", y = "Frequency (%)") + theme(axis.title.y = element_text(size = 14, vjust = 0.25, face = "bold"), axis.text.y = element_text(size = 11, color = "black"), axis.title.x = element_text(size = 14, vjust = -0.25, face = "bold"), axis.text.x = element_text(size = 11, color = "black", angle = -90), strip.text.y = element_text(size=12, face="bold", color="white"), strip.background = element_rect(color="black", fill="skyblue4"), panel.border = element_rect(fill = NA, colour="navy")) |