2017-03-09 61 views
2

我有一个数据框(按品种矩阵网站),看起来像这样:如何将颜色和图例添加到R中基本包中的ordiellipse?

  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%置信多边形/椭圆。我需要为每个国家(点)唯一地加上颜色代码,以及每个具有国家和图例对应颜色代码的椭圆。

#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墙壁)

+0

请输入(df)。虽然你的矩阵很小,但仍然应该以一种可以简单地复制和粘贴的形式提供示例。 – Djork

+0

我尽我所能地搭起了一个数据框。但我不确定如何在第2行 – Ash

+0

中的“rownames”中循环显示国家名称,您也可以只是“输入(df)”或其子样本,这是再现数据的好方法。 – Djork

回答

1

为基地的情节,你可以在同一个问题提供颜色矢量。

因此,对于每个点,您应该指定一种颜色,并使用此长度为n的点的颜色向量作为points中的col参数的输入。最简单的方法是使用PCA数据的国家rownames,并使用因子整数值为已定义的调色板编制索引。

同样可以提供的长度为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) 
相关问题