Como continuação do post Mapas Temáticos usando o R, mostraremos aqui como é possível unir informações pontuais (Point Process) e dados regionais (Lattice Data) além de unir esses dados com o Google maps.
O primeiro passo é a obtenção da malha (shapefile) e dos dados (DadosMapa.csv) para o processo de georeferenciamento.
O primeiro passo é invocar as bibliotecas necessárias bem como definir o Working Directory no qual o arquivo DadosMapa.csv está e também a pasta Shapes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #Limpa o Working Directory rm (list= ls ()) #Define working directory setwd ( "C:\\Blog\\ExemploMapas" ) #Invoca os pacotes necessários library (RColorBrewer) library (maptools) library (rgdal) library (rgeos) library (RgoogleMaps) library (sp) library (spdep) library (ggmap) library (plyr) library (Hmisc) |
Em seguida, importamos para o R os dados e a malha de interesse:
1 2 3 4 | #Importa os dados dados<- read.csv ( "DadosMapa.csv" ) #Lê os shapefiles que estão na pasta Shapes dentro do working directory sfn <- readOGR ( "Shapes" , "11MUE250GC_SIR" ,verbose = FALSE ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #Bounding box a ser utilizada no ggmap b <- bbox (sfn) #Cria uma variável ID para o georeferenciamento sfn@data$id <- rownames (sfn@data) #Define a projeção (Shapefile) sfn <- spTransform (sfn, CRS ( "+proj=longlat +datum=WGS84" )) #Cria um SpatialPointsDataFrame com as latitudes e longitudes dos dados spdf <- SpatialPointsDataFrame (coords=dados[, c ( "Longitude" , "Latitude" )], data=dados) #Define a projeção para os pontos proj4string (spdf) <- CRS ( "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" ) #Trasnforma o objeto espacial em um objeto que pode ser lido pelo ggplot2 sfn.df <- fortify (sfn, region= "CD_GEOCMU" ) #Substring a variável ID para ficar com 6 dígitos (ignora o dígito verificador) sfn.df$V007<- substr (sfn.df$id,1,6) #Left Join do CSV com o Shapefile sfn.df<- merge (sfn.df,dados,by= "V007" ,all.x=T) #Ordena os dados sfn.df<-sfn.df[ order (sfn.df$order), ] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #Aumenta ou diminui a Bounding Box (aumenta em 1%) bbox <- ggmap:: make_bbox (sfn.df$long, sfn.df$lat, f = 0.1) #Escolhe as cores myPalette <- colorRampPalette ( rev ( brewer.pal (11, "Spectral" ))) #Obtêm o mapa do Google Maps (Existem outras possibilidades...) map <- get_map (location=bbox, source= 'google' , maptype = 'terrain' , color= 'bw' ) #Constrói o mapa: map <- ggmap (map, base_layer= ggplot (data=sfn.df, aes (x=long, y=lat)), extent = "normal" , maprange= FALSE ) map <- map + geom_polygon (data=sfn.df, aes (x = long, y = lat, group = group, fill=Valor), alpha = .6) map <- map + geom_path ( aes (x = long, y = lat, group = group), data = sfn.df, colour = "grey50" , alpha = .7, size = .4, linetype=2) map <- map + coord_equal () map <- map + scale_fill_gradientn (colours = myPalette (4)) map<-map+ geom_point ( aes (x=Longitude, y=Latitude),color= "black" , size=1, alpha = 0.70, data=spdf@data) map<-map + ggtitle ( "Postos SINE" ) + labs (x= "Longitude" ,y= "Latitude" ) #Plota o mapa plot (map) |
1 2 3 4 5 6 7 8 9 10 11 | ##Pode-se trocar o código: map <- get_map (location=bbox, source= 'google' , maptype = 'terrain' , color= 'bw' ) ##Do bloco anterior, por algum desses outros: #map <- get_map(location=bbox, source='osm', color='bw')) #map <- get_map(location=bbox, source='stamen', color='watercolor')) #map <- get_map(location=bbox, source='stamen', color='toner')) #map <- get_map(location=bbox, source='stamen', color='terrain')) #map <- get_map(location = bbox, source = 'google', maptype = 'terrain') #map <- get_map(location = bbox, source = 'google', maptype = 'satellite') #map <- get_map(location = bbox, source = 'google', maptype = 'roadmap') #map <- get_map(location = bbox, source = 'google', maptype = 'hybrid') |