Search code examples
rggplot2customizationlegend

Customize legend for continuous size in ggplot2


I was trying to emphasize some specific size as a reference in my legend. I tried scale_size_continuous and scale_size_manual without any luck. Below is a toy example, is there anyone who can help with it?

set.seed(926)
dat.toy <- data.frame(x1 = rnorm(100),
                      x2 = rnorm(100),
                      x3 = factor(sample(1:5,100,replace = T)),
                      x4 = factor(sample(1:4,100,replace = T)))
require(ggplot2)

ggplot(dat.toy,aes(x=x3,y=x4,colour = x1, size = x2)) + 
  geom_point()

Plot

The desired result should be able to customize (amplify or shrink) the size of the circle, add the size of the circle for -3, delete (hide) the size of the circle for 2 in the legend.

Thanks in advance.


Solution

  • You could try something like this ?

    require(ggplot2)
    
    ggplot(dat.toy,aes(x=x3,y=x4, color = x1)) + 
      geom_point(aes(size=x2))+
      geom_jitter()+
      scale_size(range = c(-2,4))
    

    enter image description here