Figure labels or captions in ggplot2

How can i add a reasonable figure label (also known, i believe, as figure caption) in ggplot2? By figure label, i am referring to labelling a figure as "Figure 1" or "Figure 2", etc. This is useful if you are writing a paper, or even a book, and your narrative makes references to different figures whose plots you have included (in the paper or book).

Suppose i have the following R code:

library(ggplot2)
library(gapminder)
data(gapminder, package = 'gapminder')

ggplot(subset(gapminder, year %in% 1977), aes(gdpPercap, lifeExp)) +
  geom_point() + 
  ggtitle("test title") +
  theme(plot.title = element_text(hjust = 0.5))

What modification to the code do i need to do to add the figure label so that the figure is labelled as either "Figure 1" or "Figure 2"? "test title" in the code I have given is the (dummy) title of the plot.

I think the figure label should be placed just below the plot (below the x axis label of the plot). Just after the figure label (say "Figure 1"), I should also have the option of writing a brief note describing the salient features of the plot. For example, "Figure 1 This is a test note".

Hi,

A similar question has been asked before on this forum and was solved by a community member

Hope this helps,
PJ

1 Like

You can also use the tag label

library(ggplot2)
library(gapminder)
data(gapminder, package = 'gapminder')

ggplot(subset(gapminder, year %in% 1977), aes(gdpPercap, lifeExp)) +
    geom_point() + 
    labs(title = "test title",
         tag = "Figure 1: This is a test note") +
    coord_cartesian(clip = "off") +
    theme(plot.title = element_text(hjust = 0.5),
          plot.margin = margin(t = 10, r = 10, b = 40, l = 10),
          plot.tag.position = c(0.2, -0.1)
          )

Created on 2019-09-29 by the reprex package (v0.3.0)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.