Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 836
Aimee Nicholson-Jack Avatar asked Nov 30 '25 08:11

Aimee Nicholson-Jack


1 Answers

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

like image 124
Paul Avatar answered Dec 01 '25 21:12

Paul