Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将图例添加到绘图中_R_Ggplot2_Ggpubr - Fatal编程技术网

R 将图例添加到绘图中

R 将图例添加到绘图中,r,ggplot2,ggpubr,R,Ggplot2,Ggpubr,我有两个图,使用来自两个独立数据集(男性和女性)的数据,我使用ggarrange()并排排列。男性用填充的圆圈表示,女性用未填充的圆圈表示 我如何添加图例,以便读者了解哪个图形来自男性数据,哪个图形来自女性数据 这是我的密码: Pronotum_Width_Female <- c("6.5", "7.4", "7.0","6.2", "6.3", "6.3","

我有两个图,使用来自两个独立数据集(男性和女性)的数据,我使用
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)


Pronotum_Width_Female不确定您想要实现什么,但我认为最简单的方法是将您的数据绑定在一起,按性别绘制一个图和面,这将为面板提供漂亮的标签:


Pronotum\u Width\u Female建议发布一个完全可复制的示例(而且,您的代码的第一部分似乎被遗漏了)抱歉,我现在添加了数据。