【问题标题】:How to add labels to multiple ggplot graphs (A, B, C)如何将标签添加到多个 ggplot 图(A、B、C)
【发布时间】:2020-11-04 19:22:01
【问题描述】:

我正在尝试将标签 A、B 和 C 添加到每个图表的左上角。我试过cowplot::draw_plot_label(),但似乎没有任何效果。有人可以帮忙吗?

这些 A、B 和 C 标签并不是每个情节的主标题。

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

【问题讨论】:

    标签: r ggplot2 label gridextra cowplot


    【解决方案1】:

    我建议你使用labs(..., tag = ...)theme(plot.tag = element_text())。 该代码显示了如何格式化主标题(此处以hjust = 0.5 为中心)和theme() 函数内的标签。请参阅下面的可重现示例:

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

    如果您希望标签(A、B、C)位于绘图区域内,this post 建议使用plot.tag.position = c(x, y)。例如:

    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
    

    【讨论】:

    • 非常感谢!很有帮助。我想知道你是否也知道如何删除 p3 的 x 轴标题(日期 2015-2019),所以它现在较低,好像非常接近 x 轴标签??
    • @AimeeNicholson-Jack 不客气 :) 您可以使用 theme()element_blank() 从图中删除元素,例如:theme(axis.title.x = element_blank()))
    • 嗨,对不起,我不是要完全删除(删除)它。我的意思是调整它的位置,使其位于 x 轴标签的下方。这更有意义吗?
    • @AimeeNicholson-Jack 我的错,我读你的评论太快了,我认为可以使用theme(axis.title.x = element_text(vjust = -0.4)(或任何符合你需要的值)
    【解决方案2】:

    你试过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,看起来很有趣,你能多介绍一下这个包的使用方法吗?
    • @Paul 示例已添加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 2018-09-04
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    相关资源
    最近更新 更多