12

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
  • try last_plot() + scale_size(range = c(5,1)) + guides(size = guide_legend(reverse=TRUE))
    – baptiste
    Commented Jan 13, 2013 at 19:52

3 Answers 3

14

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

1
8

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.