I am doing a PCA on plots in 2 habitat types in which I collected data on multiple environmental variables. I was able to change the colors of the points from the ggbiplot defaults. I want the size of each point to depend on canopy cover in that plot, and I was able to do that by:
point.size = df$canopy.cover * 0.1
where df$canopy.cover
values range from 0-100 and 0.1 because a canopy cover of 100%=point size 10.
The problem: I can't maintain the colors associated with the two groups after changing the size of points. Using the following pseudo-data:
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)
I get something similar to:
When I replace:
geom_point(size=point.size)
with:
geom_point(aes(colour=habitat), size=point.size)
as per ggbiplot - change the point size I get the following following error:
Error: Incompatible lengths for set aesthetics: size
Any suggestions? Thanks.
EDIT: some PseudoData to try it out with:
This is a general ggplot2 syntax issue, I think, as it occurs with simpler examples (e.g., ggplot(mtcars) + geom_point(aes(disp, hp, color=cyl), size =mtcars$model)
)
But, I think this will work for you:
geom_point(aes(X1, X2, color=habitat, size = point.size)) + scale_size_identity()
So the full answer with the code in the question is (from Emilio's comment below):
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)