0
我有一個非常惱人的問題,用ggplot2繪製橢圓,我想知道有沒有人能看到我做錯了什麼?所有點連接起來!如何在繪製橢圓時停止連接所有點的ggplot2?
我通常使用標準plot()和ordiellipse()來做到這一點,但一直試圖使用ggplot作爲可愛,但會發生這種情況。我會承認從互聯網上收集'for循環'代碼,但我看不到它中的錯誤。
這就是我得到:
我這樣做:
## The NMDS coords have been pre-calculated, and the file looks like this:
NMDS1 NMDS2 Site Species
Sample.1 -0.390516864 -0.227890627 1 Lr
Sample.2 -0.433137678 -0.221231786 1 Al
Sample.3 -0.16384753 0.243258745 1 Lt
Sample4 -0.444995027 -0.174555074 1 Ac
Sample.5 -0.569417948 -0.223738264 2 Lr
Sample.6 -0.543744761 -0.234924777 2 Lt
.......etc
# Code
all <-read.table("/Users/Dan/Downloads/all.txt", header=TRUE)
df_ell <- data.frame(x=all$NMDS1, y=all$NMDS2, group=all$Species)
for(g in levels(all$Species)){
df_ell <- rbind(df_ell, cbind(as.data.frame(with(all[all$Species==g,], ellipse(cor(NMDS1, NMDS2), scale=c(sd(NMDS1),sd(NMDS2)),
centre=c(mean(NMDS1),mean(NMDS2))))),group=g))}
# Generate plot
p <- ggplot(all, aes(NMDS1, NMDS2))
# plot it
p + geom_point(size = 4, alpha=.8, aes(colour = factor(Species), shape = factor(Site))) + geom_path(data=df_ell, aes(x=x, y=y, colour=group), size=1, linetype=1)
謝謝!當你指出這一點時,這一點非常明顯!我不敢相信我沒有看到它。很感激。 – Daniel