0

I have a geopackage for India taken from GADM. I can plot all the districts of India by using layer 1, all states using layer 3, and the whole country using layer 4. Here is the code:

> library(rgdal)
Loading required package: sp
rgdal: version: 1.3-3, (SVN revision 759)
 Geospatial Data Abstraction Library extensions to R successfully loaded
 Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
 Path to GDAL shared files: C:/Users/H.P/Documents/R/win-library/3.4/rgdal/gdal
 GDAL binary built with GEOS: TRUE 
 Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
 Path to PROJ.4 shared files: C:/Users/H.P/Documents/R/win-library/3.4/rgdal/proj
 Linking to sp version: 1.3-1 
Warning message:
package ‘rgdal’ was built under R version 3.4.4 
> layers<-ogrListLayers("gdam36_IND.gpkg")
Error in ogrListLayers("gdam36_IND.gpkg") : Cannot open data source
> layers<-ogrListLayers("gadm36_IND.gpkg")
> x<-readOGR("gadm36_IND.gpkg",layers[3])
OGR data source with driver: GPKG 
Source: "C:\Users\H.P\Documents\gadm36_IND.gpkg", layer: "gadm36_IND_1"
with 36 features
It has 10 fields
> plot(x)

Which outputs : enter image description here

I can get the summary of the variable x by:

> summary(x)
Object of class SpatialPolygonsDataFrame
Coordinates:
        min      max
x 68.186249 97.41516
y  6.754256 35.50133
Is projected: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]
Data attributes:
 GID_0      NAME_0        GID_1                    NAME_1  
 IND:36   India:36   IND.1_1 : 1   Andaman and Nicobar: 1  
                     IND.10_1: 1   Andhra Pradesh     : 1  
                     IND.11_1: 1   Arunachal Pradesh  : 1  
                     IND.12_1: 1   Assam              : 1  
                     IND.13_1: 1   Bihar              : 1  
                     IND.14_1: 1   Chandigarh         : 1  
                     (Other) :30   (Other)            :30  
                                                                                                VARNAME_1 
 Ãles Laquedives|Laccadive|Minicoy and Amindivi Islands|Laccadives|Lackadiverna|Lakkadiven|Lakkadi  : 1  
 Agence de la Frontière du Nord-Est(French-obsolete)|North East Frontier Agency                     : 1  
 Andaman & Nicobar Islands|Andaman et Nicobar|Iihas de Andama e Nicobar|Inseln Andamanen und Nikobare: 1  
 Bangla|Bengala Occidentale|Bengala Ocidental|Bengale occidental                                     : 1  
 DAdra et Nagar Haveli|Dadra e Nagar Haveli                                                          : 1  
 (Other)                                                                                             : 9  
 NA's                                                                                                :22  
 NL_NAME_1            TYPE_1             ENGTYPE_1    CC_1        HASC_1  
 NA's:36   State         :28   State          :28   NA's:36   IN.AN  : 1  
           Union Territor: 8   Union Territory: 8             IN.AP  : 1  
                                                              IN.AR  : 1  
                                                              IN.AS  : 1  
                                                              IN.BR  : 1  
                                                              IN.CH  : 1  
                                                              (Other):30  

Is there a way to plot only for a particular state? That is, for example, x$NAME_1=="Chandigarh"?

2

Subset the SpatialPolygonsDataFrame as you would any other data frame:

plot(x[x$NAME_1=="Chandigarh",,drop=FALSE])

should do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.