简体   繁体   中英

how to plot 3 data.frames in R ggplot2

I have 3 data frames:

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

and I have tried to make a single plot from three df: df1, df2 and df3

plot df1

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

plot 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, aes(x = NMDS1, y = NMDS2, label = env.variables), cex = 4, direction = "both", segment.size = 0.25)

绘制 df1 + df2

the problem!!!

I want to add the df3 plot to the previous P plot, using geom_point

ggplot(df3, aes(x = NMDS1, y = NMDS2)) + geom_point(aes(NMDS1, NMDS2, color = Family), size=2, shape=20, alpha=0.4) + theme_bw()

在此处输入图片说明

do I have to adjust scales ?, how could I plot all 3 df in a single plot ???

Thanks

Effectively your problem is that you need two color scales for two different sets of points.

There are solutions to this problem involving other packages such as ggnewscale , but an easier way to do it here is to use a shape that has a fill color for your larger points. shape = 21 would be ideal for your larger points. That way you can use the fill scale for Station and the color scale for Family . Just remember if you want to change the colors for the larger points you need to use scale_fill_xxx rather than 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), data = df2,
                  cex = 4, direction = "both", segment.size = 0.25) +
  geom_point(aes(color = Family), data = df3, size = 2, shape = 20) +
  theme_bw()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM