9

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 The captions that I added

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Yuxin Wu
  • 101
  • 1
  • 4

1 Answers1

7
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"))

enter image description here

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.

acylam
  • 18,231
  • 5
  • 36
  • 45