【问题标题】:How to prevent ggplot2 (GADM) from plotting districts outside the selected State如何防止 ggplot2 (GADM) 绘制所选州以外的地区
【发布时间】:2022-01-21 10:11:31
【问题描述】:

我正在使用 GADM 绘制印度马哈拉施特拉邦的地图。我使用了一个地区名称的子集来映射这些地区。但是我发现其他州的同名区也在画。我该如何解决这个问题。 谢谢

我使用以下代码来准备情节。

 library(ggplot2)
    library(broom)
    library(scales)
    library(sp)
    library(maptools)

ind2 <-  getData("GADM", country = "IND", level = 2)

ind2_df <- tidy(ind2, region = "NAME_2") 
  
mh <- subset (ind2_df, 
                  id == 'Ahmadnagar' |
                  id == 'Gadchiroli' |
                  id == 'Latur' |
                  id == 'Parbhani' |
                  id == 'Mumbai Suburban' |
                  id == 'Akola' |
                  id == 'Amravati' |
                  id == 'Aurangabad' |
                  id == 'Bid' |
                  id == 'Bhandara' |
                  id == 'Buldana' |
                  id == 'Chandrapur'|
                  id == 'Dhule'|
                  id == 'Gondia'| 
                  id == 'Hingoli'|
                  id == 'Jalgaon'|
                  id == 'Jalna'|
                  id == 'Kolhapur'|
                  id == 'Mumbai City'|
                  id == 'Nagpur'|
                  id == 'Nanded'|
                  id == 'Nandurbar'|
                  id == 'Nashik'|
                  id == 'Osmanabad'|
                  id == 'Palghar'|
                  id == 'Pune'|
                  id == 'Raigarh'|
                  id == 'Ratnagiri'|
                  id == 'Sangli'|
                  id == 'Satara'|
                  id == 'Sindhudurg'|
                  id == 'Solapur'|
                  id == 'Thane'|
                  id == 'Wardha'|
                  id == 'Washim'|
                  id == 'Yavatmal')

                

质心数据,我使用上述相同的标准进行转租。

centroid <-  as.data.frame(coordinates(ind2))
colnames(centroid) = c("long","lat")
centroid$id <-  ind2@data$NAME_2
centroid <- subset ( SAME AS the ABOVE)

  
plot2 <- ggplot()+
  geom_polygon(data = mh, aes( long, lat, group = group,  fill = id ))+
  geom_text(data = centroid, aes(x = long, y = lat, label = id),
            size = 4,
            check_overlap = T)+
  theme_void()+
  theme(aspect.ratio=1)
                                                                                                                                        

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    使用sf简单特征方法可能更好...

    根据 OP 评论修改答案。

    library(ggplot2)
    library(dplyr)
    library(sp)
    library(sf)
    library(raster) # getData
    
    
    ind_sf <- 
      getData("GADM", country = "IND", level = 2) %>% 
      st_as_sf() %>% 
      filter(NAME_1 == "Maharashtra") %>% 
      mutate(lab_x =  )
    
    
    
    ggplot(ind_sf)+
      geom_sf(aes(fill = NAME_2))+
      geom_sf_text(aes(label = NAME_2),
                   size = 3,
                   fun.geometry = st_centroid)+
      theme_void()+
      theme(aspect.ratio = 1,
            legend.position = "bottom")
    

    reprex package (v2.0.1) 于 2021-12-19 创建

    【讨论】:

    • 谢谢,我对远离该州(马哈拉施特拉邦)的那两个地区(奥兰加巴德和赖加尔)有意见。我想删除这两个区。这两个区属于其他一些州。他们出现在这里可能我们在不同的州有同名的地区。如何删除这两个不属于该州的地区。
    • 感谢您的解释。请参阅修改后的答案。
    • 谢谢,我学到的比我想要的要多。
    猜你喜欢
    • 2021-07-22
    • 1970-01-01
    • 2023-03-22
    • 2015-07-13
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多