1. Bloque de inicializacion de librerias

ejecutar el siguiente código si no se dispone del paquete ggmap: devtools::install_github(“dkahle/ggmap”)

ejecutar el siguiente código si al ejecutar los comandos aparecen errores relacionados con el environment: devtools::install_github("hadley/ggplot2@v2.2.0")

library(ggmap)
## Loading required package: ggplot2
## Google Maps API Terms of Service: http://developers.google.com/maps/terms.
## Please cite ggmap if you use it: see citation("ggmap") for details.

2. Bloque de parametros iniciales

setwd("~/Desktop/Taller")

3. Bloque de carga de informacion

Viviendas <- read.csv("listings.csv")

7. Bloque de distancias

desde=c("El Arte de Medir, Calle Cundinamarca, Madrid")
hasta=c("Medialab-Prado, Calle de la Alameda, Madrid")
mapdist(desde, hasta, mode = "driving")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=El%20Arte%20de%20Medir%2C%20Calle%20Cundinamarca%2C%20Madrid&destinations=Medialab-Prado%2C%20Calle%20de%20la%20Alameda%2C%20Madrid&mode=driving&language=en-EN
##                                           from
## 1 El Arte de Medir, Calle Cundinamarca, Madrid
##                                            to     m     km    miles
## 1 Medialab-Prado, Calle de la Alameda, Madrid 17658 17.658 10.97268
##   seconds minutes     hours
## 1    1227   20.45 0.3408333
mapdist(desde, hasta, mode = "bicycling")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=El%20Arte%20de%20Medir%2C%20Calle%20Cundinamarca%2C%20Madrid&destinations=Medialab-Prado%2C%20Calle%20de%20la%20Alameda%2C%20Madrid&mode=bicycling&language=en-EN
##                                           from
## 1 El Arte de Medir, Calle Cundinamarca, Madrid
##                                            to     m     km    miles
## 1 Medialab-Prado, Calle de la Alameda, Madrid 11742 11.742 7.296479
##   seconds minutes hours
## 1    2610    43.5 0.725
mapdist(desde, hasta, mode = "walking")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=El%20Arte%20de%20Medir%2C%20Calle%20Cundinamarca%2C%20Madrid&destinations=Medialab-Prado%2C%20Calle%20de%20la%20Alameda%2C%20Madrid&mode=walking&language=en-EN
##                                           from
## 1 El Arte de Medir, Calle Cundinamarca, Madrid
##                                            to     m     km    miles
## 1 Medialab-Prado, Calle de la Alameda, Madrid 10194 10.194 6.334552
##   seconds  minutes    hours
## 1    7706 128.4333 2.140556

EJERCICIO: Calcular la distancia entre Atocha y Chamartin en bicicleta, caminando y en coche

desde=c("Estación de Madrid Atocha, Plaza Emperador Carlos V, Madrid")
hasta=c("Chamartín, Madrid")
mapdist(desde, hasta, mode = "driving")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=Estaci%C3%B3n%20de%20Madrid%20Atocha%2C%20Plaza%20Emperador%20Carlos%20V%2C%20Madrid&destinations=Chamart%C3%ADn%2C%20Madrid&mode=driving&language=en-EN
##                                                          from
## 1 Estación de Madrid Atocha, Plaza Emperador Carlos V, Madrid
##                  to     m     km    miles seconds  minutes     hours
## 1 Chamartín, Madrid 11356 11.356 7.056618    1073 17.88333 0.2980556
mapdist(desde, hasta, mode = "bicycling")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=Estaci%C3%B3n%20de%20Madrid%20Atocha%2C%20Plaza%20Emperador%20Carlos%20V%2C%20Madrid&destinations=Chamart%C3%ADn%2C%20Madrid&mode=bicycling&language=en-EN
##                                                          from
## 1 Estación de Madrid Atocha, Plaza Emperador Carlos V, Madrid
##                  to    m    km    miles seconds minutes     hours
## 1 Chamartín, Madrid 6888 6.888 4.280203    1590    26.5 0.4416667
mapdist(desde, hasta, mode = "walking")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=Estaci%C3%B3n%20de%20Madrid%20Atocha%2C%20Plaza%20Emperador%20Carlos%20V%2C%20Madrid&destinations=Chamart%C3%ADn%2C%20Madrid&mode=walking&language=en-EN
##                                                          from
## 1 Estación de Madrid Atocha, Plaza Emperador Carlos V, Madrid
##                  to    m    km    miles seconds  minutes    hours
## 1 Chamartín, Madrid 6362 6.362 3.953347    5081 84.68333 1.411389
  1. Bloque de posicionamiento de objetos
