Layering a ggplot2 map onto ggmap satellite with bathymetry data

萝らか妹 提交于 2019-12-13 17:21:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!