【问题标题】:Is there an issue with how I'm reading in my shapefile and plotting it?我在 shapefile 中读取和绘制它的方式有问题吗?
【发布时间】:2021-04-14 17:17:10
【问题描述】:

我试图读入我的形状文件然后绘制它,但似乎 RStudio 卡在了某些东西上,并且无法完成绘图功能的运行。现在我有:

library(rgdal)
new_county_path <- paste(county_path, "tl_2014_us_county.shp",sep='/')
county1 <- readOGR(new_county_path)
plot(county1)

但它不会产生情节,而且它似乎一直被困在某些东西上,正如它所说的那样

>plot(county1)

在 R 控制台中。我做错了什么导致这种情况,有没有更好的方法来读取和绘制 shapefile?

【问题讨论】:

  • 您查看county1 是否正常?例如。 str(county1)
  • 您是否有其他图形“设备”处于活动状态?在过去,我有一个失败的渲染留下一个pdfpng 图形坐在堵塞作品。 dev.list() 显示什么?我在 Windows 上,而在 RStudio 中 not 我只看到 windows\n2(一个设备);在RStudio的yes中,我看到RStudioGD\n2png\n3(两个设备),这在RStudio中是正常的。
  • 这是一个感觉不错的空间多边形数据框

标签: r shapefile geostatistics


【解决方案1】:

如果没有可重现的示例,我无法说明您的代码为何不起作用。如果您有一个有效的 shapefile,您应该能够使用您提供的代码将其读入并绘制它:

# first get and save a shapefile to make the code easily reproducible
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
st_write(obj = nc, dsn = 'test/nc.shp')
# now there's a shapefile named "nc.shp" saved in the "test" folder.

# the functions you're using will work on a valid path to a valid shapefile:
library(rgdal)
nc1 <- readOGR("test/nc.shp")
plot(nc1)

我通常使用sf 包,它提供了更多的多功能性(particularly for plotting):

library(sf)
nc2 <- st_read("test/nc.shp")
plot(st_geometry(nc2)) 

如果您的代码不起作用,则可能是您提供给 readOGR 函数的路径或 shapefile 本身存在问题。

【讨论】:

    猜你喜欢
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多