2  Análise Exploratória de Dados Espaciais (AED-E)

2.1 Leitura do shapefile

Para a leitura de arquivos shapefile no R, precisamos usar alguns pacotes. Após a instalação dos pacotes, use os seguintes comandos.

# Pacotes
library(sf)
Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(sp)
The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
which was just loaded, will retire in October 2023.
Please refer to R-spatial evolution reports for details, especially
https://r-spatial.org/r/2023/05/15/evolution4.html.
It may be desirable to make the sf package available;
package maintainers should consider adding sf to Suggests:.
The sp package is now running under evolution status 2
     (status 2 uses the sf package in place of rgdal)
# Abra o arquivo 'gm10.shp'
fp_mg.shp <- st_read("data/FP_MG.shp", options = "ENCODING=WINDOWS-1252")
options:        ENCODING=WINDOWS-1252 
Reading layer `FP_MG' from data source 
  `/home/raphael/projects/ecoespacial/data/FP_MG.shp' using driver `ESRI Shapefile'
Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : GDAL
Message 1: organizePolygons() received an unexpected geometry.  Either a
polygon with interior rings, or a polygon with less than 4 points, or a
non-Polygon geometry.  Return arguments as a collection.
Simple feature collection with 66 features and 41 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -51.06258 ymin: -22.91696 xmax: -39.85724 ymax: -14.23725
CRS:           NA
fp_mg.shp <- st_make_valid(fp_mg.shp)
fp_mg.shp <- as_Spatial(fp_mg.shp)
# encoding = "UTF-8"

# Plotar o mapa
plot(fp_mg.shp)

2.2 Atributos do shapefile

Podemos ver a tabela de atributos do shapefile desta forma.

head(fp_mg.shp@data)
  CODMIC      NOMMIC      Q     AC     AP    K   R  RP RNP DRNP   F    AREA
1     41     Aimorés 134310  60132  60132 1833 363 155 208   25  84  8354.1
2     49     Alfenas 564476 113253 113253 7203 312 273  39    8   1  4998.9
3     14    Almenara  84631  20737  20737  783 383 150 233   15   1 15504.5
4     55 Andrelândia  59164  27764  27764 1154 264 200  64   13 227  5047.3
5     12     Araçuaí 159897  36779  36779  552 315 162 153   15   1 10299.4
6     23       Araxá 495102 160580 160643 7582 645 564  81    6 327 14145.6
  ESCTOT POPTOT    LP   ACP    KP   ETOTP   DRNPP       FP     RNPP      RPP
1    347 152658 0.111 0.394 0.012 0.00227 0.00016 0.000550 0.001363 0.001015
2    229 179366 0.111 0.631 0.040 0.00128 0.00004 0.000006 0.000217 0.001522
3    384 213342 0.047 0.097 0.004 0.00180 0.00007 0.000005 0.001092 0.000703
4    191  70783 0.037 0.392 0.016 0.00270 0.00018 0.003207 0.000904 0.002826
5    327 143468 0.143 0.256 0.004 0.00228 0.00010 0.000007 0.001066 0.001129
6    182 158275 0.043 1.015 0.048 0.00115 0.00004 0.002066 0.000512 0.003563
       R_P       QP   X_COORD   Y_COORD CMICRO     VP      VPP    LA   LM     L
1 0.002378 0.879810 -41.38876 -19.53116  31041  78349 0.513232 42737 3997 46734
2 0.001739 3.147062 -46.01852 -21.37551  31049 216064 1.204598 33737 1118 34855
3 0.001795 0.396692 -40.65978 -16.35385  31014  40164 0.188261 30920 4259 35179
4 0.003730 0.835850 -44.44562 -21.95045  31055  42360 0.598449 15135  908 16043
5 0.002196 1.114513 -41.87023 -17.00645  31012  28864 0.201188 45015 7069 52084
6 0.004075 3.128112 -46.95948 -19.59779  31023 197010 1.244732 23442 1488 24930
  CO_RUR  NU_RUR   CO_TOT      EER      K_L VEG TEMP PREC     KL
1   4937 1314045  4402218 0.003757 0.039222   1    4    1 0.0392
2   6736 2957879 12228817 0.002277 0.206656   2    2    3 0.2067
3   1431  282421  3363297 0.005067 0.022258   2    5    1 0.0223
4   1717  498819  2819950 0.003442 0.071932   2    1    4 0.0719
5   1431  365829  2404595 0.003912 0.010598   3    4    1 0.0106
6   4357 1476959 14220985 0.002950 0.304132   1    2    4 0.3041

2.3 Mapa

Podemos produzir um mapa colorido com os seguintes comandos.

p <- colorRampPalette(c("white", "blue"))(128)
palette(p)
plot(fp_mg.shp, col = fp_mg.shp@data$Q)

2.3.1 Sua vez

Faça um mapa com a variável AC com a cor vermelha.

Código
p <- colorRampPalette(c("yellow", "red"))(128)
palette(p)
plot(fp_mg.shp, col = fp_mg.shp@data$AC)