2

plotI am trying to add the labels A, B, and C to the top left hand corner of each of these graphs. I have tried cowplot::draw_plot_label(), but nothing seems to work. Can anyone help?

These A, B and C labels are not the main title of each plot.

# Packages
library(ggplot2)
library(gridExtra)
library(cowplot)

# 1st plot
p1 <- ggplot(data = new_data %>% 
               filter(Species =="Sharksucker_Remora")) +
  scale_colour_manual(values=c(Sharksucker_Remora="black"), labels = c("Sharksucker Remora")) + 
  geom_line(mapping = aes(x = Date, y = Proportion, group = Species, colour = Species)) + 
  xlab("") + 
  ylab("Proportion") + 
  theme(legend.position="top") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + labs(colour = ~italic(M.alfredi)~"Hitchhiker Species:") +
  theme(legend.key=element_blank())

# 2nd plot
p2 <- ggplot(data = new_data %>% 
               filter(Species !="Sharksucker_Remora")) +
  geom_line(mapping = aes(x = Date, y = Proportion, group = Species, colour = Species)) + 
  scale_colour_manual(values=c(Golden_Trevally="goldenrod2", Red_Snapper="firebrick2", Juvenile_Remora="darkolivegreen3"), labels = c("Juvenile Remora", "Golden Trevally", "Red Snapper")) + 
  xlab("") + ylab("Proportion") + labs(colour = "") + theme(legend.position="top") +  theme(legend.key=element_blank()) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

# 3rd plot
p3 <- ggplot(data = new_data_counts) +
  geom_bar(mapping = aes(x = Date, y = Count), stat =
             'identity') +
  xlab("Date (2015-2019)") +  ylab("Total"~italic
                                   (M.alfredi)~"Sightings") +
  draw_plot_label(label =c("C") + theme(axis.text.x =          
                                          element_text(angle = 90, vjust = 0.5, hjust   = 1))
                  
# The grid
grid.arrange(p1,p2,p3)
Z.Lin
  • 28,055
  • 6
  • 54
  • 94

3 Answers3

6

I suggest you use labs(..., tag = ...) and theme(plot.tag = element_text()). The code show how you can format the main title (here centered with hjust = 0.5) and the tag inside the theme() function. See the reproducible example, below:

# Packages
library(ggplot2)
library(gridExtra)
# library(cowplot) # not necessary here

# Plots
p1 <- ggplot() +
  labs(title = "plot 1", tag = "A") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.tag = element_text())

p2 <- ggplot() +
  labs(title = "plot 2", tag = "B") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.tag = element_text()) 

grid.arrange(p1, p2)

enter image description here

If you want the tag (A, B, C) to be inside the plotting area, this post suggest to use plot.tag.position = c(x, y). See for example:

p3 <- ggplot() +
  labs(title = "plot 3", tag = "C") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.tag = element_text(),
        plot.tag.position = c(0.1, 0.8))
p3

enter image description here

Paul
  • 2,850
  • 1
  • 12
  • 37
  • Thank you so much! so helpful. I was wondering if you also know how to drop the x axis title of p3 (Date 2015-2019) so its is lower as currently if is very close to the x axis labels?? – Aimee Nicholson-Jack Nov 05 '20 at 11:43
  • @AimeeNicholson-Jack you are welcome :) you can drop element from the plot using `theme()` and `element_blank()`, for example: `theme(axis.title.x = element_blank()))` – Paul Nov 05 '20 at 11:56
  • Hi, sorry I didn't mean to remove (drop) it completely. I meant to adjust the positioning of it, so that it sits lower down from the x axis labels. Does that make more sense? – Aimee Nicholson-Jack Nov 05 '20 at 11:58
  • @AimeeNicholson-Jack my bad, I read your comment too fast, I think it is possible using `theme(axis.title.x = element_text(vjust = -0.4)` (or any value that matches you need) – Paul Nov 05 '20 at 12:06
  • @AimeeNicholson-Jack this post has a more detailed procedure: https://stackoverflow.com/a/10840417/10264278 as you might need to increase the margins around the plot to make some space for the title. – Paul Nov 05 '20 at 12:10
  • how can this be used with facet_wrap/facet_grid, when specifying `labs(title = "plot 1", tag = "A")` isn't really a possibility? – E. Moore Jun 24 '21 at 18:19
  • @E.Moore I am not sure to understand. If you want to change facets labels you should use the labeller argument and function: `facet(..., labeller = labeller(...))` i.e. https://stackoverflow.com/questions/48860158/changing-ggplot2facet-wrap-title-from-the-default – Paul Jun 28 '21 at 07:42
  • @Paul Is it possible to have 2 "tags"? That is, in addition to the `tag = "C"` and a tag position of `plot.tag.position = c(0.1, 0.8))`, to specify an additional tag and position for that second tag? – coip Mar 09 '22 at 00:09
  • @coip, I don't know. I tried to feed a character vector to the `tag =` argument but it does not work. I think the way to go depends on if you want multipe tags on the same plot or on a facetted plot (*i.e.* using `ggplot2::facet_*()`). I would consider `geom_text()` for more complex situations. – Paul Mar 09 '22 at 07:31
0

Have you tried the package egg? https://cran.r-project.org/web/packages/egg/vignettes/Overview.html

library(tidyverse)
library(magrittr)
data <- list()
for(i in 1:6) data[[i]] <- rnorm(100,0,1)
data %<>% bind_cols() %>% setNames(paste0("var",1:6))
p1 <- ggplot(data,aes(x = var1, y = var2)) + geom_point()
p2 <- ggplot(data,aes(x = var3, y = var4)) + geom_point()
p3 <- ggplot(data,aes(x = var5, y = var6)) + geom_point()
egg::ggarrange(p1,p2,p3,ncol = 1,
               labels = c("A","B","C"))
Yang Liu
  • 36
  • 4
0

Another option is using the patchwork package with plot_annotation which has the tag_levels argument which gives the possibility to add tags like letters or numbers. First a reproducible example with letters:

library(patchwork)
library(ggplot2)
p1 <- ggplot(mtcars) + 
  geom_point(aes(hp, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, mpg, group = gear)) + 
  ggtitle('Plot 2')

p1 + p2 & plot_annotation(tag_levels = 'A')

Created on 2022-08-21 with reprex v2.0.2

Another option with numbers where you change the tag_levels to "1" like this:

p1 + p2 & plot_annotation(tag_levels = '1')

Created on 2022-08-21 with reprex v2.0.2

As you can see, the tags have letters or numbers. Check the links above for more information and options.

Quinten
  • 35,235
  • 5
  • 20
  • 53