我的数据集:
Taxa dn dc
Cha 10.2 -20.4
Cha 10.7 -19.7
Cha 4.9 -21.0
Cha 5.4 -20.6
Cha 8.6 -21.2
Cha 8.0 -20.9
Cha 8.1 -21.3
Cha 6.9 -21.1
Cha 8.5 -21.1
Cha 9.1 -20.8
Hyd 6.6 -19.2
Hyd 10.2 -17.0
Hyd 9.7 -18.2
Hyd 8.1 -16.5
Hyd 8.8 -15.8
Hyd 8.7 -15.8
Hyd 7.6 -18.3
Hyd 8.9 -16.0
Hyd 8.4 -17.5
Hyd 9.8 -18.8
Hyd 8.3 -18.4
Scy 9.4 -20.1
Scy 9.1 -20.0
Scy 7.8 -20.2
Scy 9.1 -17.6
Scy 8.2 -19.8
Scy 9.4 -19.2
Scy 9.0 -20.1
Sip 5.7 -15.2
Sip 6.2 -18.6
Sip 5.6 -18.0
Sip 8.6 -17.6
Sip 4.8 -16.9
Sip 5.2 -15.4
Sip 1.9 -18.4
我使用的代码是:
library(ggplot2)
ggplot(mydata, aes(x=dC, y=dN, colour=Taxa, shape=Taxa))+
geom_point(size=2, alpha=0.5)+
geom_polygon(aes(fill=Taxa, group=Taxa))+
theme(legend.position = "none")
我想在数据中绘制带有“ Taxa”的多边形组。但是,看起来多边形连接了每个点。
我想要的就是这样的。我应该如何编辑代码?
答案 0 :(得分:4)
您还可以计算凸包,将它们绘制下来:
library(ggplot2)
library(plyr)
# some fake data:
mydata <- data.frame(Taxa = c('Cha','Cha','Cha','Cha','Cha','Cha','Hyd','Hyd','Hyd','Hyd','Hyd','Hyd'),
dn = c(10.2,10.7,4.9,5.4,8.6,8.0, 6.6,10.2,9.7,8.1,8.8,8.7),
dc =c(-20.4,-19.7,-21.0,-20.6,-21.2,-20.9,-19.2,-17.0,-18.2,-16.5,-15.8,-15.8))
# calculate convex hulls:
chulls <- ddply(mydata, .(Taxa), function(mydata) mydata[chull(mydata$dn, mydata$dc), ])
# plot them:
ggplot(data=mydata, aes(x=dn, y=dc, color=Taxa)) + geom_point() +
geom_polygon(data=chulls, aes(x=dn, y=dc, fill=Taxa, alpha=0.2))
好的来源here。
答案 1 :(得分:3)