简体   繁体   English

在ggplot中绘制nb对象?

[英]Plotting nb object in ggplot?

I would like to plot the outline of UK, along with an nb object that I generated from a spatial points dataframe. 我想绘制英国的轮廓,以及我从空间点数据框生成的nb对象。

The problem is that the outline of UK takes really, really long to plot --it keeps crashing my Rstudio. 问题在于,英国的轮廓非常非常长,因为我的Rstudio不断崩溃。 This for example, either take really long to load or my Rstudio just stops responding. 例如,要么加载很长时间,要么我的Rstudio停止响应。

library(raster)    
UK_gadm <- getData("GADM", country="GB", level=0)
    plot(UK_gadm)

So I resorted to using ggplot2 from this solution where I can get the outline of UK like in a fraction of a second with the following commands: 所以我使用了这个解决方案中的ggplot2,我可以通过以下命令在几分之一秒内得到英国的轮廓:

library(ggplot2)
UK <- map_data(map = "world", region = "UK") # changed map to "world"
    ggplot(data = UK, aes(x = long, y = lat, group = group)) + 
      geom_polygon() +
      coord_map()

The issue now is that I would like the nb object to be plotting against the backdrop of the outline of UK -- however, this seems only achievable in base R like for example : 现在的问题是,我希望nb对象能够在英国大纲的背景下进行绘图 - 但是,这似乎只能在基础R中实现, 例如

plot(orotl.shp, xlim=c(-125, -115), ylim=c(42,47))
plot(orstationc.neighbors.dist, orstationc.loc, add=T, lwd=2, col="blue")

Is there any way I could plot nb objects in ggplot or is there a way for R to plot the outline of UK without crashing my computer with the base R plot function? 有没有什么方法可以在ggplot中绘制nb个对象,或者有没有办法让R绘制英国的轮廓而不会使我的计算机与基础R绘图功能崩溃?

Managed to find a fast, simple solution after a whole night of effort. 经过一整夜的努力,管理找到一个快速,简单的解决方案。 Hope this helps someone else with a similar issue. 希望这可以帮助其他有类似问题的人。

Just to elaborate on the goal: plot a neighbours object (nb) against a shapefile. 只是详细说明目标:针对shapefile绘制邻居对象(nb)。 This is to visualise the linkages among certain coordinates. 这是为了可视化某些坐标之间的联系。 After some googling, I think this can only be done with base R's plot function. 经过一些谷歌搜索后,我认为这只能通过基础R的绘图功能来完成。 The problem however, was loading a country's shapefile (downloaded from official sources/gadm)-- its too big. 然而问题是加载一个国家的shapefile(从官方来源/ gadm下载) - 它太大了。

To solve this issue, get a more generalised, simple map of the country via the maps package, turn it into a shapefile and then plot it alongside the neighbours object. 要解决此问题,请通过maps包获取更加通用,简单的国家/地区地图,将其转换为shapefile,然后将其与邻居对象一起绘制。

Here's the code: 这是代码:

library(spdep)

# get your neighbour object from your spatial points df
rest_neighbours <- dnearneigh(rest_spdf,0,1)

library(maps)

# get boundary of UK
UK_map <- sf::st_as_sf(maps::map(database='world',regions='UK', plot = FALSE, fill = TRUE))

# write to shapefile
st_write(UK_map, 'shapefiles/UK.shp')

# henceforth, we can just call the shapefile 
UK <- readOGR('shapefiles/UK.shp')

# plot the boundary and the neighbours
plot(UK)
plot(rest_neighbours, rest_coords, add=T, lwd=2, col="blue")

I did not realise that official boundary files are often really detailed which also means that they are really huge and I'm glad that there's ready-made watered down versions of the maps available in the maps package of r. 我没有意识到官方边界文件通常非常详细,这也意味着它们非常庞大,我很高兴r的地图包中有现成的淡化版本的地图。 (Sorry if you already knew -- I'm still learning!) (对不起,如果你已经知道了 - 我还在学习!)

Hope this helps anyone else! 希望这有助于其他任何人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM