[英]how to plot 3 data.frames in R ggplot2
I have 3 data frames:我有 3 个数据框:
head(df1)头(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)头(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)头(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我试图从三个 df 制作一个图:df1、df2 和 df3
plot df1绘图 df1
P <- ggplot(df1, aes(x = NMDS1, y = NMDS2)) + geom_point(aes(NMDS1, NMDS2, color = Station), size=4) + theme_bw()
plot df1 + df2绘制 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)
the problem!!!问题!!!
I want to add the df3 plot to the previous P plot, using geom_point我想使用 geom_point 将 df3 图添加到之前的 P 图
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 ???我必须调整比例吗?,我怎么能在一个图中绘制所有 3 个 df ???
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.这个问题有一些解决方案涉及其他包,例如
ggnewscale
,但在这里更简单的方法是使用具有较大点填充颜色的形状。 shape = 21
would be ideal for your larger points. shape = 21
将是您较大点的理想选择。 That way you can use the fill scale for Station
and the color scale for Family
.这样您就可以对
Station
使用填充比例,对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
请记住,如果要更改较大点的颜色,则需要使用
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), data = df2,
cex = 4, direction = "both", segment.size = 0.25) +
geom_point(aes(color = Family), data = df3, size = 2, shape = 20) +
theme_bw()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.