如何在R的基础包中添加ordiellipse的颜色和图例?

时间:2017-03-09 19:37:12

标签: r colors legend pca confidence-interval

我有一个数据框(按物种矩阵排列),如下所示:

            SP1      SP2     SP3     SP4
    US       5       6       2       5
    US       5       6       2       5
    UK       5       6       2       5
   AUS       5       6       2       5

我正在尝试创建一个95%置信度多边形/椭圆的PCoA图(主坐标分析)。我需要对每个国家/地区(点)进行唯一的颜色编码以及每个椭圆具有相应的国家和传说颜色代码。

#My current code
#If you  need a dataframe
df <- t(data.frame(matrix(rexp(10000, rate=10),nrow=100,ncol=100)))
rownames(df) <- rep(c("UK", "US", "Aus","Spain"),length.out=100)#I dont know how to loop this over 100 times to make it the rownames


df <- as.matrix(df[,-1]) #Use this to convert dataframe to matrix
row.names(df) <- df[,1]#Use this to convert dataframe to matrix
dat <- df
dat.db <- vegdist(dat, method = "bray")
dat.pcoa <- cmdscale(dat.db, eig = TRUE, k = 3)
explainvar1 <- round(dat.pcoa$eig[1] / sum(dat.pcoa$eig), 3) * 100
explainvar2 <- round(dat.pcoa$eig[2] / sum(dat.pcoa$eig), 3) * 100
explainvar3 <- round(dat.pcoa$eig[3] / sum(dat.pcoa$eig), 3) * 100
sum.eig <- sum(explainvar1, explainvar2, explainvar3)

# Define Plot Parameters
par(mar = c(5, 5, 1, 2) + 0.1)
# Initiate Plot
plot(dat.pcoa$points[ ,1], dat.pcoa$points[ ,2],
     xlab = paste("PCoA 1 (", explainvar1, "%)", sep = ""),
     ylab = paste("PCoA 2 (", explainvar2, "%)", sep = ""),
     pch = 16, cex = 2.0, type = "n", cex.lab = 1.5, cex.axis = 1.2, axes = FALSE)
axis(side = 1, labels = T, lwd.ticks = 2, cex.axis = 1.2, las = 1)
axis(side = 2, labels = T, lwd.ticks = 2, cex.axis = 1.2, las = 1)
abline(h = 0, v = 0, lty = 3)
box(lwd = 2)
points(dat.pcoa$points[ ,1], dat.pcoa$points[ ,2],
       pch = 19, cex = 1, bg = "gray", col = "grey")
ordiellipse(ord = dat.pcoa, groups = rownames(dat), kind = "se",conf = .95,col = NULL)

注意:这与发布的问题不同here.这个问题只询问如何在基础包中执行ordiplot(因为我已经找到了ggplot2的墙)

1 个答案:

答案 0 :(得分:1)

对于基础图,您可以提供颜色矢量。

因此,对于每个点,您应该指定一种颜色,并使用长度为n个点的颜色矢量作为points中col参数的输入。最简单的方法是计算PCA数据的国家/地区名称,并使用因子整数值来索引定义的调色板。

同样,您可以提供长度为n组的颜色向量ordiellipse

以下是基于您的示例df的代码:

library(vegan)
df <- t(data.frame(matrix(rexp(10000, rate=10),nrow=100,ncol=100)))
rownames(df) <- rep(c("UK", "US", "Aus","Spain"), length.out=100)#I dont know how to loop this over 100 times to make it the rownames
colnames(df) <- paste0("SP", 1:ncol(df))

现在我们考虑国家rownames

# factor country and set levels
col_vector <- factor(rownames(dat.pcoa$points), levels=unique(rownames(dat.pcoa$points)))
col_vector

# see integer values of your factored countries and the given order
str(col_vector) 
# Factor w/ 4 levels "UK","US","Aus",..: 1 2 3 4 1 2 3 4 1 2 ...

使用因式国家/地区名称

创建调色板和索引
# palette() is the default R color palette or you can specify a vector of 4 colors
col_palette <- palette()[col_vector]
col_palette

然后在你的情节中使用这些值

par(mar = c(5, 5, 1, 2) + 0.1)
# Initiate Plot
plot(dat.pcoa$points[ ,1], dat.pcoa$points[ ,2],
     xlab = paste("PCoA 1 (", explainvar1, "%)", sep = ""),
     ylab = paste("PCoA 2 (", explainvar2, "%)", sep = ""),
     pch = 16, cex = 2.0, type = "n", cex.lab = 1.5, cex.axis = 1.2, axes = FALSE)
axis(side = 1, labels = T, lwd.ticks = 2, cex.axis = 1.2, las = 1)
axis(side = 2, labels = T, lwd.ticks = 2, cex.axis = 1.2, las = 1)
abline(h = 0, v = 0, lty = 3)
box(lwd = 2)
points(dat.pcoa$points[ ,1], dat.pcoa$points[ ,2],
   pch = 19, cex = 1, bg = "gray", col = col_palette)
ordiellipse(ord = dat.pcoa, groups = col_vector, kind = "se", conf = .95, col = unique(col_palette)) 

在这里,我们检查标签是否与指定的颜色匹配

# sanity check that point colors match label
text(dat.pcoa$points[ ,1], dat.pcoa$points[ ,2], labels=rownames(dat.pcoa$points), col = col_palette) 
# sanity check that ellipse color match labels
ordiellipse(ord = dat.pcoa, groups = col_vector, kind = "se", conf = .95, col = unique(col_palette), label=TRUE) 

enter image description here enter image description here

最后添加一个传奇

legend('topright', legend=unique(col_vector), col=unique(col_palette), pch = 16)