10

I'm trying to create a complex ggplot plot but some things don't work as expected.

I have extracted the problematic part, the creation of points and its associated legend.

library(data.table)
library(ggplot2)
lev <- c("A", "B", "C", "D") # define levels.
bb <- c(40, 30,20,10,5)/100 # define breaks.
ll <- c("40%","30%","20%","10%","5%") # labels.
# Create data
nodos <- data.table(event = c("A", "B", "D", "C", "D"), ord = c(1, 2, 3, 3, 4),
NP = c(0.375, 0.25, 0.125, 0.125, 0.125))


ggplot() + geom_point(data=nodos,aes(x=ord, 
 y=event, size=NP), color="black", shape=16) +
 ylim(lev)  + scale_size_continuous(name="Prop.",
 breaks=bb, labels=ll, range=c(0,6))+ 
 scale_x_continuous(limits=c(0.5, 4.5),
 breaks=seq(1,4,1))

enter image description here

As you can see, no matter what breaks and labels I use I'm not able to force ggplot to paint a legend containing 0% or 10%. scale_size_continuous keeps creating just two elements.
And the smaller points are very badly scaled.

I have also tried with scale_scale_area, but it doesn't work either.

I'm using R 3.4.2 and ggplot2 2.2.1 (also tried the latest github version).

How can I get it?

joran
  • 169,992
  • 32
  • 429
  • 468
skan
  • 7,423
  • 14
  • 59
  • 96
  • No packages installed. Your dataset has a variable named `evetype` and then you call varaible `event`.... – AntoniosK Nov 16 '17 at 16:55
  • I'm sorry, just a typo. – skan Nov 16 '17 at 17:06
  • Looks like it has to do with how small is the point. Very small values are not included in the legend. This trick seems to work : `ggplot() + geom_point(data=nodos,aes(x=ord, y=event, size=factor(NP)), color="black", shape=16)`. They hey is to resize based on values that can be included in the legend, but plot the correct numbers next to them. – AntoniosK Nov 16 '17 at 17:21
  • 1
    @AntoniosK with your trick it create the legend but the size of the points don't have anything to do with the original values (is not lineal). You can check it by making one very big. – skan Nov 16 '17 at 17:30
  • I agree. You can see the sizes it uses if you do `as.numeric(factor(nodos$NP))`. But, that was just to make sure that it's the value of the size that created the problem. You can probably transform your original size values to something that can be plotted and then use the correct values. – AntoniosK Nov 16 '17 at 17:37
  • 1
    @AntoniosK transforming the data doesn't seem to help. I've tried multiplying all NP by 100 but I'm still getting just 2 elements on the legend. – skan Nov 16 '17 at 19:34
  • Try to multiply by 2. For your example above this works for me: `ggplot() + geom_point(data=nodos,aes(x=ord, y=event, size=NP*2), color="black", shape=16)`. I think you should try to map your values' range [a,b] to a new [A, B] that works. Like a normalisation process. – AntoniosK Nov 17 '17 at 09:43
  • 1
    Have you tried defining the `limits` in `scale_size_continuous`? Most of your `breaks` are currently outside the default `limits`, so adding something like `limits = c(.05, .4)` will allow them to show. See if that looks more like the result you expected. – aosmith Nov 17 '17 at 22:15
  • @aosmith great, it works, I'm feeling stupid. If you convert your reply to an answer I'll vote it. – skan Nov 17 '17 at 22:32

1 Answers1

12

If you set the limits to encompass the breaks you'll be able to alter the legend. Current most of the breaks are outside the default limits of the scale.

ggplot() + 
    geom_point(data = nodos,
               aes(x = ord, y = event, size = NP), color="black", shape = 16) +
    scale_size_continuous(name = "Prop.",
                          breaks = bb,
                          limits = c(.05, .4),
                          labels = ll,
                          range = c(0, 6) )

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118