I have a function and I want to plot only x and y. z should be represented as a color. Is there a package that does the work for 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 to plot this function on a map generated with rGoogleMaps. Maybe there is a more specific package for this use?
Something like this?
This solution uses
ggplot. Perhaps someone else will show you how to do this usingRgoogleMaps.Basically, we load the map, using
get_map(...)(which is just a wrapper forGetMap(...)in theRgoogleMapspackage).Then we create the sample data frame
df, which contains three columns,x,y, andz, and one row for every combination of x and y (this is the format required by ggplot).Then we create the map layers. First the map itself, using
ggmap(...); then a layer of tiles "filled" based on the value of z, usinggeom_tile(...); then a set of contour lines colored using the value of z, usingstat_contour(geom="path",...). The rest of the code sets the fill and line colors and renders the map.Purists will tell you that you can render the filled contours directly using
stat_contour(geom="polygon",...), instead of using tiles, but this has the unfortunate effect of clipping any contours not completely enclosed in the plot area.