How to reverse point size in ggplot?

巧了我就是萌 提交于 2021-02-15 19:21:06

问题


Please, help me with this point. I need the positive values to be represented as small points and negative as large points. If I tape minus before size, the point sizes are right but the legend is changing:

df=data.frame(x=rnorm(20),y=runif(20),z=rnorm(20))
ggplot(df,aes(x=x,y=y))+geom_point(aes(size=-z))

so that does not suite.


回答1:


One solution would be to use scale_size() and set your own breaks and then labels in opposite direction. Changed range of z values to get better representation.

df=data.frame(x=rnorm(20),y=runif(20),z=(-13:6))
ggplot(df,aes(x=x,y=y))+geom_point(aes(size=-z))+
  scale_size("New legend",breaks=c(-10,-5,0,5,10),labels=c(10,5,0,-5,-10))

enter image description here




回答2:


Little late, but an easier way would just to add trans='reverse' to scale_size.

Example:

df=data.frame(x=rnorm(20),y=runif(20),z=z=(-13:6))
ggplot(df,aes(x=x,y=y))+
geom_point(aes(size=z)) +
scale_size(trans = 'reverse')




回答3:


While this question is very old - and has an accepted answer - the comment from baptiste that suggests using last_plot() + scale_size(range = c(5,1)) + guides(size = guide_legend(reverse=TRUE)) works very elegantly and simply. For my data where I needed to produce the same result as OP, this worked with zero modification required.



来源:https://stackoverflow.com/questions/14307205/how-to-reverse-point-size-in-ggplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!