2017-03-09 105 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) 
相關問題