0

I am trying to figure out how to display a map including the legend with ggmap/ggplot.

I have gotten so far:

library(ggmap)
library(RColorBrewer)
bbox <- c(8.437526,47.328268,8.605915,47.462160)
map.base <- get_map(maptype='toner',source = 'stamen',location = bbox)
ggmap(map.base) +
geom_blank() +
ggtitle("2015-09-21 06:00:00 CEST") +
scale_colour_manual(values = rev(brewer.pal(7,"Spectral")), drop = FALSE)+
scale_size_manual(values=c(1:7), drop = FALSE) +
guides(color=guide_legend(title='Mean Delay [s]'), size = guide_legend(title='Mean Delay [s]'))+
ggsave(file=paste("map_","2015-09-21 060000",".png",sep=""),dpi = 100)
dev.off()

This generates the correct map in the correct bounding box. But even thought I have specified: "scale_colour_manual" and "scale_size_manual" with "drop = FALSE", no legend is appearing. How can I have the legend shown when no data is to be displayed?

The overall intention is to create a single map of a given interval in a time series. Now the problem is that some intervals have no data and so the map is displayed without a scale. If the map does not have a scale the dimensions of the map are different making it impossible to create a movie out of the different maps. That is why I need to be able to create a map WITHOUT data but WITH the legend showing.

Thank you.

Mfbaer
  • 465
  • 3
  • 15
  • 2
    You don't see a legend because you didn't call one in the `aes` of your `geom`. [See here for an example](http://stackoverflow.com/questions/34070448/manually-creating-a-legend-when-you-cant-supply-a-color-aesthetic/34072063#34072063) on how to manually add a legend to a plot. – Jaap Dec 07 '15 at 16:54
  • @ Jaap Thank you for your comment. @ rawr What do you mean? – Mfbaer Dec 07 '15 at 17:09
  • [This joran](http://stackoverflow.com/users/324364/joran) is quite active on the R tag, it's surprising to see another Joran answering R questions. – Gregor Thomas Dec 07 '15 at 17:10
  • 1
    Oh! Jorans seem to be drawn to R in some mysterious way :) Thanks for the info. – Mfbaer Dec 07 '15 at 17:12

1 Answers1

3

Taking Jaap's comment into account, that I have to call a legend in aes I have been able to achieve what I want with following code:

library(ggmap)
library(RColorBrewer)
bbox <- c(8.437526,47.328268,8.605915,47.462160)
map.base <- get_map(maptype='toner',source = 'stamen',location = bbox)
ggmap(map.base) +
geom_point(aes(x=0,y=0, color=cut(0,breaks = c(-Inf,0,60,120,240,300,360,Inf),right = FALSE), size=cut(0,breaks = c(-Inf,0,60,120,240,300,360,Inf),right = FALSE))) +
ggtitle("2015-09-21 06:00:00 CEST") +
scale_colour_manual(values = rev(brewer.pal(7,"Spectral")), drop = FALSE)+
scale_size_manual(values=c(1:7), drop = FALSE) +
guides(color=guide_legend(title='Mean Delay [s]'), size = guide_legend(title='Mean Delay [s]')) +
ggsave(file=paste("map_","2015-09-21 060000",".png",sep=""),dpi = 100)
dev.off()

I know this is not the most elegant way, but it works for now.

I basically make a dummy point outside of the bounding box to be displayed. I then give the point a value, which is cut according to the breaks I want and then colored and sized accordingly. Just remember to put the values of x and y in aes outside of the bounding box. Better solutions are welcome.

Mfbaer
  • 465
  • 3
  • 15