使用测深数据将ggplot2地图分层到ggmap卫星上 [英] Layering a ggplot2 map onto ggmap satellite with bathymetry data
问题描述
好的,我希望在ggmap基座上绘制一个普通地图,以便海洋显示测深数据.
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()
但是,我收到错误消息:错误:尝试运行强化代码时,ggplot2不知道如何处理ggmapraster类的数据
.
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.
如果有人有任何指针,甚至还有其他方法来绘制ggmaps卫星以外的看起来干净的测深数据,请让我知道:)
If anyone has any pointers, or even other ways of plotting cleanish looking bathymetry data other that ggmaps satellite please let me know :)
推荐答案
这是一种方法.使用 ggmap
下载地图时,地图的类为 ggmap
和 raster
.看来这是您不能应用 fortify
的东西.看到您的代码,我想这也许就是您想要做的.首先,您将获得秘鲁的地图.然后,您将获得数据以绘制灰色的秘鲁地图.在这里,我仅对秘鲁的数据进行了子集化.最后,当我绘制此图时,我使用了 ggmap
和 geom_map
.
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()
这篇关于使用测深数据将ggplot2地图分层到ggmap卫星上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!