I tried to add captions in some plots that will be shown in a .rmd file, but the captions added are not very aesthetically pleasing. It will look much better if I just include the captions in the .rmd file instead of the plot. Are there any ways to make the captions look prettier in ggplot2?
library(ggplot2)
data <- data.frame(col = c("left", "right"),
row = c("first", "second", "third", "fourth"),
x = rep.int(1,4),
y = rep.int(1,4))
data$col <- as.character(data$col)
data$row <- as.character(data$row)
caption <- paste(strwrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", width = 170), collapse = "\n")
ggplot(data = data) +
facet_grid(row~col) +
labs(x = "x", y = "y", caption = caption) +
theme_bw(base_size = 16) +
theme(legend.position = "bottom",
plot.margin = margin(15, 15, 15, 15),
plot.caption = element_text(size = 10, hjust = 0))
The captions that I added
When using ggplot2 you can set a title, a subtitle, a caption and a tag. There are two ways to add titles: using ggtitle or labs function. The former is only for titles and subtitles and the latter also allows adding tags and captions. Option 1. Using ggtitle Option 2.
Add caption to a ggplot and change the position. Split a long title into two lines or more using as a text separator. Change the font appearance (text size, color and face) of titles and caption. For example, to set a bold ggplot title, use this: p + theme (plot.title = element_text (face = "bold")).
Change title position to the center or to any other locations (left, right). The default ggplot title alignment is not centered. It is on the left. It’s possible to put the title in the middle of the chart by specifying the argument hjust = 0.5 in the function element_text (): p + theme (plot.title = element_text (hjust = 0.5)).
p + labs (caption = c ("right footer", "left footer")) + theme (plot.caption = element_text (hjust=c (1, 0))) Great solution, should be the accepted answer.
library(ggplot2)
data <- data.frame(col = c("left", "right"),
row = c("first", "second", "third", "fourth"),
x = rep.int(1,4),
y = rep.int(1,4))
data$col <- as.character(data$col)
data$row <- as.character(data$row)
caption <- paste(strwrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", width = 170), collapse = "\n")
library(extrafont)
font_import(prompt=FALSE) # This installs the fonts. Only run this once!
loadfonts(device="win") # Loads the fonts
fonts_lookup = t(data.frame(windowsFonts())) # font lookup table
ggplot(data = data) +
facet_grid(row~col) +
labs(x = "x", y = "y", caption = caption) +
theme_bw(base_size = 16) +
theme(legend.position = "bottom",
plot.margin = margin(15, 15, 15, 15),
plot.caption = element_text(size = 12, hjust = 0.5,
family = "Garamond", color = "blue", face = "bold"))
Maybe change the font family, color, or face, or all at the same time? You can refer to this question for fonts: Changing fonts in ggplot2. The extrafont
package let's you install a lot more fonts than what is already available. windowsFonts()
let's you check what fonts are already loaded. I've created fonts_lookup
for easy lookup of fonts and their respective names. As for which font is the "prettiest", that's very subjective.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With