4

I am trying to both edit the legend for the size aesthetic and set the scale size for that aesthetic, but can't seem to do both.

Here are the steps:

#create the map  
library(ggplot2)
world <- broders("world")

#generate a dataframe of geolocated data with attributes
points <- data.frame(lat = seq(0, 50, 10), 
                         lon = seq(0, 50, 10),
                         type = c("y", "n", "y", "n", "y", "n"), 
                         impact.x.r = seq(0, 50, 10))
points
lat lon type impact.x.r
1   0   0    y          0
2  10  10    n         10
3  20  20    y         20
4  30  30    n         30
5  40  40    y         40
6  50  50    n         50

I then map the points in ggplot - everything works:

ggplot() + 
  world + 
  geom_point(data = points, aes(x = lon, y = lat, col = type, size = impact.x.r)) + 
  scale_color_discrete(name = "types of birds", breaks = c("n","y"), labels = c("no", "yes")) +
  scale_size_continuous(name = "impact")

working ggplot code

However as soon as i try and set the scale size using: + scale_size(), i get an error. I also lose the formatting that i did for the size aesthetic legend:

ggplot() + 
  world + 
  geom_point(data = points, aes(x = lon, y = lat, col = type, size = impact.x.r)) + 
  scale_color_discrete(name = "types of birds", breaks = c("n","y"), labels = c("no", "yes")) +
  scale_size_continuous(name = "impact") + 
  scale_size(range = c(0,15))

Scale for 'size' is already present. Adding another scale for 'size', which
will replace the existing scale.

ggplot code broken by adding size_range()

If i switch the order of the scale_size_continuous() and scale_size() calls, I still get the error. This way around I keep the formatting but lose the scale re-size:

ggplot() + 
  world + 
  geom_point(data = points, aes(x = lon, y = lat, col = type, size = impact.x.r)) + 
  scale_color_discrete(name = "types of birds", breaks = c("n","y"), labels = c("no", "yes")) +
  scale_size(range = c(0,15)) +
  scale_size_continuous(name = "impact")
 Scale for 'size' is already present. Adding another scale for 'size', which
will replace the existing scale.    

Another piece of broken ggplot code

So its using the last call to scale_size...

But how do I amend the legend for the size aesthetic and set the scale_size to something that fits my map?

MorrisseyJ
  • 1,191
  • 12
  • 19
  • 3
    Do you just want to do `scale_size_continuous(range = c(0,15), name = "impact")`? You can only have one size scale. I don't quite understand why you need the second.. – Axeman Dec 01 '18 at 01:05
  • @Axeman: Yup, that solves it. Thanks for the simple answer. I hadn't realized scale was an option within `scale_size_continuous()`. – MorrisseyJ Dec 01 '18 at 01:11

0 Answers0