使用较旧的ggmap数据使用getNOAA创建新地图

时间:2019-07-02 06:58:34

标签: r ggplot2 ggmap rgooglemaps

我去年使用R中的Rgooglemaps绘制了一个调查点地图。这是一个简单的地图,显示了进行不同调查的地点。由于Google已更新其许可证,因此我想使用marmap使用getNOAA函数重新创建此地图。

这是我的旧地图

  [![Survey Map using Google Maps][1]][1]

这是我正在使用的当前代码,但是新系统使我有些困惑。

  library(marmap)
  library(ggplot2)

  Survey_Map<- getNOAA.bathy(lon1 = -26, lon2 = -15, lat1 = 22, lat2 = 
  19, resolution = 1)

  Survey_Points<-autoplot(Survey_Map, geom = c("raster", "contour")) + 
  scale_fill_etopo()

  Map<-Survey_Points+geom_point(data = Survey_Map, aes(x=Longitude, 
  y=Latitude), size = 1.3, colour= Survey_Type)

  Map

这是我正在使用的数据

  structure(list(Survey_Type = structure(c(2L, 2L, 2L, 2L, 2L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
  3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L, 4L), .Label = c("BRUVs Survey", "Fishermen Survey", 
  "Snorkel Survey 2014", "Snorkel Survey 2015"), class = "factor"), 
   Latitude = c(16.73194, 16.73274, 15.33520907, 15.33486, 15.3316, 
   15.33568, 15.33568, 15.3458, 15.3458, 15.33301, 15.33301, 
   15.34517, 15.34517, 15.08846, 15.11057, 15.11057, 15.11228, 
   15.11228, 15.2832, 15.2832, 15.29262, 15.29262, 15.22394, 
   15.22394, 15.24419, 15.24419, 15.22698, 15.22698, 15.25396, 
   15.25396, 15.11008, 15.11008, 15.10803, 15.10803, 15.19545, 
   15.17862, 15.11457, 15.09544, 15.09544, 15.11015, 15.11015, 
   15.28835, 15.2752, 15.16301, 15.16301, 15.11597, 15.1107, 
   15.11036, 15.23928, 15.26985, 15.11055, 15.11055, 15.10636, 
   15.10636, 15.26554, 15.26554, 15.22398, 15.17643, 15.17643, 
   15.19829, 15.19829, 14.93243, 15.25434, 15.25428, 15.3304, 
   15.3304, 15.29193, 15.29193, 15.36435, 15.27594, 15.27594, 
   15.30885, 15.30885, 15.14253, 15.14253, 15.16037, 15.16037, 
   15.11021, 15.11121, 15.11121, 15.2726, 15.28601, 15.1433, 
   15.1433, 15.16446, 15.16446, 15.27095, 15.22856, 15.22856, 
   15.22529, 15.22529, 15.3303, 15.32407, 15.12321, 15.0802, 
   15.31593, 15.31559, 15.31693, 15.31729, 15.31674, 15.31674, 
   15.31709, 15.109374, 15.10863, 15.10827, 15.12663, 15.2719, 
   15.2698, 15.31164, 15.31172, 15.31215, 15.31457, 15.31857, 
   15.11158, 15.11055, 15.27275, 15.15893, 15.16213, 15.11035, 
   15.10915, 15.10344, 15.10923, 15.11025, 15.11104, 15.10917, 
   15.10687, 15.11028, 15.11004, 15.11876, 15.11755, 15.11565, 
   15.1089, 15.11058, 15.11012, 15.11027, 15.22524, 15.22644
   ), Longitude = c(-27.0132, -27.0112, -16.9656, -23.19415, 
   -23.18153, -23.14718, -23.14718, -23.16627, -23.16627, -23.19646, 
   -23.19646, -23.19459, -23.19459, -23.16098, -23.11927, -23.11927, 
   -23.11406, -23.11406, -23.24286, -23.24286, -23.23423, -23.23423, 
   -23.27888, -23.27888, -23.24419, -23.24419, -23.08382, -23.08382, 
   -23.08812, -23.08812, -23.18041, -23.18041, -23.14643, -23.14643, 
   -23.25713, -23.24797, -23.10995, -23.17202, -23.17202, -23.15479, 
   -23.15479, -23.2331, -23.24257, -23.07842, -23.07842, -23.20737, 
   -23.19075, -2311970, -23.09255, -23.10476, -23.11863, -23.11863, 
   -23.13563, -23.13563, -23.0757, -23.0757, -23.0857, -23.25823, 
   -23.25823, -23.25384, -23.25384, -22.22907, -23.22907, -23.23523, 
   -23.20714, -23.20714, -23.23079, -23.23079, -23.20673, -23.23527, 
   -23.23527, -23.21675, -23.21675, -23.09247, -23.09247, -23.08146, 
   -23.08146, -23.11954, -23.18338, -23.18338, -23.2411, -23.24536, 
   -23.09274, -23.09274, -23.08542, -23.08542, -23.24192, -23.27364, 
   -23.27364, -23.26522, -23.26522, -23.1819, -23.1738, -23.2068, 
   -23.1558, -23.18878, -23.1883, -23.18521, -23.18542, -23.1845, 
   -23.1845, -23.18621, -23.1265, -23.1221, -23.12294, -23.13424, 
   -23.1035, -23.1039, -23.14203, -23.14224, -23.1434, -23.14557, 
   -23.18595, -23.1205, -23.12284, -23.1016, -23.06118, -23.06145, 
   -23.11963, -23.12103, -23.13235, -23.12047, -23.11879, -23.1178, 
   -23.12014, -23.1244, -23.12028, -23.12266, -23.13638, -23.13973, 
   -23.13559, -23.12061, -23.1147, -23.11508, -23.11559, -23.21706, 
   -23.22154)), class = "data.frame", row.names = c(NA, -137L))

