Basic functions
Archvios en el directorio dir()
|
Archivos en carpeta dir("folder")
|
require(dgdal) |
Cargar cartografía map=readOGR("folder_address","name", stringsAsFactors = FALSE)
|
Check structure of map View(map) View(map@data)
|
Map summary summary(map)
|
Plot map plot(map)
|
Add borders to map require(tmap) tm_shape(CCAA_MAP)+ tm_borders()
|
Dynamic representation
require(rgdal) |
require(leaflet) |
map=readOGR("folder","map_name") |
leaflet(map, options = leafletOptions(attributionControl = FALSE)) %>% addPolygons(data=CCAA_MAP, stroke=TRUE, opacity = 0.5, fillOpacity = 0.7,color="grey10", fillColor = ~colorQuantile("YlOrBr", n=9, SALARIO, na.color = "white")(SALARIO)) |
leaflet(CCAA_MAP,options = leafletOptions(attributionControl = FALSE)) %>% addTiles()%>% addPolygons(data=CCAA_MAP, stroke=TRUE, color="grey10") Cargamos el OpenStreetMap (OSM)
|
leaflet(CCAA_MAP,options = leafletOptions(attributionControl = FALSE)) %>% addTiles()%>% addPolygons(data=CCAA_MAP, stroke=TRUE, opacity = 0.25, fillOpacity = 0.27,color="grey10", fillColor = ~colorQuantile("YlOrBr", n=9, SALARIO, na.color = "white")(SALARIO)) Realizamos la representación dinámica + OSM
|
library(tmap) tmap_mode("view") |
tm_shape(CCAA_MAP) + tm_fill(palette ="Blues",col = "SALARIO",style = "quantile") Otra opción sin OpenStreetMap
|
Localización de datos espaciales
datos_map<-data.frame( longx= c(-3.741274,-3.718765,-3.707027), laty=c(40.38479, 40.36751, 40.45495)) Para situar marcadores
|
marker_icon <- makeIcon( iconUrl = "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.8.0-beta.0/images/marker-icon.png", shadowUrl = "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.8.0-beta.0/images/marker-shadow.png") Obtenemos los marcadores del repositorio de la libería leaflet
|
leaflet(data=datos_map) %>% addTiles()%>% addMarkers(data=datos_map,lng=longx, lat=laty, icon = marker_icon) # Para integrarlo con la librería leaflet
|
|
|
Add info from csv to map
Load csv new_data=read.csv("new_data.csv",sep = ";")
|
Check info View(new_data)
|
Add data to map require(dplyr) map@data=dplyr::left_join(map@data,new_data, by=c("map_col"="new_data_col"))
|
Check new data View(CCAA_MAP@data)
|
Adding data changes row names, so we change them back rownames(map@data)=map@data$map_col
|
Cambiar el nombre de los polígonos para que sean igual que tengan el mismo nombre que las filas de los datos mapa new_IDs = rownames(map@data) for (i in 1:length(slot(map, "polygons"))){ slot(slot(map, "polygons")[[i]], "ID") = new_IDs[i] }
|
Mapa con los nuevos datos require(tmap) tm_shape(map) + tm_borders() tm_shape(map) + tm_polygons(col = "SALARIO")
|
Cambiar la paleta de colores tm_shape(CCAA_MAP) + tm_borders() + tm_fill(palette ="Blues",col = "SALARIO", style = "quantile")
|
Más información tm_shape(CCAA_MAP) + tm_fill(palette ="Blues",col = "SALARIO",style = "quantile")+ tm_bubbles(size = "SALARIO",scale=1,style = "quantile", col = "darkblue")
|
Cambiar algunas opciones de leyenda map1 = tm_shape(CCAA_MAP) + tm_fill(palette ="Blues",col = "SALARIO",style = "quantile")+ tm_bubbles(size = "SALARIO",scale=1,style = "quantile", col = "SALARIO")+ tm_layout(legend.title.size = .7, legend.text.size =0.6, legend.position = c("right","bottom"), legend.bg.color = "white", legend.bg.alpha = 1, legend.stack = "horizontal", legend.width = 1.5, legend.height = 1.5) map1
|
guardar la nueva cartografía writeOGR(obj=map, dsn="folder", layer="CCAA_SALARIOS", driver="ESRI Shapefile")
|
guardar la imagen en formato jpeg tmap_save(map1, filename="map1.jpeg", width=15, height=10, units="cm")
|
Añadir fichero con localizaciones
load(file="datos_CCAA/Data_Housing_Madrid.RData") # Ojo con el directorio View(Datos_house) Cargamos datos desde fichero
|
par(mfrow=c(1,1)) hist(Datos_house$house.price) acemos un histograma para ver la distribución del precio de la vivienda en Madrid
|
mapa_pisos = leaflet(data=Datos_house[sample(nrow(Datos_house),100),]) %>% addTiles() %>% # Add default OpenStreetMap map tiles addMarkers(lng=longitude, lat=latitude, icon = marker_icon, popup=~paste0(type.house, " - ", house.price, " euros")) Localizamos dichos datos y lo integramos con la librería leaflet
|
mapa_pisos Representamos dinámicamente el objeto creado antes
|
|
|
More tools
Color map based on information already contained tm_shape(CCAA_MAP) + tm_borders(col = "black")+ tm_shape(Munic_ESP)+ tm_fill(col="PrecioIn16", style = "quantile" )
|
Cambiar sistema de referencia require(rgdal) CRS.new = CRS("+init=epsg:4258") Munic_ESP = spTransform(Munic_ESP, CRS.new) View(Munic_ESP@data)
|
Loading maps alternative
require(sf) |
map=st_read("map.shp") |
View(CCAA_MAP) Visualizamos su estructura
|
summary(CCAA_MAP) |
class(CCAA_MAP) Clase del objeto creado (data.frame)
|
Lista de geometría st_geometry(CCAA_MAP)
|
Una de las geometrías st_geometry(CCAA_MAP)[[13]]
|
methods(class='sfc') Operaciones con geometría
|
map2 = cbind(map, st_coordinates(st_centroid(map))) Obtain centroid of map
|
plot(CCAA_MAP[1]) Map of each variable
|
require(ggplot2) ggplot(data=CCAA_MAP)+ &nsbp; geom_sf()+ &nsbp; theme_minimal() Another option to style maps
|
salarios=read.csv("datos_CCAA/SALARIOS.csv",sep = ";") |
require(dplyr) |
CCAA_MAP<-dplyr::left_join(CCAA_MAP,salarios, by=c("cod_CCAA"="COD_CCAA")) |
View(CCAA_MAP) |
require(tmap) |
require(RColorBrewer) |
tm_shape(CCAA_MAP) + tm_borders() |
tm_shape(CCAA_MAP) + tm_polygons(col = "SALARIO") |
tm_shape(CCAA_MAP) + tm_fill(palette ="Blues",col = "SALARIO",style = "quantile") |
tm_shape(CCAA_MAP) + tm_fill(palette ="Blues",col = "SALARIO",style = "quantile")+ tm_bubbles(size = "SALARIO",scale=1,style = "quantile", col = "SALARIO") |
st_write(obj=CCAA_MAP,dsn = "cartografías/CCAA_SALARIOS.shp", append=FALSE) Plotting new info changes a bit
|
|