微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在R ggplot2中绘制3个data.frames

如何解决如何在R ggplot2中绘制3个data.frames

我有3个数据帧:

head(df1)

  Row.names       Nmds1       Nmds2 Station
1     E01M1 -1.39917737 -0.52996467     S01
2     E01M2 -0.85390268 -0.24919481     S01
3     E01M3 -0.31713304  0.14375418     S01
4     E01M4 -0.54494214 -0.12534209     S01
5     E02M1  0.03874897 -0.10945807     S02
6     E02M2  0.68255493 -0.07708227     S02

head(df2)

              Nmds1      Nmds2 env.pvals env.variables
Deep      0.23990125 -0.3194394 0.5384615          Deep
Salinity -0.18454375 -0.1849230 1.0000000      Salinity
Temp     -0.02935085  0.1953837 1.0000000          Temp
OD        0.27043891 -0.3425079 0.2967033            OD
pH        0.34483843 -0.1953211 0.6043956            pH
DBO5     -0.31011556  0.1377326 1.0000000          DBO5

head(df3)

               Nmds1      Nmds2 spp_pvals             Family
Otu_3503 -0.13102909  0.5044264     0.009 Desulfobacteraceae
Otu_3887 -0.09181908  0.5502878     0.004 Desulfobacteraceae
Otu_4287 -0.08140270  0.3914792     0.031 Desulfobacteraceae
Otu_4804 -0.10993229  0.5427913     0.005 Desulfobacteraceae
Otu_4807  0.21147428 -0.1891993     0.198   Desulfobulbaceae
Otu_5108 -0.45920996  0.0493874     0.012     Desulfatiglans

我试图从三个df,df2和df3中绘制一个

绘制df1

P <- ggplot(df1,aes(x = Nmds1,y = Nmds2)) + geom_point(aes(Nmds1,Nmds2,color = Station),size=4) + theme_bw()

绘制df1 + df2

P <- P + geom_segment(data = df2,aes(x = 0,xend = Nmds1,y = 0,yend = Nmds2),arrow = arrow(length = unit(0.25,"cm")),colour = "grey10",lwd = 0.3) + 
    ggrepel::geom_text_repel(data = df2,y = Nmds2,label = env.variables),cex = 4,direction = "both",segment.size = 0.25)

plot df1 + df2

问题!

我想使用geom_point将df3图添加到先前的P图中

ggplot(df3,color = Family),size=2,shape=20,alpha=0.4) + theme_bw()

enter image description here

我必须调整比例吗?如何在一个绘图中绘制所有3个df ???

谢谢

解决方法

实际上,您的问题是,对于两个不同的点集,您需要两个色标。

有解决此问题的方法,涉及到诸如ggnewscale之类的其他程序包,但在此情况下,更简单的方法是使用具有填充颜色的形状来代表较大的点。 shape = 21非常适合您的大积分。这样,您可以将填充比例用于Station,将颜色比例用于Family。请记住,如果要更改较大点的颜色,则需要使用scale_fill_xxx而不是scale_color_xxx

library(ggplot2)
library(ggrepel)

ggplot(df1,aes(x = NMDS1,y = NMDS2)) + 
  geom_point(aes(fill = Station),size = 5,shape = 21,color = "00000000") + 
  geom_segment(aes(x = 0,xend = NMDS1,y = 0,yend = NMDS2),data = df2,arrow = arrow(length = unit(0.25,"cm")),colour = "grey10",lwd = 0.3) +
  geom_text_repel(aes(label = env.variables),cex = 4,direction = "both",segment.size = 0.25) +
  geom_point(aes(color = Family),data = df3,size = 2,shape = 20) +
  theme_bw()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。