2

I find there is a temporal way to change the text size of the caption. However, I am just searching for a safer solution. Typically, I go through all arguments in the function theme with caption*, but I cannot find an argument to deal with it.


Here is a reproducible minimal example for the suggestion from @Tung.

The only difference between the 1st figure and the 2nd one is the space width of the two lines in the caption. For my preference, I think one-line width is too wider, how to get it like 50% one-line width.

suppressMessages(library(tidyverse))
mtcars %>% 
    ggplot() +
    aes(mpg, disp) +
    geom_point() -> p
p + 
    labs(caption = 'line1\nline2')

enter image description here

p + 
    labs(caption = 'line1\n\nline2')

enter image description here


I don't use the function reprex to get the output because of this error.

> reprex(si = T)
Rendering reprex...
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Timeout was reached: Connection timed out after 10001 milliseconds

By the way, I cannot use the function reprex with the output picture because of the connection restrictions in China. This issue I talks with @yihui and @jennybc on one GitHub Issue for a long time, it is hard to fix and only to wait for an available one in China. But all code I provide I think is enough to reproduce the figures.


Thanks for the @Gregor 's solution, here is an example to display this idea.

mtcars %>% 
    ggplot() +
    aes(mpg, disp) +
    geom_point() +
    labs(caption = 'line1\nline2') -> p0
# p0
p0 + theme(plot.caption = element_text(lineheight = 1.5)) -> p1
p0 + theme(plot.caption = element_text(lineheight = 2.0)) -> p2
p0 + theme(plot.caption = element_text(lineheight = 3.0)) -> p3
p0 + p1 + p2 + p3 + plot_layout(nrow = 2,byrow = T)

enter image description here

Jiaxiang
  • 865
  • 12
  • 23
  • 1
    Are you looking for `theme(plot.caption = geom_text(...))`? – Z.Lin Mar 26 '19 at 04:59
  • 1
    I think @Z.Lin meant `theme(plot.caption = element_text(size = ..., vjust = ...))`. Alternatively, you can just add `\n` before your caption to add an extra line to it. – Tung Mar 26 '19 at 06:41
  • 1
    @Tung Thanks for pointing out! was typing in a hurry... – Z.Lin Mar 26 '19 at 07:16
  • Hi, @Tung, I think it is a manual way. But if I want to just 0.5 widths ... Because sometimes 1 space is too large, 0.5 is better. – Jiaxiang Mar 26 '19 at 17:30
  • @Jiaxiang: Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Mar 26 '19 at 23:04
  • @Tung, I add it in the content, thanks for your suggestion. – Jiaxiang Mar 27 '19 at 16:49

1 Answers1

4

You can adjust the lineheight for the caption text.

p + labs(caption = 'line1\nline2') +
  theme(plot.caption = element_text(lineheight = 1.5))

I think this is equivalent to "line spacing", that is 1 is singlespaced, 2 is doublespaced, etc.

If found the solution by looking at the ?theme help page and searching for "caption". This revealed the plot.caption theme setting, and sad it was element_text. I followed the link to ?element_text and saw the lineheight argument.

enter image description here

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294