R: Import maps shapefile format to R

1

With these packages:

        library(readr)
        library(leaflet)
        library(magrittr)
        library(ggmap)
        library(ggrepel)

I would like to be able to put these putos on a map with greater relief following this script

        puntos <- data.frame(lon=c(-3.391830, -3.530613, -3.205903, -3.137145, -3.500323,-3.536191, -3.196057,  -3.131139,  -3.345220, -3.476462,   -3.125794,  -3.252617,  -3.345220,  -3.503040,  -3.125000,  -3.182801,  -3.476076,  -3.131000,  -3.593308,  -3.599064,  -3.477574,  -3.214026,  -3.253943),
                  lat=c(39.595520, 40.225155, 40.320294, 40.345348, 40.059138,  40.234966,  40.089637,  40.494167,  40.03184,   40.048758,  40.050352,  40.245459,  40.03184,   40.050578,  40.0459,    40.032298,  40.050052,  40.04472,   40.051302,  40.043193,  40.04933,   40.085899,  40.245852),
                  nom=c('Q1', 'Q2', 'Q3', 'Q4','E1', 'E2', 'E3', 'E4',
                       'L1', 'L2', 'L3', 'L4','M1', 'M2', 'M3', 'M4',
                      'Z1', 'Z2','C1', 'C2', 'H1', 'H2', 'H3'),
                 habitat=c('Oak', 'Oak', 'Oak', 'Oak','Wasteland', 'Wasteland', 'Wasteland', 'Wasteland',
                       'Edge', 'Edge', 'Edge', 'Edge','Crop', 'Crop', 'Crop', 'Crop',
                       'Crop', 'Crop','Crop', 'Crop', 'Crop', 'Crop', 'Crop'))

        puntos<-as.data.frame(puntos)

        map.unizar <- get_map( location = c(-3.4, 40.1),
                   color = "color",
                   maptype = "terrain",
                   scale = 2,
                   zoom = 9)

        ggmap(map.unizar) + geom_point(aes(color=habitat),
                           data = puntos,
                           size = 2)                                                                                                                                                                    

         +scale_x_continuous("longitude")+scale_y_continuous("latitude")+
           geom_text_repel(aes(label = puntos$nom),
                          data = puntos, 
                          size = 3, 
                          #vjust = 0.75, 
                          #hjust = -0.3,
                          color = "black")

So my question would be: How can I import a map in shapefile format in R to do the same treatment of the script that I summarized earlier? This is the shapefile:

link

Thanks in advance.

    
asked by Adrián P.L. 15.02.2018 в 13:38
source

1 answer

1

The first step would be to import the shapefile, making it the path in an object of the class SPDF (Spatial Polygon DataFrame). This is a special class of R objects that is used to handle cartographic polygons. To be able to use them you should have installed the libraries sp , rgdal and rgeos . They have dependencies among themselves, in general R resolves them. Depending on the operating system you are using, you may have some complication, because there are dependencies at the operating system level that R can not resolve. In my experience it is relatively easy in Windows and it gives something more war in Linux (Fedora) and macOS. It is not impossible either.

With the packages installed and running you have to have all the contents of the .zip with the shapefiles in a folder, not just the .shp file. The projection and other metadata are in separate files. All have to have the same name and only change the extension, in your case the data is already good in this regard.

Ideally, you place them in a subfolder of the R project you are working on, in order to work with relative path. In this case I use ./CORINE .

With this ready, you can import the shapes to an object SPDF with the function readOGR of rgdal .

library(rgdal)
readOGR("./CORINE") -> poligonos   #Importo los polígonos
#readOGR me pasa una advertencia señalando que ignoró la dimensión Z. Asumo que no cargó los relieves, no sé cuan problemático es eso para tus propósitos. 
plot(poligonos)                    #Mapa básico, para ver si todo anda bien. Hay un método plot para objetos SPDF.

So far with the import of the data. The rest of the question I do not understand it well, that is, I do not know if you want to add the CORINE layer on a map of google maps (the one obtained with ggmap in the example script) or put the points of puntos on the map of CORINE.

Note on the data.

It seems to me that there is a problem with the data imported from CORINE, the latitudes and longitudes that result are very rare (the Bay of Biscay goes through the 4800000 parallel). They may be using an unconventional coordinate system (for example, they use INEGI in Mexico, use their own projection that expresses the coordinates in meters and not degrees, and zero defines it with their own datum). I suggest you carefully review the technical documents of the organization that produces those shapefiles. If it's like I think it's going to be difficult to merge the different layers, because you would have to do a "translation" to standardize the coordinate systems.

A notebook about this problem and some basic examples of R mapping are in link It's a guide I prepared some time ago for my students.

    
answered by 17.02.2018 / 03:03
source