2

I am trying to look at insect populations over time in a given geographic location. However, I can't figure out how ggmap will take both the insect population and date factor into consideration.

Below, you can see it is plotting according to dates 0-10. However, it's not plotting the insect abundance.

Map Results

My variables are adult (# insects) and the corresponding date ("num") and lat/long information. My code is as follows:

     num adult      lat       lon
1      1     0 42.67523 -84.48507
2      1     1 42.67524 -84.48432
3      1     2 42.67473 -84.48511
4      1     0 42.67475 -84.48432
5      2     0 42.67523 -84.48507
6      2     5 42.67524 -84.48432
7      2    10 42.67473 -84.48511
8      2     2 42.67475 -84.48432
9      3     2 42.67523 -84.48507
10     3    30 42.67524 -84.48432
11     3    50 42.67473 -84.48511
12     3     5 42.67475 -84.48432


dat$cut <- cut(dat$num, breaks=seq(0,11,1), labels=sprintf("date %d",seq(0, 10, 1), seq(1,11,1)))

tunnel <- get_map(location=c(lon=-84.484652,
                                 lat=42.675014), zoom=18, maptype='satellite')
gg <- ggmap(tunnel)
gg <- gg + stat_density2d(data=dat, aes(x=lon, y=lat, fill=..level.., alpha=..level..),
                          geom="polygon", size=0.01, bins=4)
gg <- gg + scale_fill_viridis()
gg <- gg + scale_fill_gradient('adult')
gg <- gg + scale_alpha(range=c(0.2, 0.4), guide=FALSE)
gg <- gg + coord_map()
gg <- gg + facet_wrap(~cut, ncol=4)
gg <- gg + labs(x=NULL, y=NULL, title="SWD Heat Map")
gg <- gg + theme_map(base_family="")
gg <- gg + theme(plot.title=element_text(face="bold", hjust=1))
gg <- gg + theme(panel.spacing.x=unit(0.5, "cm"))
gg <- gg + theme(panel.spacing.y=unit(0.5, "cm"))
gg <- gg + theme(legend.position="right")
gg <- gg + theme(strip.background=element_rect(fill="white", color="white"))
gg <- gg + theme(strip.text=element_text(face="bold", hjust=0))
gg <- gg + guides(fill = guide_colorbar(barwidth = 1.5, barheight = 10))
gg

Any help would be greatly appreciated!!

clemens
  • 16,716
  • 11
  • 50
  • 65
Heather
  • 21
  • 2
  • 1
    It's easier to help you if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data that we can use to actually run the code. Also, it's a good idea to learn to [avoid `attach()`](https://www.r-bloggers.com/to-attach-or-not-attach-that-is-the-question/) (though that's probably not the problem here). – MrFlick Apr 10 '17 at 21:51
  • Thank you for your comments - these changes have been made! – Heather Apr 11 '17 at 14:42
  • You can not calculate a heatmap directly like this, based on your data locations. Your data is 3D data. `stat_density2d` is for 2d data. It calculates the number (or proportion) of observation in a raster. You want to draw a 3D map. For that, you first need to create a regular raster on your area and define how you want to interpolate your data between observations and the raster grid. Look at: http://stackoverflow.com/questions/43006045/obtain-function-from-akimainterp-matrix/43064436#43064436 – Sébastien Rochette Apr 12 '17 at 11:39

0 Answers0