2

I am trying to build a map which includes points of different color and size. I managed to add a legend for the color, but I cannot figure why the size scale doesn't show up. I made 3 attempts, each of them didn't lead to what I want.

This is the first part of my code with simplified data :

library(ggmap)
library(ggplot2)

###building data###
HK <- get_map(c(lon=114.1408686,lat=22.3593252), zoom=11)
map <- ggmap(HK, darken = c(0.5, "white"))
insignificantFrac <- 0.05
Records <- data.frame(lon=c(114.1288, 114.0235, 113.9876, 114.2219, 114.1809, 113.9933, 114.0798, 114.1812),
                        lat=c(22.23539, 22.29873, 22.20123, 22.24567, 22.30987, 22.37298, 22.34234, 22.26428),
                        pointColor=c('#AAFF00FF', '#F00000FF', '#FFE500FF', '#FFD100FF', '#00FF00FF', '#B3FF00FF', '#FFE200FF', '#FF2F00FF'),
                        pointSize=c(1,2,3,2,2,1,3,4))
legendPoints <- data.frame(labels=c('100+','10-99','1-9'), sizes=c(3,2,1), lon=c(0,0,0), lat=c(0,0,0))

###drawing the map###
x11()
transactionsMap <- map + geom_point(data=Records, aes(x=lon, y=lat), colour=Records$pointColor, size=Records$pointSize)
transactionsMap <- transactionsMap + ggtitle("HK Map") + xlab("") + ylab("") 
transactionsMap <- transactionsMap + scale_colour_gradient2(breaks=c(5000, 11000, 20000), name='Value \n', 
                                labels=c(paste(insignificantFrac/2*100,'% : ', round(5000)),paste('50% : ', round(11000)),paste((1-insignificantFrac/2)*100,'% : ', round(20000), '+')), 
                                low='green', mid='yellow', high='red', limits=c(5000-1000, 20000+500), midpoint=11000)
#second legend line here
transactionsMap

At this point, I got something like this :

enter image description here

So I tried this three following lines. For the first 2 attempts, the plot doesn't change. For the third one, I cannot control the sizes, the number, the name nor the labels of the legend:

enter image description here

transactionsMap <- transactionsMap + scale_size_manual(values=legendPoints$size, name="Volume")
transactionsMap <- transactionsMap + scale_size_area(name='Volume', breaks=c(3,2,1), labels=legendPoints$labels)
transactionsMap <- transactionsMap + geom_point(data=legendPoints, aes(x=lon, y=lat,  size=sizes))
Ricardo Oliveros-Ramos
  • 4,322
  • 2
  • 25
  • 42
OIE66
  • 23
  • 2

1 Answers1

1

If you combine your last two:

transactionsMap  +
  geom_point(data=legendPoints, aes(x=lon, y=lat,  size=sizes)) +
  scale_size_area(name='Volume', breaks=c(3,2,1), labels=legendPoints$labels) 

enter image description here

is that what you were after?


Edit - to make the sizes on the legend match those in your map, add in your scale_size_area call:

scale_size_area(..., max_size=max(legendPoints$size))
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
  • Hi, It is quite close to what I want to do but on your plot, the size of the scale points (seems to be 3,4,5) are bigger than those on the map (1,2,3). Is it a way to control this ? – OIE66 Jul 22 '15 at 05:45
  • Yep - use `max_size=max(legendPoints$size)` in your `scale_size_area` call - updated answer – mathematical.coffee Jul 22 '15 at 06:30