使用 geom_point / ggmap / plot 在海岸线附近绘制点
Plotting points near a coastline with geom_point / ggmap / plot
我习惯于使用 matlab,现在正在尝试学习如何在 R 中使用 tidyverse(特别是 ggplot2),所以我正在绘制新斯科舍海岸外所有点的地图我正在为一个项目收集数据。我知道我在绘制从 "map" 开始的部分是错误的,但我不知道如何使用基于 latitude/longitude 的 ggmap 进行绘制。我假设下一行 "loc_map" 不起作用,因为 "map" 不是在 tidyverse 中创建的,但我不知道如何解决这个问题!
lat <- loc$Lat
long <- loc$Long
locs <- data.frame(long,lat)
data("coastlineWorldFine")
map <- plot(coastlineWorldFine, col='grey', clong= mean(long),
clat=mean(lat), span=400, projection = "+proj=merc",
main="Sample Sites")
loc_map <- map + geom_point(data=locs, aes(x=long, y=lat), size = 20)
这是一个起点,您可以将 geom_point
图层添加到该起点。首先,我加载了很多库。 marmap
和 oce
分别是测深和海岸线数据所必需的。 RColorBrewer
用于测深的调色板,而 mutate
需要 dplyr
。 magrittr
提供复合赋值管道算子(%<>%
),tibble
用于我重构测深数据,ggthemes
提供theme_tufte
.
# Load libraries
library(ggplot2)
library(marmap)
library(oce)
library(RColorBrewer)
library(dplyr)
library(magrittr)
library(tibble)
library(ggthemes)
在这里,我获取了测深数据,对其进行了重组,并将其分类为深度区间。
# Get bathymetry data
bathy <- getNOAA.bathy(lon1 = -68, lon2 = -56,
lat1 = 41, lat2 = 49,
resolution = 1, keep = TRUE)
bathy <- as.tibble(fortify.bathy(bathy))
bathy %<>% mutate(depth_bins = cut(z, breaks = c(Inf, 0, -200, -500, -1000,
-1500, -2000, -2500, -3000, -Inf)))
接下来,我获取海岸线数据并将其放入数据框中。
# Get coast line data
data(coastlineWorldFine, package = "ocedata")
coast <- as.data.frame(coastlineWorldFine@data)
终于画出来了
# Plot figure
p <- ggplot()
p <- p + geom_raster(data = bathy, aes(x = x, y = y, fill = depth_bins), interpolate = TRUE, alpha = 0.75)
p <- p + geom_polygon(data = coast, aes(x = longitude, y = latitude))
p <- p + coord_cartesian(ylim = c(42, 47), xlim = c(-67, -57))
p <- p + theme_tufte()
p <- p + theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
legend.position = "right",
plot.title = element_text(size = 24),
legend.title = element_text(size = 20),
legend.text = element_text(size = 18))
p <- p + scale_fill_manual(values = rev(c("white", brewer.pal(8, "Blues"))), guide = "none")
print(p)
这给出了以下内容:
添加 geom_point
层将允许您绘制您的现场站点。
你没有给我们太多的工作机会,但现在开始了。
library(sf)
library(lwgeom) # needed only for st_sample
library(tidyverse)
让我们获取 Admin01 级别的加拿大 shapefile,抽出 Nova Scotia 并稍微简化多边形
# Get a Canada Admin01 shapefile
canada <- st_as_sf(raster::getData("GADM", country = "CAN", level = 1))
# just get Nova Scotia
ns <- filter(canada, NAME_1 == "Nova Scotia")
# simplify the polygons a bit (tweak `0.01` as you need)
ns <- st_simplify(ns, preserveTopology = TRUE, 0.01)
现在,我们将生成一些点数据,因为您没有提供任何数据。这些不会都在海岸线上:
set.seed(2018-11-23)
some_random_points <- as_data_frame(st_coordinates(st_sample(ns, 20)))
some_random_points
## # A tibble: 18 x 2
## X Y
## <dbl> <dbl>
## 1 -63.4 44.7
## 2 -63.9 45.1
## 3 -64.2 44.7
## 4 -60.8 46.8
## 5 -65.0 44.3
## 6 -63.8 45.4
## 7 -62.7 45.3
## 8 -66.1 44.3
## 9 -64.8 44.1
## 10 -64.5 44.8
## 11 -63.8 44.5
## 12 -64.7 44.8
## 13 -63.1 44.9
## 14 -65.5 43.9
## 15 -64.6 44.4
## 16 -60.4 45.9
## 17 -63.9 44.6
## 18 -62.4 45.6
现在,做一些现代的事情 gg_cartography:
ggplot() +
geom_sf(data = ns, fill = "gray90", color = "#2b2b2b", size=0.125) +
geom_point(data = some_random_points, aes(X, Y)) +
theme_bw()
我习惯于使用 matlab,现在正在尝试学习如何在 R 中使用 tidyverse(特别是 ggplot2),所以我正在绘制新斯科舍海岸外所有点的地图我正在为一个项目收集数据。我知道我在绘制从 "map" 开始的部分是错误的,但我不知道如何使用基于 latitude/longitude 的 ggmap 进行绘制。我假设下一行 "loc_map" 不起作用,因为 "map" 不是在 tidyverse 中创建的,但我不知道如何解决这个问题!
lat <- loc$Lat
long <- loc$Long
locs <- data.frame(long,lat)
data("coastlineWorldFine")
map <- plot(coastlineWorldFine, col='grey', clong= mean(long),
clat=mean(lat), span=400, projection = "+proj=merc",
main="Sample Sites")
loc_map <- map + geom_point(data=locs, aes(x=long, y=lat), size = 20)
这是一个起点,您可以将 geom_point
图层添加到该起点。首先,我加载了很多库。 marmap
和 oce
分别是测深和海岸线数据所必需的。 RColorBrewer
用于测深的调色板,而 mutate
需要 dplyr
。 magrittr
提供复合赋值管道算子(%<>%
),tibble
用于我重构测深数据,ggthemes
提供theme_tufte
.
# Load libraries
library(ggplot2)
library(marmap)
library(oce)
library(RColorBrewer)
library(dplyr)
library(magrittr)
library(tibble)
library(ggthemes)
在这里,我获取了测深数据,对其进行了重组,并将其分类为深度区间。
# Get bathymetry data
bathy <- getNOAA.bathy(lon1 = -68, lon2 = -56,
lat1 = 41, lat2 = 49,
resolution = 1, keep = TRUE)
bathy <- as.tibble(fortify.bathy(bathy))
bathy %<>% mutate(depth_bins = cut(z, breaks = c(Inf, 0, -200, -500, -1000,
-1500, -2000, -2500, -3000, -Inf)))
接下来,我获取海岸线数据并将其放入数据框中。
# Get coast line data
data(coastlineWorldFine, package = "ocedata")
coast <- as.data.frame(coastlineWorldFine@data)
终于画出来了
# Plot figure
p <- ggplot()
p <- p + geom_raster(data = bathy, aes(x = x, y = y, fill = depth_bins), interpolate = TRUE, alpha = 0.75)
p <- p + geom_polygon(data = coast, aes(x = longitude, y = latitude))
p <- p + coord_cartesian(ylim = c(42, 47), xlim = c(-67, -57))
p <- p + theme_tufte()
p <- p + theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
legend.position = "right",
plot.title = element_text(size = 24),
legend.title = element_text(size = 20),
legend.text = element_text(size = 18))
p <- p + scale_fill_manual(values = rev(c("white", brewer.pal(8, "Blues"))), guide = "none")
print(p)
这给出了以下内容:
添加 geom_point
层将允许您绘制您的现场站点。
你没有给我们太多的工作机会,但现在开始了。
library(sf)
library(lwgeom) # needed only for st_sample
library(tidyverse)
让我们获取 Admin01 级别的加拿大 shapefile,抽出 Nova Scotia 并稍微简化多边形
# Get a Canada Admin01 shapefile
canada <- st_as_sf(raster::getData("GADM", country = "CAN", level = 1))
# just get Nova Scotia
ns <- filter(canada, NAME_1 == "Nova Scotia")
# simplify the polygons a bit (tweak `0.01` as you need)
ns <- st_simplify(ns, preserveTopology = TRUE, 0.01)
现在,我们将生成一些点数据,因为您没有提供任何数据。这些不会都在海岸线上:
set.seed(2018-11-23)
some_random_points <- as_data_frame(st_coordinates(st_sample(ns, 20)))
some_random_points
## # A tibble: 18 x 2
## X Y
## <dbl> <dbl>
## 1 -63.4 44.7
## 2 -63.9 45.1
## 3 -64.2 44.7
## 4 -60.8 46.8
## 5 -65.0 44.3
## 6 -63.8 45.4
## 7 -62.7 45.3
## 8 -66.1 44.3
## 9 -64.8 44.1
## 10 -64.5 44.8
## 11 -63.8 44.5
## 12 -64.7 44.8
## 13 -63.1 44.9
## 14 -65.5 43.9
## 15 -64.6 44.4
## 16 -60.4 45.9
## 17 -63.9 44.6
## 18 -62.4 45.6
现在,做一些现代的事情 gg_cartography:
ggplot() +
geom_sf(data = ns, fill = "gray90", color = "#2b2b2b", size=0.125) +
geom_point(data = some_random_points, aes(X, Y)) +
theme_bw()