[英]Layering a ggplot2 map onto ggmap satellite with bathymetry data
好的,我希望在ggmap基座上繪制一個普通地圖,以便海洋顯示測深數據。
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()
但是,我收到錯誤消息: Error: ggplot2 doesn't know how to deal with data of class ggmapraster
嘗試運行強化代碼時, Error: ggplot2 doesn't know how to deal with data of class ggmapraster
。
在添加測深多邊形之前,我曾使用過此方法(請參閱上一個問題),但是當我將數據點繪制到海洋上時,它們看起來很凌亂,因此我現在嘗試這樣做。
如果有人有任何指針,甚至還有其他方法來繪制ggmaps衛星以外的看起來干凈的測深數據,請讓我知道:)
這是一種方法。 使用ggmap
下載地圖時,地圖具有ggmap
和raster
類。 看來這是您無法申請fortify
。 看到您的代碼,我想這也許就是您想要做的。 首先,您將獲得秘魯的地圖。 然后,您將獲得數據以繪制灰色的秘魯地圖。 在這里,我僅對秘魯的數據進行了子集化。 最后,當我繪制該圖時,我使用了ggmap
和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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.