0

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.

PSraj
  • 229
  • 4
  • 10
  • I assume data_ny$clicks is with shiny? to make it work you would need to use aes_string() – MLavoie Jan 27 '16 at 21:45
  • I am still not sure about how to replace this, so basically you are telling substituting aes() by aes_string() would work ? – PSraj Jan 27 '16 at 22:09
  • here is an example http://stackoverflow.com/questions/28483389/r-shiny-error-object-input-not-found/28483695#28483695 – MLavoie Jan 27 '16 at 22:24

1 Answers1

3

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

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for this, it works, just another question, can we change colors as well based on let say "city.x" column ? this data set has just one value but the entire data set would have more values. – PSraj Jan 27 '16 at 23:07
  • I don't see why not. I tested by assigning `color` to this new variable: `data_ny$Up.v.Down <- as.numeric(factor( data_ny$latitude.x > mean(data_ny$latitude.x)))` – IRTFM Jan 27 '16 at 23:29