0

I have the following code in R

mp1 <- fortify(map(fill=TRUE, plot=FALSE))
mp2 <- mp1
mp2$long <- mp2$long + 360
mp2$group <- mp2$group + max(mp2$group) + 1
mp <- rbind(mp1, mp2)
map <- (ggplot(aes(x = long, y = lat, group = group), data = mp) + 
  geom_path()  + 
  scale_x_continuous(name="Longitude",minor_breaks=seq(170,246,2), limits = c(170, 246)) + 
  scale_y_continuous(name= "Latitude",minor_breaks=seq(30,64,2),limits = c(30, 64)))

which produces this image

My aim next is to colour in the 'pixels' (or breaks) blue depending on whether they are land or not (I'm showing which parts of the map I have a times series for to put the task into context) so I run this for loop which shades in the relevant pixels I have data to get this image

Is there a way to get the original plot lines (so the breaks going up at 170,172,..... and across at 30,32,.....) and also the original map so that it goes over to filled in blue bits.

The for loop I used was:

for(i in 1:663){
  if(sst.yearly.matrix[i,3] != sst.yearly.matrix[1,3]){
    map <- map + annotate("rect", xmin=sst.yearly.matrix[i,2], xmax=sst.yearly.matrix[i,2]+2, 
                          ymin=sst.yearly.matrix[i,1],ymax=sst.yearly.matrix[i,1]+2, fill="deepskyblue")
  }
}

Any help is much appreciated! Thanks.

  • 1
    instead of adding `geom_path` __before__ the `for` loop, try adding it on __after__ – bouncyball Aug 03 '17 at 17:04
  • As for getting gridlines on top of the blue, [this question is a couple years old but it has some ideas](https://stackoverflow.com/q/33989595/903061). You could also make the blue semi-transparent (maybe `alpha = .5`). – Gregor Thomas Aug 03 '17 at 17:10
  • @bouncyball I have tried doing what you said, I took out the geom_path and running the for loop, and once that has ran I tried map <- map + geom_path but I get the error 'don't know how to add geom_path to a plot'. I'm guessing I may have done what you suggested wrong so you could please advise? Thanks Edit: I just realised I put 'geom_path' rather than 'geom_path()' and this has run now, many thanks! – David Ireland Aug 04 '17 at 14:08

0 Answers0