我想创建一个显示两个变量的图,其中一个变量分组(椭圆),另一个符号着色。 x轴上的“ RW1”,y轴上的“ RW2”,根据“ Morph.cluster”对椭圆进行分组,并根据作为遗传簇的“ ALL”对几何形状进行着色。我正在使用adegenet软件包中的ade4。问题在于,我似乎只能使该图一次只适用于一个变量。
我的数据看起来像
ID ggpop AC70 AC50 ALL Morph.cluster RW1 RW2 RW3
LT-103 gg3 NA 2 2 1 0.00793 0.01137 -0.00379
LT-109 gg4 1 1 1 2 -0.0156 -0.01056 0.00869
LT-110 gg3 1 1 1 1 0.01585 0.00077 -0.00923
LT-12 gg5 4 4 4 1 0.01299 0.00383 0.00517
LT-122 gg4 NA 3 3 1 0.02727 0.01845 -0.0078
对于某些示例,ggpop是NA。对于这些样品,我希望我可以将颜色留空。如果没有,我将再创建一个gg类别,并将其标记为空。
使用
library(ade4)
plot_All = plot(RWggclust2_eds$RW1,RWggclust2_eds$RW2, type="n", yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)")
axis(2, las=2)
myCol2=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$Morph.cluster),
add.plot=T,
col=myCol2,
cpoint=2,
clabel=0,
pch=c(15,16,17) [as.numeric(RWggclust2_eds$Morph.cluster)],
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
我得到了上面想要的方式绘制的椭圆,定义了形态簇,但是几何形状的颜色不是我想要的。我希望颜色符合“ ALL”(遗传)簇中定义的五个组。
但是,使用
RWggclust2_eds <- read.csv("RWggclust2_eds.csv")
str(RWggclust2_eds)
RWggclust2_eds$ALL = as.factor(RWggclust2_eds$ALL)
RWggclust2_eds$ALL = as.factor(RWggclust2_eds$ggpop)
RWggclust2_eds$Morph.cluster = as.factor(RWggclust2_eds$Morph.cluster)
plot_All = plot(RWggclust2_eds$RW1,RWggclust2_eds$RW2, type="n", yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)")
axis(2, las=2)
myCol2=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$ALL),
add.plot=T,
col=myCol2,
cpoint=2,
clabel=0,
pch=c(15,16,17) [as.numeric(RWggclust2_eds$Morph.cluster)],
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
我得到了上面的内容,并在遗传簇上绘制了椭圆(数据集中的“ ALL”列),这不是我想要的(我希望形状根据我的第一个图进行分组),但是颜色是在图中是正确的。
我也厌倦了“ col”参数的各种迭代
#index=as.integer(RWggclust2_eds$ALL)
#sclasscolour=rainbow(6)
#sclasscolour=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
#myCol2= transp(c(sclasscolour), 1) [index]
问题是我认为符号的颜色是从“ fac”中读取的,但是似乎Morph.cluster的变形也是如此。
如果可以的话,我很乐意发布我的数据文件。
答案 0 :(得分:1)
您快到了。我认为在s.class中,这些点的颜色将与日食一起出现(至少在我尝试过的时候)。
由于您必须将月食分层放置在一个空图上,所以我认为最简单的解决方案是先根据自己的喜好绘制点,然后在没有点的情况下添加月食(设置pch = NA)
我使用了ggplot中的alpha作为透明度,但随时可以将其更改回
library(ade4)
library(ggplot2)
# i use alpha from ggplot2
# how i read and proceeded with your csv file
#CSV = read.csv("RWggclust2_eds.csv")
#RWggclust2_eds = CSV[,c("ggpop","ALL","Morph.cluster","RW1","RW2")]
# similar to what you did
# just providing names
myCol2=alpha(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
# set it according to popn
names(myCol2) = levels(RWggclust2_eds$ggpop)
# set the shapes
PCH = c(15,16,17)
# we can wrap it around using with
plot_All = with(RWggclust2_eds,
plot(RW1,RW2, yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)",
pch= PCH[Morph.cluster],
col=myCol2[as.character(ggpop)]
)
)
#provide a legend
legend("topleft",names(myCol2),fill=myCol2,horiz=T,cex=0.7)
axis(2, las=2)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$Morph.cluster),
add.plot=T,
cpoint=2,
clabel=0,
pch=NA,
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
您得到了情节:
下面是绘制图的数据
dput(RWggclust2_eds)
structure(list(ggpop = structure(c(3L, 4L, 3L, 5L, 4L, 1L, 3L,
3L, 1L, 1L, 4L, 3L, 5L, 1L, 3L, 1L, 1L, 1L, 1L, 4L, 3L, 1L, 1L,
3L, 1L, 3L, 3L, 5L, 3L, 1L, 3L, 1L, 3L, 3L, 3L, 1L, 4L, 3L, 1L,
4L, 5L, 3L, 4L, 1L, 2L, 2L, 5L, 5L, 4L, 4L, 2L, 5L, 2L, 2L, 3L,
3L, 1L, 1L, 3L, 4L, 1L, 3L, 1L, 1L, 1L, 4L, 1L, 2L, 4L, 1L, 5L,
1L, 4L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 4L, 4L, 5L, 5L, 3L, 3L, 1L,
1L, 3L, 2L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 5L,
3L, 1L, 3L, 4L, 4L, 1L, 1L, 3L, 4L, 3L, 1L, 1L, 4L, 1L, 1L, 2L,
4L, 1L, 4L, 5L, 5L, 1L, 1L, 1L, 1L, 4L, 1L, 3L, 1L, 3L, 1L, 1L,
2L, 3L, 1L, 1L, 5L, 4L, 4L, 3L, 2L, 4L, 3L, 3L, 3L, 2L, 3L, 2L,
3L, 3L, 2L, 4L, 2L, 1L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 5L, 1L, 1L,
1L, 4L, 1L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L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 3L,
2L, 2L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 3L, 2L, 2L,
2L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 3L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
2L, 3L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
3L, 1L, 3L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 2L), RW1 = c(0.00793,
-0.0156, 0.01585, 0.01299, 0.02727, 0.00344, 0.031, 0.01568,
-0.00411, -0.00076, 0.01339, 0.02017, 0.02123, -0.0061, 0.01376,
-0.02301, 0.00501, 0.02418, -0.00102, -0.00813, 0.0032, -0.0122,
-0.00317, 0.00018, 0.00051, -0.00184, -0.00323, 0.01701,
-0.00027, 0.00288, -0.00689, -0.01864, 0.003, 0.00463, 0.0119,
-0.01515, 0.00364, 0.02048, -0.01233, -0.00774, -0.00348,
-0.00219, 0.00676, -0.00978, 0.00767, -0.00819, -0.01892,
-0.00584, 0.012, 0.00617, -0.01743, -0.02724, -0.03363, 0.01004,
0.02196, 0.01074, 0.0053, 0.00197, -0.01368, 0.00802, -0.00659,
-0.00521, -0.00938, 0.00901, 0.00639, 0.02151, -0.01637,
-0.02571, -0.00354, -0.00396, 0.03228, 0.01599, -0.00646,
0.00993, 0.02582, -0.01889, 0.01162, 0.00487, 0.02127, -0.00994,
-0.01862, 0.01338, 0.00229, 0.01818, 0.00687, 0.00545, 0.01723,
0.00438, 0.00832, 0.00095, -0.00751, -0.00645, -0.01469,
-0.01141, 0.01079, -0.00567, 0.00348, 0.00904, 0.00055, 0.02002,
-0.00382, -0.00244, 0.03351, -0.01225, 0.00865, -0.00105,
-0.01337, 0.00853, -0.00584, -0.001, -0.00068, -0.01427,
0.00742, 0.00352, -0.01016, -0.00713, -0.00129, -0.01557,
-0.00792, -0.00067, 0.00432, -0.01509, 0.03477, 0.01061,
-0.00097, 0.00797, -0.01066, -0.01534, 0.0192, 0.00266, -0.00549,
-0.02314, 0.00139, -0.02194, -0.00406, 0.00287, -0.01561,
-0.00329, 0.01625, 0.00822, 0.01719, -0.00545, 0.0019, 0.00278,
0.00436, 0.01424, -0.01102, -0.01331, 0.00928, 0.01181, 0.00416,
0.01673, 0.00715, -0.0159, -0.00208, -0.03367, 9e-04, -0.00011,
-0.02768, -0.01608, -0.00345, 0.00062, 0.00048, -0.01542,
0.02665, -0.0181, 0.0098, -0.00995, 0.00842, 0.00123, 0.00896,
0.02197, -0.00163, -0.01072, -0.01137, -0.01126, -0.01656,
0.00518, 0.03913, -0.02236, -0.02881, -0.0117, 0.01644, -0.00465,
-0.00811, 0.00107, 0.02483, 0.00197, -0.01018, 0.01023, 0.01279,
-0.01052, -0.0069, -0.01409, 0.01462, 0.00435, 0.01912, -0.01392,
0.02166, 0.00621, 0.0112, -0.01114, 0.00728, -0.0086, 5e-05,
0.00352, -0.00939, -0.01502, 0.00394, 0.01684, -0.00077,
-0.00037, 0.00563, -0.03636, 0.00443, 0.01819, 0.00603, -0.00363,
-0.02126, 0.01857, 0.00014, -0.03048, 0.00621, -0.00452,
0.02451, 0.00715, -0.00705, 0.01619, -0.016, -0.00362, -0.00949,
0.02046, -0.016, -0.01655, 0.00079, 0.00188, -0.01497, 0.01269,
0.02089, -0.01006, -0.01574, -0.00349, -0.01533, 0.01438,
0.0055, -0.00257, 0.00254, -0.00647, -0.00356, 0.02158, -0.03029,
0.01802, -0.00391, -0.01141, -0.0355, 0.00911, 0.00136, 0.00631,
-0.00551, -0.01403, 0.00929, 0.0059, -0.00238, 0.00028, -0.01683,
0.00347, -0.0213, 0.00411, 0.00953, 0.00029, -0.0028, 0.01801,
0.00405, -0.0079, -0.01496, -0.00638, -0.01092, -0.00773,
-0.00368, 0.00526, -0.00739), RW2 = c(0.01137, -0.01056,
0.00077, 0.00383, 0.01845, 0.00058, 0.02422, 0.00129, -0.01173,
-0.0161, 0.01019, 0.00135, 0.00579, 0.00576, 0.00751, 0.01384,
0.00408, -0.00634, -0.00044, -0.00168, -0.00021, 0.00019,
0.00428, -0.00924, 0.00098, 0.00518, -0.00184, -0.00469,
-0.00921, -3e-05, 0.00688, -0.00778, 0.00165, -0.00664, 0.00856,
-0.00157, 0.00322, -7e-05, -0.00405, 6e-04, -0.00464, -0.0019,
-0.00849, 0.00363, -0.01107, 0.00175, -0.03478, -0.00816,
-0.00338, 0.02075, -0.00039, -0.02465, -0.01461, 0.00529,
-0.00144, -0.00259, -0.00803, -0.00693, 0.00761, 0.01159,
0.01422, -0.00876, -0.00469, -0.00052, 0.00823, 0.01019,
0.00032, 0.00842, 0.02197, 4e-04, -0.02939, 0.00432, 0.00774,
0.00848, -0.00342, -0.00953, -0.01048, -0.0014, -0.01465,
0.0027, 0.00879, -4e-05, 0.00985, -0.03324, 0.01427, 0.00036,
-0.01587, -0.0012, 0.00331, 0.0089, -0.00684, -0.0348, 0.00497,
0.01468, -0.00218, -0.01662, 0.00608, -0.01143, 0.00312,
-0.01165, 0.00966, 0.00179, -0.01137, 0.00335, 0.0152, -0.00442,
0.01031, -0.01745, -0.01658, 0.003, 0.011, -0.00583, 0.0171,
0.00062, 0.00842, 0.00759, -0.00401, 0.01916, -0.00491, 0.01416,
0.00117, -0.00164, -0.02815, 0.00648, 0.00067, -0.006, -0.00454,
0.00368, 0.00666, 0.00352, 0.00368, -0.00999, 0.01213, -0.0022,
-0.0015, -0.02383, 0.00748, -4e-05, -0.02224, -0.01355, 0.00525,
0.00142, 0.01344, 0.00639, 0.00822, 0.01765, 0.00431, -0.0019,
0.00204, 0.03139, 0.00251, -0.00123, -0.00833, -0.01289,
0.00716, 0.00198, 0.00263, 0.00632, 0.00179, -5e-05, 0.0088,
-0.0017, -0.00574, 0.00356, -0.02567, 0.00984, 0.00209, 0.0072,
0.00377, -0.0085, 0.01386, 0.00746, 0.00415, -0.01666, 0.00516,
0.00883, -0.01979, 0.01931, 0.00208, 0.00411, 0.01303, -0.00108,
0.00854, -0.00262, -0.02486, 0.0179, -0.00035, 0.01651, 0.00952,
0.00668, -0.00279, 0.00344, 0.00314, -0.00599, 0.00954, -0.02629,
-0.00226, 0.00282, 0.00765, 0.00624, -0.00497, -0.00269,
0.01129, -0.02122, 0.00841, -0.0011, -0.00747, 0.01913, -0.0024,
0.01528, -0.00184, -0.005, -0.01863, 0.02115, -0.00814, 0.01531,
0.00641, -0.00111, -0.01767, 0.01528, -0.00414, -0.01503,
-0.00082, 0.00429, 0.00032, 0.01733, -0.00656, 0.00176, -0.00597,
0.00423, -0.00541, 0.01085, -0.00327, -4e-04, 0.01826, -0.00852,
0.0098, -0.01052, 0.0188, 0.00234, -0.00113, -0.00335, -0.01121,
-0.00305, 0.0065, -0.0032, -0.00877, -0.01151, -0.01051,
-0.00295, 0.00751, -0.00828, -0.00164, -0.00109, 0.01665,
0.01285, -0.00875, -0.01182, -0.01427, 0.01631, -0.00241,
-0.0055, 0.00393, 0.00351, 0.00061, -0.00063, 0.01053, -0.00094,
0.00509, 0.00162, 0.00295, -0.00789, -0.03162, 0.00523, -0.00361,
-0.00171, -0.00459, 0.00237, -0.03454, -0.01103, 0.00944)), class = "data.frame", row.names = c(NA,
-281L))