ArtedeMedir <- geocode("El Arte de Medir, Calle Cundinamarca, Madrid",source = "google")
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=El%20Arte%20de%20Medir%2C%20Calle%20Cundinamarca%2C%20Madrid
map.ArtedeMedir <- get_map(location = as.numeric(ArtedeMedir),zoom = 18,maptype = "roadmap")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.470838,-3.631555&zoom=18&size=640x640&scale=2&maptype=roadmap&language=en-EN
str(ArtedeMedir)
## 'data.frame':    1 obs. of  2 variables:
##  $ lon: num -3.63
##  $ lat: num 40.5
ggmap(map.ArtedeMedir) + geom_point(aes(x = lon, y = lat),
                                    data = ArtedeMedir, colour = 'red',
                                    size = 4)

str(Viviendas)
## 'data.frame':    7446 obs. of  16 variables:
##  $ id                            : int  7328003 6289024 3778195 7056183 6847125 3453225 336869 5876946 316712 6584180 ...
##  $ name                          : Factor w/ 7251 levels "  Habitacion silenciosa ",..: 1779 5377 6958 5412 4151 4291 2858 5912 50 3977 ...
##  $ host_id                       : int  9205789 3305388 10940437 19558259 21039137 1528801 1713524 16859206 162701 34212673 ...
##  $ host_name                     : Factor w/ 1821 levels "","(email hidden)",..: 64 504 64 1001 349 500 1378 1718 1304 1208 ...
##  $ neighbourhood_group           : Factor w/ 21 levels "Arganzuela","Barajas",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ neighbourhood                 : Factor w/ 126 levels "Abrantes","Acacias",..: 83 83 83 83 83 83 83 83 83 83 ...
##  $ latitude                      : num  40.4 40.4 40.4 40.4 40.4 ...
##  $ longitude                     : num  -3.71 -3.72 -3.71 -3.71 -3.71 ...
##  $ room_type                     : Factor w/ 3 levels "Entire home/apt",..: 1 2 3 2 2 2 2 2 2 2 ...
##  $ price                         : int  60 25 22 45 37 19 55 95 25 25 ...
##  $ minimum_nights                : int  1 1 1 1 2 3 2 1 2 1 ...
##  $ number_of_reviews             : int  3 1 12 0 3 0 75 2 60 3 ...
##  $ last_review                   : Factor w/ 420 levels "","2011-11-26",..: 409 350 401 1 338 1 415 332 406 373 ...
##  $ reviews_per_month             : num  3 0.42 0.91 NA 1 NA 1.81 0.44 1.38 0.77 ...
##  $ calculated_host_listings_count: int  2 1 11 1 1 12 2 6 1 5 ...
##  $ availability_365              : int  343 330 347 335 0 26 318 302 327 295 ...
Madrid=c(-3.6883432,40.453054)
map.Madrid <- get_map(location = Madrid)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN
ggmap(map.Madrid)+ geom_point(aes(x = longitude, y = latitude),
                                       data = Viviendas, colour = 'red', alpha=1)

9. Bloque de consulta de consumo de API

geocodeQueryCheck()
## 2499 geocoding queries remaining.

10. Bloque de representacion de variables categoricas

Viviendas <- read.csv("listings.csv")
str(Viviendas)
## 'data.frame':    7446 obs. of  16 variables:
##  $ id                            : int  7328003 6289024 3778195 7056183 6847125 3453225 336869 5876946 316712 6584180 ...
##  $ name                          : Factor w/ 7251 levels "  Habitacion silenciosa ",..: 1779 5377 6958 5412 4151 4291 2858 5912 50 3977 ...
##  $ host_id                       : int  9205789 3305388 10940437 19558259 21039137 1528801 1713524 16859206 162701 34212673 ...
##  $ host_name                     : Factor w/ 1821 levels "","(email hidden)",..: 64 504 64 1001 349 500 1378 1718 1304 1208 ...
##  $ neighbourhood_group           : Factor w/ 21 levels "Arganzuela","Barajas",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ neighbourhood                 : Factor w/ 126 levels "Abrantes","Acacias",..: 83 83 83 83 83 83 83 83 83 83 ...
##  $ latitude                      : num  40.4 40.4 40.4 40.4 40.4 ...
##  $ longitude                     : num  -3.71 -3.72 -3.71 -3.71 -3.71 ...
##  $ room_type                     : Factor w/ 3 levels "Entire home/apt",..: 1 2 3 2 2 2 2 2 2 2 ...
##  $ price                         : int  60 25 22 45 37 19 55 95 25 25 ...
##  $ minimum_nights                : int  1 1 1 1 2 3 2 1 2 1 ...
##  $ number_of_reviews             : int  3 1 12 0 3 0 75 2 60 3 ...
##  $ last_review                   : Factor w/ 420 levels "","2011-11-26",..: 409 350 401 1 338 1 415 332 406 373 ...
##  $ reviews_per_month             : num  3 0.42 0.91 NA 1 NA 1.81 0.44 1.38 0.77 ...
##  $ calculated_host_listings_count: int  2 1 11 1 1 12 2 6 1 5 ...
##  $ availability_365              : int  343 330 347 335 0 26 318 302 327 295 ...
table(Viviendas$room_type)
## 
## Entire home/apt    Private room     Shared room 
##            4580            2760             106
Madrid=c(-3.6883432,40.453054)
map.Madrid <- get_map(location = Madrid,zoom=12,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=12&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Madrid)+ geom_point(aes(x = longitude, y = latitude),
                              data = Viviendas, colour = 4+as.numeric(Viviendas$room_type))