这是我尝试创建地图时遇到的错误。

  Error: Aesthetics must be either length 1 or the same as the data 
  (217800): x, y

2 个答案:

答案 0 :(得分:1)

这是我的工作,不使用google的任何地图。(但也不要使用NOAA地图,因为我以前从未使用过。)

config maps

enter image description here

library( OpenStreetMap )
library( sf )
library( magrittr )

#set boundary-coordinates
LAT1 =  15   ; LAT2 =  15.4
LON1 = -23.3 ; LON2 = -23

#get bing-sattelite map
map <- OpenStreetMap::openmap( c( 15, -23.3 ), c( 15.4, -23 ), zoom = NULL, type = "bing", mergeTiles = TRUE)

#some pretty strange coordinates in the sample data,
#so filter to boundary box of LAT and LON
df2 <- df[ df$Longitude >= LON1 & df$Longitude <= LON2 & df$Latitude >= LAT1 & df$Latitude <= LAT2, ]
#convert coordinates to Mercator projection
df2.mercator <- as.data.frame( OpenStreetMap::projectMercator( lat = df2$Latitude, long = df2$Longitude ) )
df2.plot <- cbind( df2, df2.mercator )

#and plot
ggplot2::autoplot( map ) + geom_point( data = df2.plot, aes( x = x, y = y, colour = Survey_Type), 
                                       alpha = 0.25, size = 5 ) 

enter image description here

答案 1 :(得分:1)

使用getNOAA.bathy()包中的marmap,您可以执行以下操作:

# Load packages
library(marmap)
library(ggplot2)

# Download bathymetry from NOAA
# Be careful to download an area large enough to cover all you survey points
# Add keep = TRUE to avoid re-downloading the same dataset from NOAA every time you call this command
bathy_data <- getNOAA.bathy(lon1 = -28, 
                            lon2 = -15, 
                            lat1 = 14, 
                            lat2 = 17,
                            resolution = 1,
                            keep = TRUE)

