R 如何在地图上放置比例尺和北箭头(ggplot)?
我试图使用“ggsn”包放置一个比例尺和一个北箭头。 但我收到一条错误消息: 错误:注释\自定义仅适用于笛卡尔坐标 此外:警告信息: 1:最小值(data$long):最小值没有不丢失的参数;返回Inf 2:在max中(data$long):max没有不丢失的参数;返回-Inf 3:最小值(数据$lat):最小值没有未丢失的参数;返回Inf 4:在max(data$lat)中:max没有未丢失的参数;返回-Inf 5:删除了包含非有限值的1374840行(stat_contour) 我的代码如下:R 如何在地图上放置比例尺和北箭头(ggplot)?,r,dictionary,ggplot2,graphics,R,Dictionary,Ggplot2,Graphics,我试图使用“ggsn”包放置一个比例尺和一个北箭头。 但我收到一条错误消息: 错误:注释\自定义仅适用于笛卡尔坐标 此外:警告信息: 1:最小值(data$long):最小值没有不丢失的参数;返回Inf 2:在max中(data$long):max没有不丢失的参数;返回-Inf 3:最小值(数据$lat):最小值没有未丢失的参数;返回Inf 4:在max(data$lat)中:max没有未丢失的参数;返回-Inf 5:删除了包含非有限值的1374840行(stat_contour) 我的代码如下
#loading the map
world <- maps::map("world2", fill=TRUE, plot=FALSE) # world2 is the Pacific centered map with longitude [0,360]
IDs <- sapply(strsplit(world$names, ":"), function(x) x[1])
world <- map2SpatialPolygons(world, IDs=IDs, proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
world_map <- fortify(world)
#load the bathymetry
bat <- marmap::getNOAA.bathy(lon1 = -5.0000, lon2 = -80.0000, lat1 = 14, lat2 = -70.61, keep=T)
#converting the bathymetry in data.frame
bat2 = marmap::fortify.bathy(bat)
g <- ggplot(AR, aes(x = lon, y = lat)) + #(old: Longitude #Latitude)
geom_path(aes(group = sex, col = sex), size = 0.8, alpha = 0.5) +
geom_map(data = world_map, map=world_map, aes(x = long, y = lat, map_id = id), fill = "darkgray") +
geom_contour(data = bat2,
aes(x=x, y=y, z=z),
breaks=c(-200),
size=c(0.3),
colour="blue")+
xlim(extendrange(AR$lon, f = 0.1)) + #(old: Longitude)
ylim(extendrange(AR$lat, f = 0.1)) + #(old: Latitude)
coord_map("ortho", orientation = c(-40, -40, 0))
g +
ggsn::scalebar(world_map, dist = 100,location = "bottomright", transform = TRUE, #Adicione uma barra de escala
dist_unit = "km", st.dist = 0.03, st.size = 2, model = 'WGS84') +
ggsn::north(world_map, scale = .1)
我怎样才能解决这个问题
#####编辑1
我已经编辑了我的帖子,现在“世界地图”对象可用了
我在使用GGS时遇到问题。
我加载了您提到的所有包并运行了命令,但无法执行该函数。
我去添加包“ggspatial”,但出现以下消息:
"* installing * source * package 'ggspatial' ...
** package 'ggspatial' successfully unpacked and MD5 sums checked
** R
** inst
** byte-compile and prepare package for lazy loading
Error: .onLoad failed in loadNamespace () for 'sf', details:
call: get (genname, envir = envir)
error: object 'group_split' not found
ERROR: lazy loading failed for package 'ggspatial'
* removing 'C: /Users/annel/OneDrive/Documentos/R/win-library/3.5/ggspatial'
In R CMD INSTALL
Warning in install.packages:
installation of package ‘ggspatial’ had non-zero exit status
The downloaded source packages are in
‘C: \ Users \ annel \ AppData \ Local \ Temp \ RtmpKueyQV \ downloaded_packages’ "
当我执行您请求的命令时,我收到消息:
Error in loadNamespace (name): there is no package called ‘ggspatial’
我不知道
ggsn
软件包,但是如果您不介意使用ggspatial
软件包,下面是一个我理解您试图实现的示例。因为我不能复制你的代码,我已经包括了我自己的自定义数据集和水深数据。。。使用一些漂亮的字体,以获得更美观的最终地图
#加载必要的包
图书馆(tidyverse)
图书馆(ggrepel)
图书馆(sf)
图书馆(marmap)
#创建随机数据
使用您的数据(存储在名为AR
的data.frame
中)伪造数据,并尝试匹配所需的输出,以下是我的建议:
# Load necessary packages
library(tidyverse)
library(sf)
library(marmap)
# Import AR data
# Transform data points into geographic objects
Sites_geo <- AR %>%
st_as_sf(coords = c("lon", "lat"), crs = 4326)
# Get bathymetry data: please note I zoomed on the area of interest
# to get a more manageable dataset. If you want a larger area,
# you should increase res (e.g. res = 10) in order to get a
# bathy object of a reasonable size
bathy <- getNOAA.bathy(-45, -30, -20, -5, res = 4, keep = TRUE)
# load the bathymetry
ggbathy <- fortify(bathy)
# Get countries outline
pays <- rnaturalearth::ne_countries(
country = c("Brazil"),
scale = "large", returnclass = "sf"
)
# Base plot
pl <- ggplot(data = pays) +
geom_contour(
data = ggbathy, aes(x = x, y = y, z = z),
binwidth = 200, color = "grey90", size = 0.3
) +
geom_contour(
data = ggbathy, aes(x = x, y = y, z = z),
binwidth = 1000, color = "grey70", size = 0.4
) +
geom_sf() +
geom_sf(data = Sites_geo, aes(fill = speed), shape = 21) +
geom_path(data = AR, aes(x = lon, y = lat, group = sex, col = sex)) +
coord_sf(xlim = c(-42, -30), ylim = c(-20, -5), expand = FALSE) +
scale_fill_viridis_c() +
labs(x = "", y = "", color = "Sex") +
theme_bw(base_family = "ArcherPro Book")
# Add scale and North arrow
pl +
ggspatial::annotation_scale(
location = "tl",
bar_cols = c("grey60", "white"),
text_family = "ArcherPro Book"
) +
ggspatial::annotation_north_arrow(
location = "tl", which_north = "true",
pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
style = ggspatial::north_arrow_nautical(
fill = c("grey40", "white"),
line_col = "grey20",
text_family = "ArcherPro Book"
)
)
#加载必要的包
图书馆(tidyverse)
图书馆(sf)
图书馆(marmap)
#导入AR数据
#将数据点转换为地理对象
地点(地理位置)%
st_as_sf(坐标=c(“lon”,“lat”),crs=4326)
#获取测深数据:请注意,我放大了感兴趣的区域
#以获得更易于管理的数据集。如果你想要更大的面积,
#您应该增加res(例如res=10)以获得
#大小合适的浴缸
bathy我无法复制您的代码,因为我假设dput
输出用于AR
,但是,您没有提供对象世界地图
。不管怎样,AR
或world\u map
都丢失了。@IanCampbell我编辑了我的帖子。谢谢你好,谢谢你的回答@Benoit。我现在还有一个问题,你能帮我解决吗?我会和他们一起编辑我的帖子。“如果你认为这能回答你的问题,你可以考虑通过检查旁边的记号来接受它。谢谢!”AnneElise您似乎试图从源代码安装ggspatial
。你试过安装二进制软件包吗install.packages(“ggspatial”,type=“binary”)
@Benoit我没有收到。我按照你说的安装了软件包,并在地图中插入了北箭头,但我仍在尝试将其定位在正确的位置。比例尺仍会出现错误。在我完成编辑后,是否无法帮助尝试使用我的数据制作地图?对不起,我对这个问题一无所知。这张地图在我的工作中非常重要,我一直在努力绘制它。@LucyWheeler这是geom_sf()
行(脚本中最短的一行!),它使用ggplot(data=pays)
绘图第一行中指定的数据来绘制国家。
# Load necessary packages
library(tidyverse)
library(sf)
library(marmap)
# Import AR data
# Transform data points into geographic objects
Sites_geo <- AR %>%
st_as_sf(coords = c("lon", "lat"), crs = 4326)
# Get bathymetry data: please note I zoomed on the area of interest
# to get a more manageable dataset. If you want a larger area,
# you should increase res (e.g. res = 10) in order to get a
# bathy object of a reasonable size
bathy <- getNOAA.bathy(-45, -30, -20, -5, res = 4, keep = TRUE)
# load the bathymetry
ggbathy <- fortify(bathy)
# Get countries outline
pays <- rnaturalearth::ne_countries(
country = c("Brazil"),
scale = "large", returnclass = "sf"
)
# Base plot
pl <- ggplot(data = pays) +
geom_contour(
data = ggbathy, aes(x = x, y = y, z = z),
binwidth = 200, color = "grey90", size = 0.3
) +
geom_contour(
data = ggbathy, aes(x = x, y = y, z = z),
binwidth = 1000, color = "grey70", size = 0.4
) +
geom_sf() +
geom_sf(data = Sites_geo, aes(fill = speed), shape = 21) +
geom_path(data = AR, aes(x = lon, y = lat, group = sex, col = sex)) +
coord_sf(xlim = c(-42, -30), ylim = c(-20, -5), expand = FALSE) +
scale_fill_viridis_c() +
labs(x = "", y = "", color = "Sex") +
theme_bw(base_family = "ArcherPro Book")
# Add scale and North arrow
pl +
ggspatial::annotation_scale(
location = "tl",
bar_cols = c("grey60", "white"),
text_family = "ArcherPro Book"
) +
ggspatial::annotation_north_arrow(
location = "tl", which_north = "true",
pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
style = ggspatial::north_arrow_nautical(
fill = c("grey40", "white"),
line_col = "grey20",
text_family = "ArcherPro Book"
)
)