Aimee Nicholson-Jack
Aimee Nicholson-Jack

Reputation: 69

How to add labels to multiple ggplot graphs (A, B, C)

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)

Upvotes: 3

Views: 14322

Answers (3)

Quinten
Quinten

Reputation: 41285

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.

Upvotes: 0

Yang Liu
Yang Liu

Reputation: 36

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

Upvotes: 0

Paul
Paul

Reputation: 2977

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

Upvotes: 7

Related Questions