# Load the location and type of surveys
location <- structure(list(Survey_Type = structure(c(2L, 2L, 2L, 2L, 2L, 
                                                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                       1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                       3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
                                                       4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                                       4L, 4L, 4L, 4L), .Label = c("BRUVs Survey", "Fishermen Survey", 
                                                                                   "Snorkel Survey 2014", "Snorkel Survey 2015"), class = "factor"), 
                             Latitude = c(16.73194, 16.73274, 15.33520907, 15.33486, 15.3316, 
                                          15.33568, 15.33568, 15.3458, 15.3458, 15.33301, 15.33301, 
                                          15.34517, 15.34517, 15.08846, 15.11057, 15.11057, 15.11228, 
                                          15.11228, 15.2832, 15.2832, 15.29262, 15.29262, 15.22394, 
                                          15.22394, 15.24419, 15.24419, 15.22698, 15.22698, 15.25396, 
                                          15.25396, 15.11008, 15.11008, 15.10803, 15.10803, 15.19545, 
                                          15.17862, 15.11457, 15.09544, 15.09544, 15.11015, 15.11015, 
                                          15.28835, 15.2752, 15.16301, 15.16301, 15.11597, 15.1107, 
                                          15.11036, 15.23928, 15.26985, 15.11055, 15.11055, 15.10636, 
                                          15.10636, 15.26554, 15.26554, 15.22398, 15.17643, 15.17643, 
                                          15.19829, 15.19829, 14.93243, 15.25434, 15.25428, 15.3304, 
                                          15.3304, 15.29193, 15.29193, 15.36435, 15.27594, 15.27594, 
                                          15.30885, 15.30885, 15.14253, 15.14253, 15.16037, 15.16037, 
                                          15.11021, 15.11121, 15.11121, 15.2726, 15.28601, 15.1433, 
                                          15.1433, 15.16446, 15.16446, 15.27095, 15.22856, 15.22856, 
                                          15.22529, 15.22529, 15.3303, 15.32407, 15.12321, 15.0802, 
                                          15.31593, 15.31559, 15.31693, 15.31729, 15.31674, 15.31674, 
                                          15.31709, 15.109374, 15.10863, 15.10827, 15.12663, 15.2719, 
                                          15.2698, 15.31164, 15.31172, 15.31215, 15.31457, 15.31857, 
                                          15.11158, 15.11055, 15.27275, 15.15893, 15.16213, 15.11035, 
                                          15.10915, 15.10344, 15.10923, 15.11025, 15.11104, 15.10917, 
                                          15.10687, 15.11028, 15.11004, 15.11876, 15.11755, 15.11565, 
                                          15.1089, 15.11058, 15.11012, 15.11027, 15.22524, 15.22644
                             ), Longitude = c(-27.0132, -27.0112, -16.9656, -23.19415, 
                                              -23.18153, -23.14718, -23.14718, -23.16627, -23.16627, -23.19646, 
                                              -23.19646, -23.19459, -23.19459, -23.16098, -23.11927, -23.11927, 
                                              -23.11406, -23.11406, -23.24286, -23.24286, -23.23423, -23.23423, 
                                              -23.27888, -23.27888, -23.24419, -23.24419, -23.08382, -23.08382, 
                                              -23.08812, -23.08812, -23.18041, -23.18041, -23.14643, -23.14643, 
                                              -23.25713, -23.24797, -23.10995, -23.17202, -23.17202, -23.15479, 
                                              -23.15479, -23.2331, -23.24257, -23.07842, -23.07842, -23.20737, 
                                              -23.19075, -23.11970, -23.09255, -23.10476, -23.11863, -23.11863, 
                                              -23.13563, -23.13563, -23.0757, -23.0757, -23.0857, -23.25823, 
                                              -23.25823, -23.25384, -23.25384, -22.22907, -23.22907, -23.23523, 
                                              -23.20714, -23.20714, -23.23079, -23.23079, -23.20673, -23.23527, 
                                              -23.23527, -23.21675, -23.21675, -23.09247, -23.09247, -23.08146, 
                                              -23.08146, -23.11954, -23.18338, -23.18338, -23.2411, -23.24536, 
                                              -23.09274, -23.09274, -23.08542, -23.08542, -23.24192, -23.27364, 
                                              -23.27364, -23.26522, -23.26522, -23.1819, -23.1738, -23.2068, 
                                              -23.1558, -23.18878, -23.1883, -23.18521, -23.18542, -23.1845, 
                                              -23.1845, -23.18621, -23.1265, -23.1221, -23.12294, -23.13424, 
                                              -23.1035, -23.1039, -23.14203, -23.14224, -23.1434, -23.14557, 
                                              -23.18595, -23.1205, -23.12284, -23.1016, -23.06118, -23.06145, 
                                              -23.11963, -23.12103, -23.13235, -23.12047, -23.11879, -23.1178, 
                                              -23.12014, -23.1244, -23.12028, -23.12266, -23.13638, -23.13973, 
                                              -23.13559, -23.12061, -23.1147, -23.11508, -23.11559, -23.21706, 
                                              -23.22154)), class = "data.frame", row.names = c(NA, -137L))

# Create the map
Survey_Map <- autoplot(bathy_data, geom = c("raster", "contour")) + 
  scale_fill_etopo()

# Add survey points
Map <- Survey_Map +
  geom_point(data = location, 
             aes(x = Longitude, 
                 y = Latitude,
                 color = Survey_Type),
             size = 1.6)

# Display the result
Map

这是我的机器上的样子:

enter image description here