问题
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))
回答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