1

Okay so I wish to plot a plain map onto the ggmap base so that the ocean shows the bathymetry data.

library(maps)
library(mapdata)
library(ggplot2)
library(ggmap)

#pick out the base layer I want
get.Peru<-get_map(location = c(left = -85, bottom = -20, right = -70, top = -5), maptype="satellite") 

#this is the layer i wish to put over the top
coast_map <- fortify(map("worldHires", fill = TRUE, plot = FALSE)) 

peru_bathy_map <- fortify(get.Peru)

gg <- ggplot() 
gg <- gg + geom_map(data=peru_bathy_map, map=peru_bathy_map,
                    aes(map_id=id))  
gg <- gg + geom_map(data=coast_map, map=coast_map, aes(x=long, y=lat, map_id=region),
                    fill="gray", color="black") + xlim(-86,-70) + ylim(-20,-4) + labs(x="Longitude", y="Latitude") 
gg <- gg + coord_map() +  theme_classic()

However, I get the error: Error: ggplot2 doesn't know how to deal with data of class ggmapraster when I try and run the fortify code.

I have used this method before to add bathymetry polygons (see previous question) but they looked messy when I plotted datapoints onto the ocean so I am now trying this.

If anyone has any pointers, or even other ways of plotting cleanish looking bathymetry data other that ggmaps satellite please let me know :)

CDav
  • 187
  • 1
  • 5
  • 12
  • Seeing your code, I am guessing you want draw a Peru map on top of the ggmap image. Is that correct? I saw your previous question, and I wonder if you have any bathymetry things in this question. – jazzurro Dec 11 '14 at 03:20
  • Yes that is what I would like to do. There is no bathymetry data as such in this one, but the 'satellite' map type in ggmaps shows the underwater contours nicely. – CDav Dec 11 '14 at 12:57
  • OK. I will repost my answer then. I tried to show the entire of Peru in my answer, just in case. – jazzurro Dec 11 '14 at 13:31

1 Answers1

3

Here is one approach. When you download maps using ggmap, maps have class ggmap and raster. It seems that this is something you cannot apply fortify. Seeing your code, I think this is perhaps what you want to do. First, you get a map of Peru. Then, you get the data to draw the map for Peru filled with gray colour. Here, I subsetted data for Peru only. Finally, when I drew this figure, I used ggmap and geom_map.

library(mapdata)
library(ggmap)
library(ggplot2)

# Get Peru map
Peru <- get_map(location = "Peru", zoom = 5, maptype="satellite") 

# This is the layer I wish to put over the top
coast_map <- fortify(map("worldHires", fill = TRUE, plot = FALSE)) 

# Subset data for Peru
peru.coast <- subset(coast_map, region == "Peru")

# Draw a graphic
ggmap(Peru) +
geom_map(data = peru.coast, map = peru.coast, aes(x = long, y = lat, map_id = region),
         fill="gray", color="black") +
xlim(-86, -68) +
ylim(-20, 0) + 
labs(x = "Longitude", y = "Latitude") +
coord_map() +
theme_classic()

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • This is actually pretty much what I wanted! However, when I run this code, I get: 'Error in as.raster(raster) : Objet is not of class bathy' – CDav Dec 11 '14 at 13:52
  • 1
    @CDav I removed all items and started a new R session with the three packages in my answer. I did not receive any error message. I wonder if other packages or something else in interrupting the code. Could you double check it? – jazzurro Dec 11 '14 at 14:04