0

我正在对 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 - 更改点大小我收到以下错误:

Error: Incompatible lengths for set aesthetics: size

有什么建议么?谢谢。

编辑:一些 PseudoData 尝试一下:

4

1 回答 1

1

我认为这是一个一般的 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)
于 2015-06-22T20:49:10.053 回答