【问题标题】:Add legend to ggarrange plot将图例添加到 ggarrange 情节
【发布时间】:2021-02-15 22:15:17
【问题描述】:

我有两个使用来自两个单独数据集(男性与女性)的数据的图,我使用 ggarrange() 并排排列了这些数据。男性用实心圆圈表示,女性用空心圆圈表示。

如何添加图例以便读者了解哪个图表来自男性数据,哪个图表来自女性数据?

这是我的代码:


Pronotum_Width_Female <- c("6.5", "7.4", "7.0","6.2", "6.3", "6.3","6.0", "6.4", "6.9","6.6", "6.8", "7.2")

Year_Female <- c("1995", "1999", "2001","2003", "2005", "2007","2008", "2009", "2010","2011", "2012", "2013")

female <- data.frame(Pronotum_Width_Female, Year_Female)

Pronotum_Width_Male <- c("6.4", "5.9", "5.8","6.2", "6.5", "6.0","6.2", "5.7", "5.8","6.0", "6.1", "6.5")

Year_Male <- c("1995", "1999", "2001","2003", "2005", "2007","2008", "2009", "2010","2011", "2012", "2013")

male <- data.frame(Pronotum_Width_Male, Year_Male)

plot1 <- ggplot(female, aes(y=Pronotum_Width_Female,x=Year_Female)) + 
  geom_point(size = 2, shape = 1) + 
  labs(x="Year", y = "Pronotum Width (mm)") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.title = element_text(size=15), # for axix
        title = element_text(size=15),axis.text = element_text(size=15),
        axis.title.x = element_text(vjust = 0),
        axis.title.y = element_text(vjust = 2), 
        legend.title = element_text(vjust = 0.5))+
  coord_cartesian(ylim=c(5,12))


plot2 <- ggplot(male, aes(y=Pronotum_Width_Male,x=Year_Male)) + 
  geom_point(size = 2) + 
  geom_smooth(method = "lm", se = TRUE,col="black",size=1) +
  labs(x="Year", y = "Pronotum Width (mm)") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.title = element_text(size=15), # for axix
        title = element_text(size=15),axis.text = element_text(size=15),
        axis.title.x = element_text(vjust = 0),
        axis.title.y = element_text(vjust = 2), 
        legend.title = element_text(vjust = 0.5))+
  coord_cartesian(ylim=c(5,12))

ggarrange(plot2, plot1,
          labels = c("a)", "b)"),
          ncol = 2, nrow = 1)

【问题讨论】:

标签: r ggplot2 ggpubr


【解决方案1】:

不确定您要达到什么目的,但我认为最简单的方法是将您的数据绑定在一起,按性别制作一个情节和方面,这将为您提供漂亮的面板标签:

Pronotum_Width_Female <- c("6.5", "7.4", "7.0","6.2", "6.3", "6.3","6.0", "6.4", "6.9","6.6", "6.8", "7.2")
Year_Female <- c("1995", "1999", "2001","2003", "2005", "2007","2008", "2009", "2010","2011", "2012", "2013")
Female <- data.frame(Pronotum_Width = Pronotum_Width_Female, Year = Year_Female)

Pronotum_Width_Male <- c("6.4", "5.9", "5.8","6.2", "6.5", "6.0","6.2", "5.7", "5.8","6.0", "6.1", "6.5")
Year_Male <- c("1995", "1999", "2001","2003", "2005", "2007","2008", "2009", "2010","2011", "2012", "2013")
Male <- data.frame(Pronotum_Width = Pronotum_Width_Male, Year = Year_Male)

library(ggplot2)
library(dplyr)

d <- dplyr::bind_rows(list(Female = Female, Male = Male), .id = "gender") %>% 
  # Make Pronotum_Width numeric
  mutate(Pronotum_Width = as.numeric(Pronotum_Width))

ggplot(d, aes(y=Pronotum_Width, x = Year)) + 
  geom_point(aes(shape = gender), size = 2) + 
  scale_shape_manual(values = c(Female = 1, Male = 16)) +
  labs(x="Year", y = "Pronotum Width (mm)") + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = NA), 
        axis.line = element_line(colour = "black")) +
  theme(axis.title = element_text(size=15), # for axix
        title = element_text(size=15),
        axis.text = element_text(size=15),
        axis.title.x = element_text(vjust = 0),
        axis.title.y = element_text(vjust = 2), 
        legend.title = element_text(vjust = 0.5),
        strip.background.x = element_rect(fill = NA)) +
  coord_cartesian(ylim = c(5, 12)) +
  facet_wrap(~gender) +
  # Drop the legend
  guides(shape = FALSE)

【讨论】:

    猜你喜欢
    • 2019-11-06
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多