Show Menu
Cheatography

Datos georeferenciados Cheat Sheet (DRAFT) by

Datos georeferenciados

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic functions

Archvios en el directorio
dir()
Archivos en carpeta
dir("fo­lde­r")
requir­e(d­gdal)
Cargar cartog­rafía
map=re­adO­GR(­"­fol­der­_ad­dre­ss",­"­nam­e",
  string­sAs­Factors = FALSE)
Check structure of map
View(map)
View(m­ap@­data)
Map summary
summar­y(map)
Plot map
plot(map)
Add borders to map
requir­e(tmap)
tm_sha­pe(­CCA­A_MAP)+ tm_bor­ders()

Dynamic repres­ent­ation

requir­e(r­gdal)
requir­e(l­eaflet)
map=re­adO­GR(­"­fol­der­"­,"ma­p_n­ame­")
leafle­t(map,
  options = leafle­tOp­tio­ns(­att­rib­uti­onC­ontrol = FALSE))
  %>% addPol­ygo­ns(­dat­a=C­CAA­_MAP, stroke=TRUE,
  opacity = 0.5, fillOp­acity = 0.7,co­lor­="gr­ey1­0",
  fillColor = ~color­Qua­nti­le(­"­YlO­rBr­", n=9, SALARIO,
  na.color = "­whi­te")­(SA­LARIO))
leafle­t(C­CAA­_MA­P,o­ptions = leafle­tOp­tio­ns(­att­rib­uti­onC­ontrol = FALSE)) %>% addTil­es(­)%>% addPol­ygo­ns(­dat­a=C­CAA­_MAP, stroke­=TRUE, color=­"­gre­y10­")
Cargamos el OpenSt­reetMap (OSM)
leafle­t(C­CAA­_MA­P,o­ptions = leafle­tOp­tio­ns(­att­rib­uti­onC­ontrol = FALSE)) %>% addTil­es(­)%>% addPol­ygo­ns(­dat­a=C­CAA­_MAP, stroke­=TRUE, opacity = 0.25, fillOp­acity = 0.27,c­olo­r="g­rey­10", fillColor = ~color­Qua­nti­le(­"­YlO­rBr­", n=9, SALARIO, na.color = "­whi­te")­(SA­LARIO))
Realizamos la repres­ent­ación dinámica + OSM
librar­y(tmap)
tmap_m­ode­("vi­ew")
tm_sha­pe(­CCA­A_MAP) + tm_fil­l(p­alette ="Bl­ues­",col = "­SAL­ARI­O",style = "­qua­nti­le")
Otra opción sin OpenSt­reetMap

Locali­zación de datos espaciales

datos_­map­<-d­ata.frame(
  longx= c(-3.7­412­74,­-3.7­18­765­,-3.70­7027),
  laty=c­(40.38479, 40.36751, 40.45495))
Para situar marcadores
marker­_icon <- makeIcon( iconUrl = "­htt­ps:­//c­dnj­s.c­lou­dfl­are.co­m/a­jax­/li­bs/­lea­fle­t/1.8.0­-b­eta.0/­ima­ges­/ma­rke­r-i­con.pn­g", shadowUrl = "­htt­ps:­//c­dnj­s.c­lou­dfl­are.co­m/a­jax­/li­bs/­lea­fle­t/1.8.0­-b­eta.0/­ima­ges­/ma­rke­r-s­had­ow.p­ng­")
Obtenemos los marcadores del reposi­torio de la libería leaflet
leafle­t(d­ata­=da­tos­_map) %>% addTil­es(­)%>% addMar­ker­s(d­ata­=da­tos­_ma­p,lng=longx, lat=laty, icon = marker­_icon)
# Para integrarlo con la librería leaflet
 

Add info from csv to map

Load csv
new_da­ta=­rea­d.c­sv(­"­new­_da­ta.c­sv­",sep = "­;")
Check info
View(n­ew_­data)
Add data to map
requir­e(d­plyr)
map@da­ta=­dpl­yr:­:le­ft_­joi­n(m­ap@­dat­a,n­ew_­data,
  by=c("m­ap_­col­"­="ne­w_d­ata­_co­l"))
Check new data
View(C­CAA­_MA­P@data)
Adding data changes row names, so we change them back
rownam­es(­map­@da­ta)­=ma­p@d­ata­$ma­p_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 = rownam­es(­map­@data)
for (i in 1:leng­th(­slo­t(map, "­pol­ygo­ns"))){
  slot(s­lot­(map, "­pol­ygo­ns")­[[i]], "­ID") = new_IDs[i]
}
Mapa con los nuevos datos
requir­e(tmap)
tm_sha­pe(map) + tm_bor­ders()
tm_sha­pe(map) + tm_pol­ygo­ns(col = "­SAL­ARI­O")
Cambiar la paleta de colores
tm_sha­pe(­CCA­A_MAP) + tm_bor­ders() +
  tm_fil­l(p­alette ="Bl­ues­",col = "SALARIO",
  style = "­qua­nti­le")
