ggbiplot:更改点大小后如何保持组颜色?

ggbiplot: how to maintain group colors after changing point size?

我正在对 2 种栖息地类型的地块进行 PCA,我在其中收集了多个环境变量的数据。我能够从 ggbiplot 默认值更改点的颜色。我希望每个点的大小取决于该地块中的树冠覆盖,我能够通过以下方式做到这一点:

point.size = df$canopy.cover * 0.1  

其中 df$canopy.cover 值范围为 0-100 和 0.1,因为 100% 的树冠覆盖率 = 磅值 10。

问题:更改点的大小后,我无法保持与两组相关的颜色。使用以下伪数据:

env.vars<-data.frame(replicate(5,sample(0:10,20,rep=TRUE)))

cover<-c(89, 92, 72, 53, 88, 89, 71, 83, 71, 66, 23, 30,  5, 15, 57, 54,0, 23, 9, 16)

habitat<-c("habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2")

point.size<-cover*0.1

nest.env.pca <- prcomp(env.vars, center = TRUE, scale. = TRUE) 

g <- ggbiplot(nest.env.pca, obs.scale = 1, var.scale = 1, 
     group = habitat, ellipse = TRUE, 
     circle = TRUE, varname.size=3)+
     scale_colour_manual(values=c("blue", "red")) +
     geom_point(size=point.size)

print(g)

我得到类似于:

当我替换时:

geom_point(size=point.size)

与:

geom_point(aes(colour=habitat), size=point.size)

根据 ggbiplot - change the point size 我收到以下错误:

Error: Incompatible lengths for set aesthetics: size

有什么建议吗?谢谢。

编辑:一些用于尝试的伪数据:

我认为这是一个普遍的 ggplot2 语法问题,因为它出现在更简单的示例中(例如,ggplot(mtcars) + geom_point(aes(disp, hp, color=cyl), size =mtcars$model)

但是,我认为这对你有用:

geom_point(aes(X1, X2, color=habitat, size = point.size)) + scale_size_identity()

所以问题中代码的完整答案是(来自以下 Emilio 的评论):

env.vars<-data.frame(replicate(5,sample(0:10,20,rep=TRUE)))

cover<-c(89, 92, 72, 53, 88, 89, 71, 83, 71, 66, 23, 30,  5, 15, 57, 54,0, 23, 9, 16)

habitat<-c("habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2", "habitat1", "habitat2")

point.size<-cover*0.1

nest.env.pca <- prcomp(env.vars, center = TRUE, scale. = TRUE) 

g <- ggbiplot(nest.env.pca, obs.scale = 1, var.scale = 1, 
     group = habitat, ellipse = TRUE, 
     circle = TRUE, varname.size=3)+
     scale_colour_manual(values=c("blue", "red")) +
     geom_point(aes(color=habitat, size = point.size)) + scale_size_identity() 

print(g)