R 使用ggplot,我如何在散点图中框定属于一个组的点?
在我的数据中,我将每个观察结果归为一组。我想在散点图中绘制数据,并将每组框起来以显示差异。据我所知,如果使用Kmens、pam或clara的clusteres数据,R中有几个函数可以这样做(例如ggfortify包中的autoplot(),factoextra包中的fviz_cluster()或cluster包中的clustlot())。例如,我使用iris数据集和ggfortify包R 使用ggplot,我如何在散点图中框定属于一个组的点?,r,ggplot2,k-means,R,Ggplot2,K Means,在我的数据中,我将每个观察结果归为一组。我想在散点图中绘制数据,并将每组框起来以显示差异。据我所知,如果使用Kmens、pam或clara的clusteres数据,R中有几个函数可以这样做(例如ggfortify包中的autoplot(),factoextra包中的fviz_cluster()或cluster包中的clustlot())。例如,我使用iris数据集和ggfortify包 library(ggplot2) library(ggfortify) #dimension reducti
library(ggplot2)
library(ggfortify)
#dimension reduction
IrisPrinComp <- princomp(formula=~Sepal.Length +Sepal.Width +Petal.Length +Petal.Width, data = iris )
IrisWithPrinComp <- cbind(iris, IrisPrinComp$scores)
IrisWithPrinComp$FactorSpecies <- factor(IrisWithPrinComp$Species)
#Plot
ggplot(data=IrisWithPrinComp, aes(x=Comp.1, y=Comp.2, color=FactorSpecies))+geom_point()
库(ggplot2)
图书馆(GGF)
#降维
IrisPrinComp您可以从ggalt
包中使用geom_encycle
库(dplyr)
图书馆(GG2)
图书馆(tibble)
#devtools::install_github(“hrbrmstr/ggalt”)
图书馆(ggalt)
princomp(公式=~萼片长度+萼片宽度+花瓣长度+花瓣宽度,
数据=iris)%>%
强化()%>%
添加列(因子种类=因子(iris$种类))%>%
ggplot(aes(x=成分1,y=成分2,颜色=因子种类))+
几何点()+
几何环(aes(组=因子种类))
您可以使用ggalt
软件包中的geom\u encycle
库(dplyr)
图书馆(GG2)
图书馆(tibble)
#devtools::install_github(“hrbrmstr/ggalt”)
图书馆(ggalt)
princomp(公式=~萼片长度+萼片宽度+花瓣长度+花瓣宽度,
数据=iris)%>%
强化()%>%
添加列(因子种类=因子(iris$种类))%>%
ggplot(aes(x=成分1,y=成分2,颜色=因子种类))+
几何点()+
几何环(aes(组=因子种类))
你有什么问题?你有什么问题?
Irisclusters <- kmeans(x = IrisWithPrinComp[,c("Comp.1", "Comp.2")], centers = 3, iter.max = 20, nstart = 5)
IrisWithPrinComp$Cluster <- factor(Irisclusters$cluster)
autoplot(Irisclusters, data = IrisWithPrinComp, frame=T)