library(leaflet)
library(leaflet.extras)
library(knitr)
library(pander)
Primer mapa
primermapa<- leaflet() %>% #uso de la libreria
addTiles() %>% # API para el mapa, usa por defecto: OpenStreetMap
addMarkers(lng=-5.664112384, lat=40.96500844,# Poner como parametros las coordenadas del lugar a graficar
popup ="Plaza Mayor de Salamanca" )
primermapa
Las funciones en R para la creación de mapas tienen parametros que reciben datos espaciales. En el caso de Leaflet si se hace uso de un marco de datos este debe tener columnas con la longitud(lng) y latitud(lat) del lugar a mostrar.
Ejemplo de salarios de New Jersey
Base de salarios New Jersey (continued below)
| 7675 |
STANDARD |
WESTWOOD |
NJ |
PRIMARY |
41 |
-74 |
| 7677 |
STANDARD |
WOODCLIFF LAKE |
NJ |
PRIMARY |
41 |
-74 |
| 7885 |
STANDARD |
WHARTON |
NJ |
PRIMARY |
41 |
-75 |
| 7981 |
STANDARD |
WHIPPANY |
NJ |
PRIMARY |
41 |
-74 |
| 8889 |
STANDARD |
WHITEHOUSE STATION |
NJ |
PRIMARY |
41 |
-75 |
| 7095 |
STANDARD |
WOODBRIDGE |
NJ |
PRIMARY |
41 |
-74 |
Table continues below
| NA-US-NJ-WESTWOOD |
FALSE |
13245 |
| NA-US-NJ-WOODCLIFF LAKE |
FALSE |
2945 |
| NA-US-NJ-WHARTON |
FALSE |
5273 |
| NA-US-NJ-WHIPPANY |
FALSE |
4585 |
| NA-US-NJ-WHITEHOUSE STATION |
FALSE |
4691 |
| NA-US-NJ-WOODBRIDGE |
FALSE |
10018 |
| 24083 |
1.1e+09 |
82227 |
| 5471 |
3.3e+08 |
110505 |
| 8999 |
2.4e+08 |
45672 |
| 8057 |
2.9e+08 |
63707 |
| 8570 |
4e+08 |
85549 |
| 17272 |
5.3e+08 |
52737 |
# Discretización de la variable Avgwg
wages$wgClass <- cut(wages$Avgwg,
quantile(wages$Avgwg,
probs = seq(0,1, 0.2)),
labels = F, include.lowest = T)
kable(table(wages$wgClass),digits = 2,align = "c")
| 1 |
118 |
| 2 |
117 |
| 3 |
117 |
| 4 |
117 |
| 5 |
117 |
Con etiquetas:
pale <- rainbow(5) # Paleta de colores
wages %>%
leaflet() %>%
addTiles() %>%
addCircles(weight = 1 # grosor de circulos
,radius = sqrt(wages$wgClass) * 1000, # radio de los circulos proporcional a la clase perteneciente
color = pale[wages$wgClass], # color diferenciador de clases
popup = as.character(wages$wgClass))%>%
addLegend(position = "bottomleft" ,colors = pale,
labels = paste("<=", formatC(round(tapply(wages$Avgwg,
wages$wgClass, max),0),big.mark = ".",format = "d")),title = "Etiqueta")
Mapa de calor:
wages %>%
leaflet() %>%
addTiles() %>%addHeatmap(
lng = ~Long, lat = ~Lat, intensity = ~Avgwg,
blur = 20, # Difuminar. cantidad de desenfoque para aplicar
max = 0.05, # Intensidad máxima del punto
radius = 10)