【问题标题】:Error when plotting latitude and longitude points on US map in RStudio在 RStudio 中在美国地图上绘制经纬度点时出错
【发布时间】:2021-11-07 04:20:15
【问题描述】:

我有一个数据框,其中包含我想在美国东北部地图上绘制的湖泊的纬度和经度。我正在关注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)

但是当我为包含美国新英格兰几个湖泊的纬度和经度列的数据集个性化代码时,我收到了错误:

我的经纬度数据框:

        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

我不太明白这个错误是什么意思。我错过了什么?

【问题讨论】:

    标签: r ggplot2 latitude-longitude


    【解决方案1】:

    所以,我不知道为什么,但是如果您将 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))
    

    【讨论】:

    • 谢谢,您的更正对我有用! R 有时会如此挑剔和精致
    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2017-10-09
    • 1970-01-01
    • 2019-02-28
    • 2017-09-15
    相关资源
    最近更新 更多