Mapa estaciones del MIO

Datos de 2015

Usando el gazetero de Google

library(ggplot2)
library(ggmap)

# registro en google
register_google(apiKey)

qmap('centro comercial gran estacion', zoom = 17, source ='google')

Usando coordenadas

# Vista rápida / Google Maps provider
caliCoordenadas <- c(-76.5328476, 3.4320292)
qmap(caliCoordenadas, zoom = 10)

Importando datos y agrupándolos usando K-Means

# Importar el dataset
MIO_STOPS <- read.delim("MIO_STOPS.txt")

#Clustering
coords <- MIO_STOPS[,c(7,8)]
colnames(coords) <- c("lon","lat")

# Kmeans
grupos <- kmeans(coords, 5, iter.max = 100, nstart = 1)
MIO_STOPS$cluster <- grupos$cluster

#scatter plot
plot <- ggplot(MIO_STOPS, aes(DECIMALLONGITUDE,DECIMALLATITUDE))
plot <- plot + geom_point(alpha=0.3, color=MIO_STOPS$cluster)
plot

Dubujo de siluetas al rededor de los clusters

# Convex hulls
cluster1 <- subset(MIO_STOPS, MIO_STOPS$cluster==1)
cluster2 <- subset(MIO_STOPS, MIO_STOPS$cluster==2)
cluster3 <- subset(MIO_STOPS, MIO_STOPS$cluster==3)
cluster4 <- subset(MIO_STOPS, MIO_STOPS$cluster==4)
cluster5 <- subset(MIO_STOPS, MIO_STOPS$cluster==5)

# Obtener indices. Funciona solo con datos x,y
indexes1 <- chull(cluster1[,c(7,8)])
indexes2 <- chull(cluster2[,c(7,8)])
indexes3 <- chull(cluster3[,c(7,8)])
indexes4 <- chull(cluster4[,c(7,8)])
indexes5 <- chull(cluster5[,c(7,8)])

# Obtener coordenadas
hullCluster1 <- cluster1[indexes1,]
hullCluster2 <- cluster2[indexes2,]
hullCluster3 <- cluster3[indexes3,]
hullCluster4 <- cluster4[indexes4,]
hullCluster5 <- cluster5[indexes5,]

#scatter plot
plot <- ggplot(MIO_STOPS, aes(DECIMALLONGITUDE,DECIMALLATITUDE))
plot <- plot + geom_point(alpha=0.3, color=MIO_STOPS$cluster)
plot <- plot + geom_polygon(data=hullCluster1, alpha=.2, fill='red')
plot <- plot + geom_polygon(data=hullCluster2, alpha=.2, fill='blue')
plot <- plot + geom_polygon(data=hullCluster3, alpha=.2, fill='darkorange4')
plot <- plot + geom_polygon(data=hullCluster4, alpha=.2)
plot <- plot + geom_polygon(data=hullCluster5, alpha=.2)
plot

Gráfica con Mapa y clustering

## Mapa
city <- get_map(caliCoordenadas, zoom = 12, maptype = "terrain")

caliMap <- ggmap(city)

caliMap <- caliMap + geom_point(
  data = MIO_STOPS, 
  aes(x= DECIMALLONGITUDE, y = DECIMALLATITUDE), 
  color = MIO_STOPS$TYPE_BUS, 
  size= MIO_STOPS$PASSANGERS_NUM * 0.01)

caliMap <- caliMap + geom_polygon(
  data = hullCluster1, 
  aes(DECIMALLONGITUDE,DECIMALLATITUDE),
  color="darkorange",
  fill="darkorange",
  alpha=0.2)
caliMap <- caliMap + geom_polygon(data = hullCluster2, aes(DECIMALLONGITUDE,DECIMALLATITUDE), color="gold", fill="gold", alpha=0.2)
caliMap <- caliMap + geom_polygon(data = hullCluster3, aes(DECIMALLONGITUDE,DECIMALLATITUDE), color="darkorchid", fill="darkorchid", alpha=0.2)
caliMap <- caliMap + geom_polygon(data = hullCluster4, aes(DECIMALLONGITUDE,DECIMALLATITUDE), color="darkcyan", fill="darkcyan", alpha=0.2)
caliMap <- caliMap + geom_polygon(data = hullCluster5, aes(DECIMALLONGITUDE,DECIMALLATITUDE), color="deeppink", fill="deeppink", alpha=0.2)

caliMap

city <- get_map(caliCoordenadas, zoom = 12, maptype = "hybrid") # terrain, satellite, roadmap, hybrid
map <- ggmap(city)
map <- map  + stat_bin2d(data=MIO_STOPS, aes(x=DECIMALLONGITUDE, y=DECIMALLATITUDE),bins = 50)
map <- map  + geom_density2d(data=MIO_STOPS, aes(x=DECIMALLONGITUDE, y=DECIMALLATITUDE,bins= 50))
map <- map  + scale_fill_viridis_c()
map