你能帮我做主成分分析吗?我想改变形状,这样每个物种都有不同的颜色,每个物种的2-3个生物体都有2-3个不同的符号。它应该看起来像这样:enter image description here
到目前为止,我尝试了以下代码:
setwd("~/Schwarze Johannisbeeren/SJ Wein mit nicht Sc/PCA/stackoverflow frage")
results = read.csv("results.csv", sep = ";", encoding = "UTF-8", header=TRUE, check.names=FALSE)
results.pca <- prcomp(results[,c(3:7)],
center = TRUE,
scale. = TRUE)
#grouping by organism
results.organism <- results[, 1]
#by species
results.species <- results[, 2]
summary(results.pca)
library(ggplot2)
library(ggbiplot)
ggbiplot(results.pca, alpha=0, obs.scale = 1, var.scale = 1 ,ellipse = TRUE,ellipse.prob=0.68, circle = F, varname.size=0,
var.axes = F, groups=results$species) +
theme_bw()+
geom_point(aes( colour=factor(results.species)), size=2)+
scale_shape_manual(values= c("Mt1"= 1, "Mt2" =2, "Al1"= 1, "Al2" =2, "Bg1" =1, "Bg2"=2, "Bg3" =3, "Cs1"= 1, "Cs2" =2, "Cs3" =3, "Df1"= 1, "Df2" =2, "Df3" =3))+
#scale_color_brewer(name= "organism", type = "qual", palette = 2)+
#scale_x_continuous (limits = c (-1, 9))+
theme(axis.text.x = element_text(size = 12, colour = "black", vjust = 0.5, hjust = 1, face= "bold"),
axis.title.y = element_text(size = 12, face = "bold"),
axis.title.x = element_text(size = 12, face = "bold"),
axis.text.y = element_text(colour = "black", size = 12, face = "bold"))
这就是我的数据
> results
organism species lactones cyanides alcohols ethers acids
1 Mt1 Mt 23435.167 166.4 137653.9 4040.1 1131.52
2 Mt1 Mt 23303.111 168.9 153511.0 4529.1 1148.52
3 Mt1 Mt 22340.556 176.6 150719.9 3255.8 1200.88
4 Mt2 Mt 51519.222 175.9 173401.1 3890.1 1196.12
5 Mt2 Mt 48824.500 166.5 171614.4 3694.1 1132.20
6 Mt2 Mt 50427.278 165.4 168865.1 3693.2 1124.72
7 Al1 Al 25260.222 162.0 211737.4 9563.9 1101.60
8 Al1 Al 23177.556 161.5 199886.7 10403.3 1098.20
9 Al1 Al 27903.000 156.2 240088.4 11897.1 1062.16
10 Al2 Al 5993.722 180.4 289334.9 6673.3 1226.72
11 Al2 Al 7307.389 169.7 275631.1 8333.4 1153.96
12 Al2 Al 9419.167 147.5 277924.5 9622.2 1003.00
13 Bg1 Bg 58216.944 132.4 92275.3 4099.5 900.32
14 Bg1 Bg 69860.222 147.4 105654.9 4080.6 1002.32
15 Bg1 Bg 72809.333 145.8 111731.3 4014.6 991.44
16 Bg2 Bg 51584.611 142.9 105548.2 6450.1 971.72
17 Bg2 Bg 57738.056 141.2 117728.9 6332.4 960.16
18 Bg2 Bg 53356.056 142.7 110260.2 6506.2 970.36
19 Bg3 Bg 41983.389 130.8 103799.4 4781.8 889.44
20 Bg3 Bg 46930.722 148.3 113944.6 5151.6 1008.44
21 Bg3 Bg 49487.611 139.4 121976.5 5318.3 947.92
22 Cs1 Cs 7155.056 161.6 221538.8 8356.0 1098.88
23 Cs1 Cs 8153.611 151.0 179823.0 7961.2 1026.80
24 Cs1 Cs 7445.722 168.6 176978.0 8196.5 1146.48
25 Cs2 Cs 10771.556 126.4 144314.1 8634.6 859.52
26 Cs2 Cs 12239.556 142.6 142913.7 9471.9 969.68
27 Cs2 Cs 13788.611 136.1 131506.7 9390.4 925.48
28 Cs3 Cs 12082.111 152.0 171730.0 6259.6 1033.60
29 Cs3 Cs 14331.556 143.3 141748.7 7532.8 974.44
30 Cs3 Cs 14123.056 158.2 150303.0 7755.8 1075.76
31 Df1 Df 26906.778 156.2 310203.9 5505.5 1062.16
32 Df1 Df 20689.111 163.5 214322.9 5315.6 1111.80
33 Df1 Df 22872.722 154.1 197572.9 4627.7 1047.88
34 Df2 Df 18838.222 159.2 125167.6 12372.9 1082.56
35 Df2 Df 18218.667 155.8 127077.2 11182.0 1059.44
36 Df2 Df 18545.389 156.2 154400.4 10543.6 1062.16
37 Df3 Df 19924.111 156.4 199472.6 4452.3 1063.52
38 Df3 Df 22504.056 158.0 196343.0 3994.1 1074.40
39 Df3 Df 16907.278 151.5 185052.9 4084.6 1030.20
>
顺便问一下,是否可以仅使用PC 1(x %)而不是PC 1(x %已解释变量)进行轴标记?
1条答案
按热度按时间rta7y2nd1#
实现所需结果的一种方法是,首先创建形状和颜色调色板,将微生物名称Map到形状和颜色;其次,在
geom_point
中,通过添加一列organism
(我使用dplyr::bind_cols
)来扩展数据;这样做可以将organism
Map到shape
和color
aes上;最后,去掉使用scale_color_discrete(guide = "none")
的组的颜色图例,并通过ggnewscale::new_scale_color
和scale_color_manual
:注意:轴标题的简单修复方法是使用
+labs(x = ..., y = ...)
手动设置它们。数据