title: Average values of a point dataset to a grid dataset
body: <p>I am relatively new to ggplot, so please forgive me if some of my problems are really simple or not solvable at all.</p>
  
  <p>What I am trying to do is generate a "Heat Map" of a country where the filling of the shape is continous. Furthermore I have the shape of the country as <code>.RData</code>. I used <a href="https://stat.ethz.ch/pipermail/r-sig-geo/2009-March/005305.html" rel="nofollow noreferrer">hadley wickham's script</a> to transform my SpatialPolygon data into a data frame. The long and lat data of my data frame now looks like this</p>
  
  <pre><code>head(my_df)
  long        lat         group
  6.527187    51.87055    0.1 
  6.531768    51.87206    0.1
  6.541202    51.87656    0.1
  6.553331    51.88271    0.1
  </code></pre>
  
  <p>This long/lat data draws the outline of Germany. The rest of the data frame is omitted here since I think it is not needed. I also have a second data frame of values for certain long/lat points. This looks like this</p>
  
  <pre><code>my_fixed_points
  long        lat         value
  12.817      48.917      0.04 
  8.533       52.017      0.034
  8.683       50.117      0.02
  7.217       49.483      0.0542
  </code></pre>
  
  <p>What I would like to do now, is colour each point of the map according to an average value over all the fixed points that lie within a certain distance of that point. That way I would get a (almost)continous colouring of the whole map of the country. 
  What I have so far is the map of the country plotted with ggplot2</p>
  
  <pre><code>ggplot(my_df,aes(long,lat)) + geom_polygon(aes(group=group), fill="white") + 
  geom_path(color="white",aes(group=group)) + coord_equal()
  </code></pre>
  
  <p>My first Idea was to generate points that lie within the map that has been drawn and then calculate the value for every generated point <code>my_generated_point</code> like so</p>
  
  <pre><code>value_vector &lt;- subset(my_fixed_points, 
    spDistsN1(cbind(my_fixed_points$long, my_fixed_points$lat),  
    c(my_generated_point$long, my_generated_point$lat), longlat=TRUE) &lt; 50, 
    select = value)
  point_value &lt;- mean(value_vector)
  </code></pre>
  
  <p>I havent found a way to generate these points though. And as with the whole problem, I dont even know if it is possible to solve this way. My question now is if there exists a way to generate these points and/or if there is another way to come to a solution.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>Thanks to Paul I almost got what I wanted. Here is an example with sample data for the Netherlands.</p>
  
  <pre><code>library(ggplot2)
  library(sp)
  library(automap)
  library(rgdal)
  library(scales)
  
  #get the spatial data for the Netherlands
  con &lt;- url("http://gadm.org/data/rda/NLD_adm0.RData")
  print(load(con))
  close(con)
  
  #transform them into the right format for autoKrige
  gadm_t &lt;- spTransform(gadm, CRS=CRS("+proj=merc +ellps=WGS84"))
  
  #generate some random values that serve as fixed points
  value_points &lt;- spsample(gadm_t, type="stratified", n = 200)
  values &lt;- data.frame(value = rnorm(dim(coordinates(value_points))[1], 0 ,1))
  value_df &lt;- SpatialPointsDataFrame(value_points, values)
  
  #generate a grid that can be estimated from the fixed points
  grd = spsample(gadm_t, type = "regular", n = 4000)
  kr &lt;- autoKrige(value~1, value_df, grd)
  dat = as.data.frame(kr$krige_output)
  
  #draw the generated grid with the underlying map
  ggplot(gadm_t,aes(long,lat)) + geom_polygon(aes(group=group), fill="white") + geom_path(color="white",aes(group=group)) + coord_equal() + 
  geom_tile(aes(x = x1, y = x2, fill = var1.pred), data = dat) + scale_fill_continuous(low = "white", high = muted("orange"), name = "value")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/3b8jP.jpg" alt="autoKrige Netherlands"></p>