Search code examples
rggplot2ggmap

Unable to set size with ggmap


I m struggling to set size of points based on a parameter value "clicks" in my case, Ideally size command should work but its not working for me somehow.Its throwing me error Error: Incompatible lengths for set aesthetics: size Here is my effort :

 library(ggmap)
    library(ggplot2)
    map <- get_googlemap(center = c(lon = -73.99, lat = 40.75), color = "bw", scale = 2,zoom=12)
   x <- ggmap(map,extent = "device") +geom_point(aes(longitude.x, latitude.x,colour=factor(geo_region)), 
                                              data=data_ny, na.rm=T,size=3)+ scale_size_continuous(range =range(data_ny$clicks))
x

The DPUT is :

structure(list(geo_region = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "NY", class = "factor"), 
    io_id = c(262046L, 262045L, 262048L, 262046L, 262048L, 262048L, 
    262045L, 262046L, 262046L, 262048L, 262048L, 262046L, 262046L, 
    262048L, 262048L, 262046L, 262046L, 262048L, 262046L, 262048L, 
    262045L, 262046L, 262048L, 262045L, 262048L, 262045L, 262046L, 
    262048L, 262045L, 262046L, 262045L, 262048L, 262046L, 262048L, 
    262045L, 262046L, 262048L, 262045L, 262046L, 262046L, 262045L, 
    262048L, 262046L, 262045L, 262048L, 262045L, 262048L, 262046L, 
    262046L, 262048L), city.x = c("new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york", "new york", "new york", "new york", 
    "new york", "new york"), latitude.x = c(40.75, 40.75, 40.75, 
    40.72, 40.72, 40.73, 40.73, 40.73, 40.7, 40.7, 40.71, 40.71, 
    40.71, 40.71, 40.71, 40.71, 40.71, 40.71, 40.72, 40.72, 40.72, 
    40.74, 40.74, 40.74, 40.74, 40.74, 40.74, 40.72, 40.72, 40.72, 
    40.72, 40.72, 40.72, 40.73, 40.73, 40.73, 40.75, 40.75, 40.75, 
    40.75, 40.75, 40.75, 40.76, 40.76, 40.76, 40.76, 40.76, 40.76, 
    40.76, 40.76), longitude.x = c(-73.99, -73.99, -73.99, -73.99, 
    -73.99, -73.99, -73.99, -73.99, -74.01, -74.01, -74.01, -74.01, 
    -74.01, -74.01, -74.01, -74.01, -74.01, -74.01, -73.98, -73.98, 
    -73.98, -73.99, -73.99, -73.99, -74, -74, -74, -74, -74, 
    -74, -74, -74, -74, -74.01, -74.01, -74.01, -73.98, -73.98, 
    -73.98, -73.97, -73.97, -73.97, -73.99, -73.99, -73.99, -73.99, 
    -73.99, -73.99, -73.98, -73.98), clicks = c(30L, 0L, 9L, 
    12L, 3L, 15L, 0L, 30L, 3L, 4L, 4L, 22L, 2L, 0L, 4L, 3L, 0L, 
    0L, 15L, 14L, 0L, 10L, 3L, 0L, 24L, 0L, 44L, 9L, 0L, 11L, 
    0L, 11L, 11L, 8L, 0L, 11L, 20L, 0L, 49L, 24L, 0L, 12L, 13L, 
    0L, 15L, 0L, 14L, 30L, 4L, 4L)), .Names = c("geo_region", 
"io_id", "city.x", "latitude.x", "longitude.x", "clicks"), row.names = c(NA, 
50L), class = "data.frame")

This is the sample output i get if i fix Size = 4 enter image description here

But i want size to be dynamic based on clicks. Ideally size=data_ny$clicks should work, but its not, can anybody help me fix this trivial issue.


Solution

  • You had an errors in the names and I thought that adding an alpha (transparency) specification would help resolve issues of nearly adjacent overlapping points with different numbers of clicks:

     x <- ggmap(map,extent = "device") + 
         geom_point(data=data_ny, aes(x=longitude.x, y=latitude.x), 
                    color="blue", alpha=0.3,    
                    size=na.omit(data_ny$clicks), na.rm=TRUE ) + 
          scale_size_continuous(range =range(data_ny$clicks))
     png(); print(x); dev.off()
    

    enter image description here