## Warning: Removed 74 rows containing missing values (geom_point).

Bernabeu=c(-3.7083759,40.4169335)
map.Bernabeu <- get_map(location = Bernabeu,zoom = 16,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.416933,-3.708376&zoom=16&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Bernabeu) + geom_point(aes(x = longitude, y = latitude),
                                 data = Viviendas, colour =4+as.numeric(Viviendas$room_type))
## Warning: Removed 6149 rows containing missing values (geom_point).

EJERCICIO: Representar en un mapa tipo satellite las viviendas cercanas a Sol con Zoom 18

Sol=c(-3.6883432,40.453054)
map.Sol <- get_map(location = Sol,zoom = 18,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=18&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Sol) + geom_point(aes(x = longitude, y = latitude),
                                 data = Viviendas, colour = 4+as.numeric(Viviendas$room_type))
## Warning: Removed 7445 rows containing missing values (geom_point).

11. Bloque de representacion de variables continuas

str(Viviendas)
## 'data.frame':    7446 obs. of  16 variables:
##  $ id                            : int  7328003 6289024 3778195 7056183 6847125 3453225 336869 5876946 316712 6584180 ...
##  $ name                          : Factor w/ 7251 levels "  Habitacion silenciosa ",..: 1779 5377 6958 5412 4151 4291 2858 5912 50 3977 ...
##  $ host_id                       : int  9205789 3305388 10940437 19558259 21039137 1528801 1713524 16859206 162701 34212673 ...
##  $ host_name                     : Factor w/ 1821 levels "","(email hidden)",..: 64 504 64 1001 349 500 1378 1718 1304 1208 ...
##  $ neighbourhood_group           : Factor w/ 21 levels "Arganzuela","Barajas",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ neighbourhood                 : Factor w/ 126 levels "Abrantes","Acacias",..: 83 83 83 83 83 83 83 83 83 83 ...
##  $ latitude                      : num  40.4 40.4 40.4 40.4 40.4 ...
##  $ longitude                     : num  -3.71 -3.72 -3.71 -3.71 -3.71 ...
##  $ room_type                     : Factor w/ 3 levels "Entire home/apt",..: 1 2 3 2 2 2 2 2 2 2 ...
##  $ price                         : int  60 25 22 45 37 19 55 95 25 25 ...
##  $ minimum_nights                : int  1 1 1 1 2 3 2 1 2 1 ...
##  $ number_of_reviews             : int  3 1 12 0 3 0 75 2 60 3 ...
##  $ last_review                   : Factor w/ 420 levels "","2011-11-26",..: 409 350 401 1 338 1 415 332 406 373 ...
##  $ reviews_per_month             : num  3 0.42 0.91 NA 1 NA 1.81 0.44 1.38 0.77 ...
##  $ calculated_host_listings_count: int  2 1 11 1 1 12 2 6 1 5 ...
##  $ availability_365              : int  343 330 347 335 0 26 318 302 327 295 ...
hist(Viviendas$price)

hist(log(Viviendas$price))

Madrid=c(-3.6883432,40.453054)
map.Madrid <- get_map(location = Madrid,zoom=12,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=12&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Madrid)+ geom_point(aes(x = longitude, y = latitude,col = room_type,size=price) ,
                              data = Viviendas)
## Warning: Removed 74 rows containing missing values (geom_point).

Bernabeu=c(-3.6883432,40.453054)
map.Bernabeu <- get_map(location = Bernabeu,zoom = 16,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=16&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Bernabeu) + geom_point(aes(x = longitude, y = latitude,col = room_type,size=price) ,data = Viviendas)
## Warning: Removed 7409 rows containing missing values (geom_point).

ggmap(map.Bernabeu) + geom_point(aes(x = longitude, y = latitude,col = room_type,size=price) ,data = Viviendas,shape=8)
## Warning: Removed 7409 rows containing missing values (geom_point).