0

Using R, I am trying to obtain an interpolation map in which I am able to see my waypoints and their interpolation (based on their value called N). In the background I wanted to have a black and white map with visible roads and paths. I am sure that it is not complicated to do, but I do not understand where I am making a mistake here.

My data looks like this:

  ALLdata
  ID  lat      long     N
1 213 52.36483 20.77724 5
2 214 52.36483 20.77724 5
3 215 52.36483 20.77724 5
4 216 52.20367 20.97026 6
5 217 52.20267 20.96939 7
6 218 52.20371 20.96483 0
7 219 52.20347 20.96595 1
8 220 52.20260 20.96884 2

Here is my code:

ALLdata <- read.csv("C:/Users/Me/Desktop/sample_animals.csv",header=TRUE,sep=";")
head(ALLdata)  
setwd("C:/Users/Me/Desktop/dataFieldseason2016/Graphics") #save graphs and maps

# grab a map. get_map creates a raster object
library(ggmap)
rwanda1 <- get_map(location = c(lon = 20.7, lat = 52.3),
               zoom = 9,
               maptype = "toner",
               source = "stamen")

g1 <- ggmap(rwanda1)
g1

# plot map and animals data
# use coord_map with default mercator projection
g1 + 
  geom_tile(data = ALLdata, aes(x = long, y = lat, z = N, fill = N), alpha = 0.8) +
  stat_contour(data = ALLdata, aes(x = long, y = lat, z = N)) +
  ggtitle("animals presence") +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_continuous(name = "animals (N)",
                    low = "white", high = "blue") +
  theme(plot.title = element_text(size = 25, face = "bold"),
    legend.title = element_text(size = 15),
    axis.text = element_text(size = 15),
    axis.title.x = element_text(size = 20, vjust = -0.5),
    axis.title.y = element_text(size = 20, vjust = 0.2),
        legend.text = element_text(size = 10)) +
  coord_map()

I got this: enter image description here

Below is what didn't work. I would like to see my waypoints as points on this interpolation cloud with the highest being darker and the lowest being lighter or white. enter image description here

I am not able to see my points or my interpolation.

ChrisB
  • 2,497
  • 2
  • 24
  • 43
  • That "interpretation cloud" looks like a filled contour grphic, possibly constructed with lattice::contourplot or loess2d or akima::interp. you need to present data with 2d values in the region of that map. – IRTFM Nov 08 '16 at 19:51
  • I tried using stat_countour but the map showes me the scale of my N values on the right side without any colour on the map itself, and I have this error: Not possible to generate contour data , I tried with statdensity_2d but the output is not what I want since it shows the density of the points and not of the N value: in akima I can see the interpolation colors but I would prefer to have a map as background. I don't know how to do it: how can I combine akima with a black and white map as background? – Parus major Nov 11 '16 at 10:22
  • 1
    I solved my problems and I finally managed to obtain the interpolated maps... I used this procedure http://stackoverflow.com/questions/19339296/plotting-contours-on-an-irregular-grid thank you anyway for your help! – Parus major Nov 24 '16 at 10:57
  • And thank you for endorsing the duplicate. I had earlier found that Q&A in a search but did not know if it would solve your problem. – IRTFM Nov 24 '16 at 16:14
  • it has been a long struggle, really... I am just facing a last problem now: I want to change the colour levels with three specific colours and intervals (i.e. rain mm like red, blue and yellow, like from 0 to 150, 150 to 200, 201 to 300. Any suggestion? – Parus major Nov 25 '16 at 07:45
  • If doing a search on `[r] geom_tile fill palette` does not answer the question then post a new question. And this time either start by constructing a complete example. – IRTFM Nov 25 '16 at 14:34
  • A bit later but I was working on it while writing my manuscript :) so, I found the solution. First, you have to create a collection with the breaks that you would like to have, for example:
      b <- c(0,25, 50, 100, 150, 200)  then you should return to your ggplot script and add a new row after geom_tile: 
    scale_fill_gradientn(colours=c("linen","darkgreen", "blue","brown","red","darkred"),breaks=b,labels=format(b)) 
    – Parus major Jan 14 '17 at 13:57

0 Answers0