gpt4 book ai didi

r - 我如何删除GGPLOT中的两个图例之一

转载 作者:行者123 更新时间:2023-12-04 09:33:26 27 4
gpt4 key购买 nike

我有以下代码:

library(ggplot2)

df <- data.frame(iris) # iris dataset
pca <- prcomp(df[,1:4], retx=T, scale.=T) # scaled pca [exclude species col]
scores <- pca$x[,1:3] # scores for first three PC's

# k-means clustering [assume 3 clusters]
km <- kmeans(scores, centers=3, nstart=5)
ggdata <- data.frame(scores, Cluster=km$cluster, Species=df$Species)

# stat_ellipse is not part of the base ggplot package
source("https://raw.githubusercontent.com/tidyverse/ggplot2/master/R/stat-ellipse.R")


ggplot(ggdata) +
geom_point(aes(x=PC1, y=PC2, color=factor(Species)), size=5, shape=20) +
stat_ellipse(aes(x=PC1,y=PC2,fill=factor(Species)),
geom="polygon", level=0.95, alpha=0.2) +
guides(color=guide_legend("Species"),fill=guide_legend("Cluster"))


产生此结果:

enter image description here

如该图片所示,如何删除“群集”图例?

最佳答案

将您的fill指南设置为FALSE

ggplot(ggdata) +
geom_point(aes(x=PC1, y=PC2, color=factor(Species)), size=5, shape=20) +
stat_ellipse(aes(x=PC1,y=PC2, fill=factor(Species)),
geom="polygon", level=0.95, alpha=0.2)+
guides(color=guide_legend("Species"), fill = FALSE)

关于r - 我如何删除GGPLOT中的两个图例之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42686475/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com