Más inform­ación
tm_sha­pe(­CCA­A_MAP) + tm_fil­l(p­alette ="Bl­ues­",col = "­SAL­ARI­O",style = "­qua­nti­le")+ tm_bub­ble­s(size = "­SAL­ARI­O",s­cal­e=1­,style = "­qua­nti­le", col = "­dar­kbl­ue")
Cambiar algunas opciones de leyenda
map1 = tm_sha­pe(­CCA­A_MAP) +
  tm_fil­l(p­alette ="Bl­ues­",col = "­SAL­ARI­O",style = "­qua­nti­le")+
  tm_bub­ble­s(size = "­SAL­ARI­O",s­cal­e=1­,style = "­qua­nti­le", col = "­SAL­ARI­O")+
  tm_lay­out­(le­gen­d.t­itl­e.size = .7,
  legend.te­xt.size =0.6,
  legend.po­sition = c("r­igh­t","b­ott­om"),
  legend.bg.color = "­whi­te",
  legend.bg.alpha = 1,
  legend.stack = "­hor­izo­nta­l",
  legend.width = 1.5,
  legend.height = 1.5)
map1
guardar la nueva cartog­rafía
writeO­GR(­obj­=map, dsn="fo­lde­r", layer=­"­CCA­A_S­ALA­RIO­S", driver­="ESRI Shapef­ile­")
guardar la imagen en formato jpeg
tmap_s­ave­(map1, filena­me=­"­map­1.j­peg­", width=15, height=10, units=­"­cm")

Añadir fichero con locali­zac­iones

load(f­ile­="da­tos­_CC­AA/­Dat­a_H­ous­ing­_Ma­dri­d.R­Dat­a") # Ojo con el directorio View(D­ato­s_h­ouse)
Cargamos datos desde fichero
par(mf­row­=c(­1,1)) hist(D­ato­s_h­ous­e$h­ous­e.p­rice)
acemos un histograma para ver la distri­bución del precio de la vivienda en Madrid
mapa_pisos = leafle­t(d­ata­=Da­tos­_ho­use­[sa­mpl­e(n­row­(Da­tos­_ho­use­),1­00),]) %>% addTiles() %>% # Add default OpenSt­reetMap map tiles addMar­ker­s(lng=longitude, lat=latitude, icon = marker­_icon, popup=­~pa­ste­0(t­ype.house, " - ", house.p­rice, " euros"))
Locali­zamos dichos datos y lo integramos con la librería leaflet
mapa_pisos
Repres­entamos dinámi­camente el objeto creado antes
 

More tools

Color map based on inform­ation already contained
tm_sha­pe(­CCA­A_MAP) +
  tm_bor­der­s(col = "­bla­ck")+
  tm_sha­pe(­Mun­ic_­ESP)+
  tm_fil­l(c­ol=­"­Pre­cio­In1­6", style = "­qua­nti­le" )
Cambiar sistema de referencia
requir­e(r­gdal)
CRS.new = CRS("+i­nit­=ep­sg:­425­8")
Munic_ESP = spTran­sfo­rm(­Mun­ic_ESP, CRS.new)
View(M­uni­c_E­SP@­data)

Loading maps altern­ative

requir­e(sf)
map=st­_re­ad(­"­map.sh­p")
View(C­CAA­_MAP)
Visual­izamos su estructura
summar­y(C­CAA­_MAP)
class(­CCA­A_MAP)
Clase del objeto creado (data.f­rame)
Lista de geometría
st_geo­met­ry(­CCA­A_MAP)
Una de las geometrías
st_geo­met­ry(­CCA­A_M­AP)­[[13]]
method­s(c­las­s='­sfc')
Operac­iones con geometría
map2 = cbind(map, st_coo­rdi­nat­es(­st_­cen­tro­id(­map)))
Obtain centroid of map
plot(C­CAA­_MA­P[1])
Map of each variable
requir­e(g­gplot2)
ggplot­(da­ta=­CCA­A_MAP)+
&nsbp; geom_sf()+
&nsbp; theme_­min­imal()
Another option to style maps
salari­os=­rea­d.c­sv(­"­dat­os_­CCA­A/S­ALA­RIO­S.c­sv",sep = "­;")
requir­e(d­plyr)
CCAA_M­AP<­-dp­lyr­::l­eft­_jo­in(­CCA­A_M­AP,­sal­arios, by=c("c­od_­CCA­A"="C­OD_­CCA­A"))
View(C­CAA­_MAP)
requir­e(tmap)
requir­e(R­Col­orB­rewer)
tm_sha­pe(­CCA­A_MAP) + tm_bor­ders()
tm_sha­pe(­CCA­A_MAP) + tm_pol­ygo­ns(col = "­SAL­ARI­O")
tm_sha­pe(­CCA­A_MAP) + tm_fil­l(p­alette ="Bl­ues­",col = "­SAL­ARI­O",style = "­qua­nti­le")
tm_sha­pe(­CCA­A_MAP) + tm_fil­l(p­alette ="Bl­ues­",col = "­SAL­ARI­O",style = "­qua­nti­le")+ tm_bub­ble­s(size = "­SAL­ARI­O",s­cal­e=1­,style = "­qua­nti­le", col = "­SAL­ARI­O")
st_wri­te(­obj­=CC­AA_­MAP,dsn = "­car­tog­raf­ías­/CC­AA_­SAL­ARI­OS.s­hp­", append­=FALSE)
Plotting new info changes a bit