在RStudio中绘制美国地图上的经纬点时出错 [英] Error when plotting latitude and longitude points on US map in RStudio
本文介绍了在RStudio中绘制美国地图上的经纬点时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,上面有我想要在美国东北部地图上绘制的湖泊的纬度和经度。我正在学习tutorial如何创建地图并绘制它们。
我可以很好地运行教程链接中介绍的代码(如下所示)。
library (ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(rgeos)
world <- ne_countries(scale = "medium", returnclass = "sf")
(sites <- data.frame(longitude = c(-80.144005, -80.109), latitude = c(26.479005, 26.83)))
ggplot(data = world) +
geom_point(data = sites, aes(x = longitude, y = latitude), size = 4,
shape = 23, fill = "darkred") +
coord_sf(xlim = c(-88, -78), ylim = c(24.5, 33), expand = FALSE)
但是,当我个性化包含美国新英格兰几个湖泊的经度和纬度列的数据集的代码时,我收到一个错误:
我的Late和Long数据帧:
LAT_DD83 LON_DD83
23 41.37213 -71.56798
34 42.33589 -71.90907
39 41.51963 -71.76691
62 41.78447 -71.64064
76 43.93213 -70.62131
129 41.41433 -71.54638
我的代码:
ggplot(data = world) + geom_sf() +
geom_point(data = lat_lon, aes(x = LON_DD83, y = LAT_DD83), size = 4, shape = 23, fill = "darkred") +
coord_sf(xlim = c(-80, -65), ylim = c(40, 50), expand = FALSE)
收到错误:
#Error in st_cast.POINT(x[[1]], to, ...) : cannot create MULTILINESTRING from POINT
我不太明白这个错误是什么意思。我错过了什么?
推荐答案
所以,我不确定为什么,但如果您将XLIM从80调整为79,它就可以正常工作。我认为这可能是投影的问题,但即使将点转换为sf
对象并与world
的投影匹配,我也会收到与您的数据相同的错误。但调整xlim
似乎起到了作用:
ggplot(data = world) +
geom_sf() +
geom_point(
data = lat_lon,
aes(x = LON_DD83, y = LAT_DD83),
size = 4,
shape = 23,
fill = "darkred"
) +
coord_sf(xlim = c(-79,-65),
ylim = c(40, 50),
expand = FALSE)
输出
数据
lat_lon <-
structure(list(
LAT_DD83 = c(41.37213, 42.33589, 41.51963, 41.78447,
43.93213, 41.41433),
LON_DD83 = c(
-71.56798,-71.90907,-71.76691,
-71.64064,-70.62131,-71.54638
)
),
class = "data.frame",
row.names = c(NA,-6L))
这篇关于在RStudio中绘制美国地图上的经纬点时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文