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牆壁)
請輸入(df)。雖然你的矩陣很小,但仍然應該以一種可以簡單地複製和粘貼的形式提供示例。 – Djork
我盡我所能地搭起了一個數據框。但我不確定如何在第2行 – Ash
中的「rownames」中循環顯示國家名稱,您也可以只是「輸入(df)」或其子樣本,這是再現數據的好方法。 – Djork