4

I'm mapping size to a variable with something like a log distribution - mostly small values but a few very large ones. How can I make the legend display custom values in the low-value range? For example:

df = data.frame(x=rnorm(2000), y=rnorm(2000), v=abs(rnorm(2000)^5))
p = ggplot(df, aes(x, y)) + 
  geom_point(aes(col=v, size=v), alpha=0.75) +
  scale_size_area(max_size = 10)
print(p)

I've tried p + guides(shape=guide_legend(override.aes=list(size=8))) solution posted in this SO question, but it makes no difference in my plot. In any case I'd like to use specific legend size values e.g. v = c(10,25,50,100,250,500) instead of the default range e.g. c(100,200,300,400)..

Grateful for assistance.

enter image description here

Community
  • 1
  • 1
geotheory
  • 22,624
  • 29
  • 119
  • 196

1 Answers1

5

To get different break points of size in legend, modify scale_size_area() by adding argument breaks=. With breaks= you can set breakpoints at positions you need.

ggplot(df, aes(x, y)) + 
  geom_point(aes(col=v, size=v), alpha=0.75) +
  scale_size_area(max_size = 10,breaks=c(10,25,50,100,250,500))
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201