0

I have a dataset with the ff columns: (1) latitude of locations; (2) longitude; (3) population_count; (4) sample_count; (5) sample_perc (sample/ pop).

I need to visualise the sample_perc (col 5) over the ggmap output of the city as a heatmap. The problems I'm encountering are:

  • Not all coordinates in the map are relevant, and hence do not have data
  • Heatmap (geom_density) uses entries (rows) in the data and can't do aggregate (as far as I know)

Code:

amsmap <- get_map(location="amsterdam", zoom=14)

ggmap(amsmap) + 
      geom_density2d(data = dummy$sample_perc, 
        aes(x =request_lng, y = request_lat), size = 0.3) + 
      stat_density2d(data = dummy$sample_perc, 
        aes(x = request_lng, y = request_lat, fill = ..level.., 
        alpha = ..level..), size = 0.01, bins = 16, geom = "polygon") + 
      scale_fill_gradient(low = "green", high = "red") + 
      scale_alpha(range = c(0, 0.3), guide = FALSE)

error: ggplot2 doesn't know how to deal with data of class factor

How can I successfully do this? Help please.

tiffinie
  • 33
  • 1
  • 6
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Show the code you tried. – MrFlick May 23 '18 at 14:51
  • Added more info – tiffinie May 23 '18 at 15:02
  • 1
    Please provide inline data via e.g. `dput`, not a temporary Google drive link! Thanks. –  May 23 '18 at 15:42
  • 1
    Your first problem is that the `data` argument to `geom_density2d` should be a data frame, not a `factor`, as the error is telling you. So you want `data = dummy`, assuming dummy is your data frame with columns `request_lat`, `request_lng`. –  May 23 '18 at 15:43

0 Answers0