library(ggplot2)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
register_google(clave)
caliCoords <- c(-76.5,3.43)
# qmap(caliCoords,zoom=14)
# Estaciones MIO
estacionesMIO <- read.delim("~/Desktop/mapaGoogle/MIO_STOPS.txt")
# K-means
coords <- data.frame(estacionesMIO$DECIMALLONGITUDE)
coords <- cbind(coords, estacionesMIO$DECIMALLATITUDE)
colnames(coords) <- c("lon", "lat")
grupos <- kmeans(coords,7, iter.max = 100, nstart = 1)
coords$cluster <-grupos$cluster
# Mapa scatter plot / dispersion
mapaCoords <- ggplot(coords, aes(lon,lat, color=as.factor(cluster)))
mapaCoords <- mapaCoords + geom_point()
mapaCoords

mapaZonas <- ggplot(estacionesMIO,aes(DECIMALLONGITUDE, DECIMALLATITUDE))
mapaZonas <- mapaZonas + geom_point()
mapaZonas

# Convex Hull / perimetro
cluster1 <- subset(coords,coords$cluster==2)
indices <- chull(cluster1)
pC1 <- cluster1[indices,]
# funcion 2
# obtengo el mapa de google
caliMapa <- get_map(caliCoords, zoom = 12)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=3.43,-76.5&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
# creo el objeto ggmap
mapa <- ggmap(caliMapa)
mapa <- mapa + geom_polygon(data=pC1, alpha=0.3, fill="#FF0000")
mapa <- mapa + geom_point(data=coords, aes(lon, lat), size=estacionesMIO$PASSANGERS_NUM * 0.03, color=coords$cluster)
# pinto el mapa
mapa
