【问题标题】:Plotting graphs from class `nb` - How to change points color?从类`nb`绘制图形-如何更改点颜色?
【发布时间】:2019-11-13 14:47:33
【问题描述】:

通过以下可重现的示例更容易解释我想要做什么:

library(sf)
library(brazilmaps)
library(spdep)

# loading cities map
cities_mg <- brazilmaps::get_brmap(geo        = 'City', 
                                   geo.filter = list(State = 31),
                                   class      = 'sf')

# creating adjacency matrix (neighborhood graph)
nb_mg <- spdep::poly2nb(cities_mg)

# plotting
coords_mg <- st_coordinates(st_centroid(st_geometry(cities_mg)))

par(bg = '#2E2E2E')
plot(nb_mg, coords_mg, pch = 19, cex = .6, lwd = .4, 
     col = '#00C040', points = T)

这段代码生成如下图

我想做的是用与边缘相同的颜色为节点(或顶点)着色。 有可能吗?

提前谢谢你。

下面的会话信息(用于重现性)。

R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 19.04

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] spdep_1.1-3      spData_0.3.2     sp_1.3-1         brazilmaps_0.1.0 sf_0.8-0        

loaded via a namespace (and not attached):
 [1] gtools_3.8.1       tidyselect_0.2.5   xfun_0.10          purrr_0.3.3        splines_3.6.1      lattice_0.20-38    expm_0.999-4      
 [8] htmltools_0.4.0    XML_3.98-1.20      rlang_0.4.1        pillar_1.4.2       e1071_1.7-2        glue_1.3.1         withr_2.1.2       
[15] DBI_1.0.0          semver_0.2.0       binman_0.1.1       caTools_1.17.1.2   coda_0.19-3        evaluate_0.14      knitr_1.25        
[22] wdman_0.2.4        callr_3.3.2        ps_1.3.0           class_7.3-15       Rcpp_1.0.2         KernSmooth_2.23-16 clipr_0.7.0       
[29] openssl_1.4.1      classInt_0.4-1     gdata_2.18.0       RSelenium_1.7.5    deldir_0.1-23      fs_1.3.1           askpass_1.1       
[36] digest_0.6.22      gmodels_2.18.1     processx_3.4.1     dplyr_0.8.3        grid_3.6.1         tools_3.6.1        bitops_1.0-6      
[43] LearnBayes_2.15.1  magrittr_1.5       tibble_2.1.3       crayon_1.3.4       pkgconfig_2.0.3    whisker_0.4        MASS_7.3-51.4     
[50] Matrix_1.2-17      reprex_0.3.0       rstudioapi_0.10    assertthat_0.2.1   rmarkdown_1.16     R6_2.4.0           boot_1.3-23       
[57] units_0.6-5        nlme_3.1-141       compiler_3.6.1    

【问题讨论】:

  • 我无法根据您的代码生成图形。我认为最后的情节线有问题。
  • 您无法重现,因为此功能存在错误。此外,我们应该将points = F 替换为points = T。我正在编辑它以使其可重现。谢谢
  • 这需要spdep 的最新版本才能重现,因为直到最近spdep 才返回sf 对象,只有sp 对象会破坏st_etc 函数.

标签: r sf spdep


【解决方案1】:

这是一个技巧。 plot.nb 调用points 来绘制点,这是一个 S3 方法。因此,如果我们可以让它在一个我们可以为其定义一个类的对象上运行,我们就可以让它做任何事情。

目前它从st_coordinates 获取一个两列矩阵,plot.nb 函数获取这两列并将它们提供给points(x,y,...)。如果我们改为从矩阵构造一个 2 列数据框:

coords_mg = data.frame(coords_mg[,1],coords_mg[,2])

那么它仍然可以工作。如果我们给第一列一个新的类:

class(coords_mg[,1]) = "adjpts"

那么points(x,y,...)会调用adjpts的方法,我们定义如下:

points.adjpts=function(x,y,...){
  points(unclass(x),unclass(y),col="#00c040",pch=19)
}

然后做:

plot(nb_mg, coords_mg, pch = 19, cex = .6, lwd = .4,col = '#00C040', points = T)

魔术会导致:

【讨论】:

  • 哦!这真的很酷!我认为用points(unclass(x),unclass(y),col="#00c040", ...) 替换points(unclass(x),unclass(y),col="#00c040",pch=19) 会给我们更多的灵活性。
【解决方案2】:

我能拍到的最好的照片如下。您想使用pch = 21bg = '#00C040'。然后您可以在点中看到相同的颜色。我唯一无法删除的是圆圈的外黑线。

plot(cities_mg['geometry'], border = 'black', bg = 'white', lwd = .4, cex = .4)
plot.nb(x = nb_mg, coords = coords_mg, pch = 21,
        cex = 1, lwd = .4, col = '#00C040', bg = '#00C040',
        points = TRUE, add = TRUE)

【讨论】:

  • plot.nb的源代码写得很清楚,问题是任何col=参数都只用于画线,传递给points的只有@ 987654329@ 参数。您可以编辑代码并添加一个新参数 - 也许是 pcol - 传递给最后一行的 points 调用...
  • @Spacedman 感谢您的反馈。当我看到帮助页面时,我认为没有太多空间可以移动,这就是我可以做的。感谢您帮助我思考在这种情况下我可以如何四处走动。为诀窍 +1!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 2012-12-14
相关资源
最近更新 更多