Search code examples
rggplot2ggmap

ggmap and scale_colour_brewer


I am trying to plot a frequency over a map obtained with ggmap in R. The idea is that I would have a plot of the frequency on each coordinates set. The frequency ("freq") would be mapped to six and a color scale. The data looks like this:

         V7       V6 freq
1   42.1752 -71.2893    1
2   42.1754 -71.2893    1
3   42.1755 -71.2901    2
4   42.1755 -71.2893    1
5   42.1756 -71.2910    1
6   42.1756 -71.2907    1
7   42.1756 -71.2906    1
8   42.1756 -71.2905    1
9   42.1756 -71.2901    1
10  42.1756 -71.2899    2
11  42.1756 -71.2897    2
12  42.1756 -71.2894    2
13  42.1757 -71.2915    1
14  42.1757 -71.2910    1

Here is the code I am using:

ggmap(newmap2) + 
geom_point(aes(x = coordfreq$V7, y = coordfreq$V6), 
           data = coordfreq, alpha = 1/sqrt(coordfreq$freq),
           colour = coordfreq$freq, size = sqrt(coordfreq$freq)) +
scale_colour_brewer(palette = "Set1")

I only get the color mapped to "freq", but I cannot get the scale_colour_brewer to work. I have tried several arguments to scale_color_brewer to no available.


Solution

  • Your code created a map without data points. Here is possibly what you are after. A few things. One is that you do not have to type x = coordfreq$V7. You can just type x = V7. The same applies to other similar cases in your code. Another is that colour should be in aes() in your case. Another thing is that freq is numeric. You need it as either factor or character when you assign colours to your graphic. The other is that freq is a function. You want to void such a name. Hope this will help you.

    library(ggmap)
    library(ggplot2)
    
    # This get_map code was suggested by an SO user. Sadly, the edit was rejected.
    # Credit to him/her (MichaelVE).
    
    newmap2 <- get_map(location = c(lon = -71.2893, lat = 42.1752), 
                       zoom = 17, maptype = 'terrain')
    
    ggmap(newmap2) +
    geom_point(data = mydf2, aes(x = V6, y = V7, colour = factor(frequency), size = sqrt(frequency))) +
    scale_colour_brewer(palette ="Set1")
    

    enter image description here