How to reverse point size in ggplot?

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


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.


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')

example plot with reversed size scale

Tags:

R

Ggplot2