heatmap - Plotting z as a color with R on a rGoogleMap -
i have function , want plot x , y. z should represented color. there package work me ?
f = function(a,b){ dnorm(a^2+b^2) } x = seq(-2, 2, 0.1) y = seq(-2, 2, 0.1) z = outer(x, y, f) persp(x, y, z) i want plot function on map generated rgooglemaps. maybe there more specific package use?
something this?

library(ggmap) # loads ggplot2 library(rgooglemaps) # getgeocode london.center <- getgeocode("london") london <- get_map("london", zoom=12) x <- seq(-2,2,0.1) df <- expand.grid(x=x,y=x) df$z <- with(df,f(x,y)) df$x <- london.center[2]+df$x/20 df$y <- london.center[1]+df$y/20 ggp <- ggmap(london)+ geom_tile(data=df,aes(x=x,y=y,fill=z), alpha=0.2)+ scale_fill_gradientn(guide="none",colours=rev(heat.colors(10)))+ stat_contour(data=df, aes(x=x, y=y, z=z, color=..level..), geom="path", size=1)+ scale_color_gradientn(colours=rev(heat.colors(10))) plot(ggp) this solution uses ggplot. perhaps else show how using rgooglemaps.
basically, load map, using get_map(...) (which wrapper getmap(...) in rgooglemaps package).
then create sample data frame df, contains 3 columns, x, y, , z, , 1 row every combination of x , y (this format required ggplot).
then create map layers. first map itself, using ggmap(...); layer of tiles "filled" based on value of z, using geom_tile(...); set of contour lines colored using value of z, using stat_contour(geom="path",...). rest of code sets fill , line colors , renders map.
purists tell you can render filled contours directly using stat_contour(geom="polygon",...), instead of using tiles, has unfortunate effect of clipping contours not enclosed in plot area.
Comments
Post a Comment