2

我有一个数据框,其中包含在两个不同年份(“treat.year”列)和一些环境变量进行的治疗。我想制作 PCA 的散点图,但我不知道如何更改点的颜色。

我希望给定处理中的点(C 或 Ca)在两个采样周期(例如,所有 C 点为蓝色,所有 Ca 点为绿色,无论年份如何)和连接“框架”的颜色相同根据治疗和年份的点(即仅连接 C.2009 点的框架,另一个仅连接 C.2015 点的框架,依此类推)。

ex <- data.frame(treat.year = c("C.2015", "C.2015", "C.2015", "C.2015", "Ca.2015", "Ca.2015", "Ca.2015", "Ca.2015", "C.2009", "C.2009", "C.2009", "C.2009", "Ca.2009", "Ca.2009", "Ca.2009", "Ca.2009"),
                 var.b = c(33, 18, 34, 28, 12, 11, 15, 13, 45, 52, 35, 39, 29, 27, 30, 27),
                 var.e = c(1.1, 0.5, 1.3, 1.2, 0, 0, 0, 0, 1.06, 0.96, 1.06, 0.58, 0.1, 0, 0, 0),
                 var.g = c(83.2, 53.6, 94.9, 80.8, 25.6, 29.2, 31.9, 29.8, 102.3, 83.9, 158.7, 126.6, 78.5, 186.9, 82.4, 62))

PCA1 <- prcomp(ex[ , 2:4], scale = TRUE)

library(ggfortify)
autoplot(PCA1, data = ex, colour = "treat.year", loadings = TRUE, 
         loadings.label = TRUE, frame = TRUE)

在此处输入图像描述

4

1 回答 1

1

最好将“treat.year”分成两列。然后你可以单独改变颜色

ex <- data.frame(treat.year = c("C.2015", "C.2015", "C.2015", "C.2015", "Ca.2015", "Ca.2015", "Ca.2015", "Ca.2015", "C.2009", "C.2009", "C.2009", "C.2009", "Ca.2009", "Ca.2009", "Ca.2009", "Ca.2009"),
                treat = c("C", "C", "C", "C", "Ca", "Ca", "Ca", "Ca", "C", "C", "C", "C", "Ca", "Ca", "Ca", "Ca"),
                 year = c("2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015", "2009", "2009", "2009", "2009", "2009", "2009", "2009", "2009"),
                 var.b = c(33, 18, 34, 28, 12, 11, 15, 13, 45, 52, 35, 39, 29, 27, 30, 27),
                 var.e = c(1.1, 0.5, 1.3, 1.2, 0, 0, 0, 0, 1.06, 0.96, 1.06, 0.58, 0.1, 0, 0, 0),
                 var.g = c(83.2, 53.6, 94.9, 80.8, 25.6, 29.2, 31.9, 29.8, 102.3, 83.9, 158.7, 126.6, 78.5, 186.9, 82.4, 62))

PCA1 <- prcomp(ex[ , 2:4], scale = TRUE)

library(ggfortify)
autoplot(PCA1, data = ex, colour = "year", 
         loadings.label = T,  frame.colour="treat.year", frame=T)+theme(legend.position = "none")
于 2021-08-17T21:41:32.810 回答