How to make ggplot2 captions more aesthetically pleasing?

I tried to add captions on some of the graphs that will be shown in the .rmd file, but the added captions are not very aesthetic. It will look much better if I just include the captions in the .rmd file instead of the graph. Are there any ways to make captions more beautiful 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)) 

Captions I added The captions that I added

+5
source share
1 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")) 

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: Change fonts in ggplot2 . The extrafont package allows you to install many more fonts than the one that is already available. windowsFonts() allows you to check which fonts are already loaded. I created fonts_lookup to conveniently find fonts and their corresponding names. As for which font is the “most beautiful”, it is very subjective.

+4
source

Source: https://habr.com/ru/post/1270452/


All Articles