Irrelevant legend information in ggplot2

2019-02-13 09:38发布

问题:

When running this code (go ahead, try it):

library(ggplot2)
(myDat <- data.frame(cbind(VarX=10:1, VarY=runif(10)), 
    Descrip=sample(LETTERS[1:3], 10, replace=TRUE)))
ggplot(myDat,aes(VarX,VarY,shape=Descrip,size=3)) + geom_point()

... the "size=3" statement does correctly set the point size. However it causes the legend to give birth to a little legend beneath it, entitled "3" and containing nothing but a big dot and the number 3.

This does the same

ggplot(myDat,aes(VarX,VarY,shape=Descrip)) + geom_point(aes(size=3)) 

Yes, it is funny. It would have driven me insane a couple hours ago if it weren't so funny. But now let's make it stop.

回答1:

That's because it's interpreting it as an aesthetic mapping rather than a constant. This works I think:

ggplot(myDat,aes(VarX,VarY,shape=Descrip)) + geom_point(size=3)


标签: r ggplot2 legend