问题
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 :)
回答1:
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()

来源:https://stackoverflow.com/questions/27413889/layering-a-ggplot2-map-onto-ggmap-satellite-with-bathymetry-data