Commit 48f3f033 authored by numeroteca's avatar numeroteca

analiza por barrios, ratios, barras, coropletas

parent 0310e883
......@@ -5,6 +5,14 @@
# -mapas de coropletas de ratio
# -exporta a geojson
# En qué momento estoy?
# he hecho points en polygons de airbnb valencia para pillar el código de distrito y poder hacer merge de número de airbnb por distrito para hacer los gricos mariposa.
# algunos códigos de distrito no los ha pillado, tngo que revisar.
# lugeo maripos ¿quizás en datawrapper?
# luego plazas por habitante?
# luego coropletas
# luego cartograma
# ---- Load libraries -----
library(tidyverse)
# read shapes
......@@ -19,8 +27,10 @@ library(gsubfn) # select text in the parenthesis with regex
barrios <- readOGR("data/original/shapes/barrios.valencia.wgs84.geojson")
distritos <- readOGR("data/original/shapes/distritos.valencia.wgs84.geojson")
# mar <- readOGR("data/original/shapes/mar-donostia.geojson") TODO
municipios <- readOGR("data/original/shapes/municipios.provincia.valencia.geojson")
viviendas_barrios <- read.delim("data/original/numero-viviendas-por-barrio-valencia-censo2011-ine.csv",sep = ",")
# viviendas_barrios <- read.delim("data/original/numero-viviendas-por-barrio-valencia-censo2011-ine.csv",sep = ",")
viviendas_barrios <- read.delim("data/output/numero-viviendas-por-barrio-valencia-censo2011-ine.csv",sep = ",")
viviendas_distritos <- read.delim("data/original/numero-viviendas-por-distrito-valencia-censo2011-ine.csv",sep = ",")
# # Airbnb listings 2017-09-12
......@@ -29,7 +39,7 @@ viviendas_distritos <- read.delim("data/original/numero-viviendas-por-distrito-v
# levels(airbnb201709$room_type) <- c("Vivienda completa","Habitación privada","Habitación compartida")
# Airbnb listings 2019-02-27
airbnb201902 <- read.delim("data/original/airbnb/190227/listings_valencia_insideairbnb.csv",sep = ",")
airbnb201902 <- read.delim("data/output/airbnb/190227/listings_valencia_insideairbnb_barrio-distrito.csv",sep = ",")
# Translate room type
levels(airbnb201902$room_type) <- c("Vivienda completa","Habitación privada","Habitación compartida")
......@@ -44,7 +54,7 @@ write.csv(as.data.frame(barrios_n_listings), file = "data/output/airbnb/190227/d
# ----- Comparative room types -----------
# Compare two data sets by number of room types----
# /Compare two data sets by number of room types----
# compare_room <- merge(data.frame(table(airbnb190227$room_type)),data.frame(table(airbnb201902$room_type)),by="Var1")
# colnames(compare_room) <- c("tipo_habitacion","2017","2018")
......@@ -86,19 +96,20 @@ write.csv(as.data.frame(barrios_n_listings), file = "data/output/airbnb/190227/d
# mutate(suma=sum(count)) %>%
# ungroup() %>%
# arrange(-count)
airbnb.barrio.room_type.2.cleansed <- airbnb201902 %>%
airbnb.barrio.room_type.2 <- airbnb201902 %>%
group_by(neighbourhood_cleansed,room_type) %>%
summarise(count=n()) %>%
mutate(suma=sum(count)) %>%
ungroup() %>%
arrange(-count)
airbnb.barrio.room_type.2 <- airbnb201902 %>%
group_by(neighbourhood,room_type) %>%
summarise(count=n()) %>%
mutate(suma=sum(count)) %>%
ungroup() %>%
arrange(-count)
# no se usa porque la variable neighbourhood es más incompleta que neighbourhood_cleansed
# airbnb.barrio.room_type.2 <- airbnb201902 %>%
# group_by(neighbourhood,room_type) %>%
# summarise(count=n()) %>%
# mutate(suma=sum(count)) %>%
# ungroup() %>%
# arrange(-count)
# Clasificación por distritos ---------------------------------------------------------
# airbnb.distrito.room_type.1 <- airbnb201709 %>%
......@@ -116,7 +127,7 @@ airbnb.distrito.room_type.2 <- airbnb201902 %>%
arrange(-count)
png(filename="images/airbnb/hab-viv-barras-airbnb-distritos-valencia-201902a.png",width = 700,height = 500)
ggplot(data = airbnb.barrio.room_type.2,aes(x = reorder(neighbourhood,suma), y = count, fill=room_type)) +
ggplot(data = airbnb.barrio.room_type.2,aes(x = reorder(neighbourhood_cleansed,suma), y = count, fill=room_type)) +
# "reverse" es la clave para reordenar las barras y que coincida con leyenda https://github.com/tidyverse/ggplot2/issues/1837
geom_col(position = position_stack(reverse = TRUE)) +
# scale_y_continuous(expand = c(0, 30)) + #limits = c(0,950),
......@@ -172,7 +183,7 @@ ggplot(data = airbnb.distrito.room_type.2,aes(x = reorder(neighbourhood_group_cl
dev.off()
png(filename="images/airbnb/hab-viv-barras-airbnb-barrio-valencia-201902.png",width = 800,height = 1300)
ggplot(data = airbnb.barrio.room_type.2.cleansed,aes(x = reorder(neighbourhood_cleansed,suma), y = count, fill=room_type)) +
ggplot(data = airbnb.barrio.room_type.2,aes(x = reorder(neighbourhood_cleansed,suma), y = count, fill=room_type)) +
# "reverse" es la clave para reordenar las barras y que coincida con leyenda https://github.com/tidyverse/ggplot2/issues/1837
geom_col(position = position_stack(reverse = TRUE)) +
# scale_y_continuous(expand = c(0, 30)) + #limits = c(0,950),
......@@ -190,7 +201,7 @@ ggplot(data = airbnb.barrio.room_type.2.cleansed,aes(x = reorder(neighbourhood_c
fill="tipo de alojamiento") +
# partial text
geom_text(aes(label = count,group=room_type),
data=airbnb.barrio.room_type.2.cleansed[airbnb.barrio.room_type.2.cleansed$count > 50,],
data=airbnb.barrio.room_type.2[airbnb.barrio.room_type.2$count > 50,],
position = position_stack(reverse = TRUE,vjust = 0.5),size=3,color="#FFFFFF") +
# total text
geom_text(aes(label = suma, y = suma+10),
......@@ -200,7 +211,7 @@ ggplot(data = airbnb.barrio.room_type.2.cleansed,aes(x = reorder(neighbourhood_c
dev.off()
png(filename="images/airbnb/hab-viv-barras-airbnb-distritos-valencia-201902-facet.png",width = 1100,height = 500)
ggplot(data = airbnb.barrio.room_type.2,aes(x = reorder(neighbourhood,suma), y = count, fill=room_type)) +
ggplot(data = airbnb.barrio.room_type.2,aes(x = reorder(neighbourhood_cleansed,suma), y = count, fill=room_type)) +
# "reverse" es la clave para reordenar las barras y que coincida con leyenda https://github.com/tidyverse/ggplot2/issues/1837
geom_col(position = position_stack(reverse = TRUE)) +
scale_y_continuous(labels=function(x) format(x, big.mark = ".", scientific = FALSE),expand = c(0, 30),limits = c(0,1500)) +
......@@ -551,35 +562,63 @@ ggplot() +
dev.off()
# ----- Prepara datos por distritos ------------------------------------------------------------------------------------
#
airbnb.distritos <- airbnb201902 %>%
group_by(neighbourhood_group_cleansed) %>%
# ----- Prepara datos por barrios y distritos ------------------------------------------------------------------------------------
# agrupa datos añadiendo código de barrio
airbnb.barrios <- airbnb201902 %>%
group_by(neighbourhood_cleansed,coddistbar) %>%
summarise(count=n()) %>%
ungroup() %>%
arrange(-count)
por_distritos <- merge(airbnb.distritos,viviendas_distritos, by.x="neighbourhood",by.y="Nom_Barri")
# adds room_type
airbnb.barrios.room_type <- airbnb201902 %>%
group_by(neighbourhood_cleansed,coddistbar,room_type) %>%
summarise(count=n()) %>% #suma el número de items por barrio
mutate(suma=sum(count)) %>%
ungroup() %>%
arrange(-count)
# agrupa datos añadiendo código de distrito
airbnb.distritos <- airbnb201902 %>%
group_by(neighbourhood_group_cleansed,coddistrit) %>%
summarise(count=n()) %>%
ungroup() %>%
arrange(-count)
# nota: aquí se pierden 235 de ser contados porque caen fuera de los contornos de los barrios (ver airbnb.distritos). Insideairbnb si los detecta como que están
# barrios. Si se le quita el coddistrit del gropup_by pilla todo os anuncios
# sum(por_barrios[is.na(por_barrios$coddistbar),]$count)
# Añade datos de vivienda
por_barrios <- merge(airbnb_20172018_barr,viviendas_barrios, by.x="neighbourhood",by.y="Nom_Barri")
por_barrios <- full_join(airbnb.barrios,viviendas_barrios, by="coddistbar")
# removes rows without código, it means that points are outside area
por_barrios <- por_barrios %>% filter( !is.na(coddistbar) )
# igual para room_type
por_barrios.room_type <- left_join(airbnb.barrios.room_type,viviendas_barrios, by="coddistbar")
por_barrios.room_type <- por_barrios.room_type %>% filter( !is.na(coddistbar) )
# une datos de airbnb con datos de distritos
por_distritos <- full_join(airbnb.distritos,viviendas_distritos, by="coddistrit")
# removes rows without código, it means that points are outside area
por_distritos <- por_distritos %>% filter( !is.na(coddistrit) )
# Ratio Airbnb por barrios: barras ------------------------------------------------------------------------------------
por_barrios$ratio2017_room_type <- round(por_barrios$anuncios2017 / por_barrios$Domicilis *100,digits = 2)
por_barrios$ratio2018_room_type <- round(por_barrios$anuncios2018 / por_barrios$Domicilis *100,digits = 2)
por_barrios$ratio2017_listings <- round(por_barrios$total2017 / por_barrios$Domicilis *100,digits = 2)
por_barrios$ratio2018_listings <- round(por_barrios$total2018 / por_barrios$Domicilis *100,digits = 2)
por_barrios$ratio2018_listings <- round(por_barrios$count / por_barrios$Total *100,digits = 2)
por_barrios.room_type$ratio2018_room_type <- round(por_barrios.room_type$count / por_barrios.room_type$Total *100,digits = 2)
# provides position for reordering
# provides position for reordering in mariposa bars graph
por_barrios$pos_airbnb_2018 <- 1
por_barrios[ order(-por_barrios[,6]), ]$pos_airbnb_2018 <- 1:17
# ordena por la columna del count airbnb
por_barrios[ order(-por_barrios[,"count"]), ]$pos_airbnb_2018 <- 1:nrow(por_barrios)
por_barrios$pos_ratio2018 <- 1
por_barrios[ order(-por_barrios[,"ratio2018"]), ]$pos_ratio2018 <- 1:17
# ordena por la columna del ratio
por_barrios[ order(-por_barrios[,"ratio2018_listings"]), ]$pos_ratio2018 <- 1:nrow(por_barrios)
# Ratio anuncios por viviendas en barrios. Barras
png(filename="images/airbnb/ratio-airbnb-barrios-valencia-201809.png",width = 900,height = 1400)
ggplot(por_barrios,aes(x = reorder(neighbourhood, ratio2018_listings), y = ratio2018_listings)) + #order by Value or by -pos_ratio2018
# Ratio anuncios por viviendas en barrios. Barras ------
png(filename="images/airbnb/ratio-listings-airbnb-anuncios-barrios-valencia-201902.png",width = 900,height = 1400)
por_barrios %>% filter(!is.na(ratio2018_listings)) %>%
ggplot(aes(x = reorder(neighbourhood_cleansed, ratio2018_listings), y = ratio2018_listings)) + #order by Value or by -pos_ratio2018
geom_col(position = "dodge") +
# scale_y_continuous(limits = c(0,8.5), expand = c(0,0)) +
theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
......@@ -598,10 +637,32 @@ ggplot(por_barrios,aes(x = reorder(neighbourhood, ratio2018_listings), y = ratio
coord_flip()
dev.off()
# Ratio anuncios pisos completos por viviendas en barrios. Barras
png(filename="images/airbnb/ratio-airbnb-pisos-completos-barrios-valencia-201809.png",width = 900,height = 1400)
por_barrios %>% filter( room_type == "Vivienda completa") %>%
ggplot(aes(x = reorder(neighbourhood, ratio2018_room_type), y = ratio2018_room_type)) + #order by Value or by -pos_ratio2018
# Ratio anuncios por viviendas en barrios. Barras. Top 15
png(filename="images/airbnb/ratio-listings-airbnb-anuncios-barrios-valencia-201902_top15.png",width = 600,height = 600)
por_barrios %>% filter(!is.na(ratio2018_listings)) %>% head(15) %>%
ggplot(aes(x = reorder(neighbourhood_cleansed, ratio2018_listings), y = ratio2018_listings)) + #order by Value or by -pos_ratio2018
geom_col(position = "dodge") +
# scale_y_continuous(limits = c(0,8.5), expand = c(0,0)) +
theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
theme(
panel.grid.minor.y = element_blank(), panel.grid.major.y = element_blank(),
legend.position = "bottom"
) +
labs(title = "Presencia de Airbnb en barrios. Top15. Febrero 2019. Valencia",
subtitle = "Ratio de anuncios de Airbnb por cada 100 viviendas",
y = "ratio anuncios Airbnb / 100 viviendas",
x = NULL,
caption = "Datos: InsideAirbnb. Gráfico: lab.montera34.com/airbnb") +
geom_text(aes(label = ratio2018_listings),
position = position_dodge(width = 1), hjust = -0.1,
size=3,color="#777777") +
coord_flip()
dev.off()
# Ratio anuncios pisos completos por viviendas completas en barrios. Barras
png(filename="images/airbnb/ratio-airbnb-viviendas-completas-barrios-valencia-201902.png",width = 900,height = 1400)
por_barrios.room_type %>% filter( room_type == "Vivienda completa") %>% filter(!is.na(ratio2018_listings)) %>%
ggplot(aes(x = reorder(neighbourhood_cleansed, ratio2018_room_type), y = ratio2018_room_type)) + #order by Value or by -pos_ratio2018
geom_col(position = "dodge") +
# scale_y_continuous(limits = c(0,8.5), expand = c(0,0)) +
theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
......@@ -620,28 +681,53 @@ ggplot(aes(x = reorder(neighbourhood, ratio2018_room_type), y = ratio2018_room_t
coord_flip()
dev.off()
# Ratio anuncios pisos completos por viviendas completas en barrios. Barras. Top15
png(filename="images/airbnb/ratio-airbnb-viviendas-completas-barrios-valencia-201902_top15.png",width = 600,height = 600)
por_barrios.room_type %>% filter( room_type == "Vivienda completa") %>% filter(!is.na(ratio2018_listings)) %>% head(15) %>%
ggplot(aes(x = reorder(neighbourhood_cleansed, ratio2018_room_type), y = ratio2018_room_type)) + #order by Value or by -pos_ratio2018
geom_col(position = "dodge") +
# scale_y_continuous(limits = c(0,8.5), expand = c(0,0)) +
theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
theme(
panel.grid.minor.y = element_blank(), panel.grid.major.y = element_blank(),
legend.position = "bottom"
) +
labs(title = "Presencia de Airbnb en barrios. Top15. Febrero 2019. Valencia",
subtitle = "Ratio de anuncios de viviendas completas en Airbnb por cada 100 viviendas",
y = "ratio viviendas compeltas de Airbnb / 100 viviendas",
x = NULL,
caption = "Datos: InsideAirbnb. Gráfico: lab.montera34.com/airbnb") +
geom_text(aes(label = ratio2018_room_type),
position = position_dodge(width = 1), hjust = -0.1,
size=3,color="#777777") +
coord_flip()
dev.off()
# -------- Gráfico mariposa ratio anuncios airbnb 1 year--------
# inspired/copied from https://github.com/meneos/R_Dataviz/blob/master/RENTABILIDAD%20INMUEBLES%20MADRID/rentabilidad_distritos.R
# library(gridExtra)
# provides position for reordering
# create dataframe only with viviendas
por_barrios.viviendas <- por_barrios %>% filter( room_type == "Vivienda completa")
por_barrios.viviendas <- por_barrios.room_type %>% filter( room_type == "Vivienda completa")
# ordena por numero de anuncios
por_barrios.viviendas$pos_airbnb_2018 <- 1
por_barrios.viviendas[ order(-por_barrios.viviendas[,"ratio2018_listings"]), ]$pos_airbnb_2018 <- 1:nrow(por_barrios.viviendas)
por_barrios.viviendas$pos_airbnb_viv_2018 <- 1
por_barrios.viviendas[ order(-por_barrios.viviendas[,"count"]), ]$pos_airbnb_viv_2018 <- 1:nrow(por_barrios.viviendas)
# ordena por ratio 2018
por_barrios.viviendas$pos_ratio2018 <- 1
por_barrios.viviendas[ order(-por_barrios.viviendas[,"ratio2018_listings"]), ]$pos_ratio2018 <- 1:nrow(por_barrios.viviendas)
por_barrios.viviendas$pos_ratio_viv_2018 <- 1
por_barrios.viviendas[ order(-por_barrios.viviendas[,"ratio2018_room_type"]), ]$pos_ratio_viv_2018 <- 1:nrow(por_barrios.viviendas)
# por_barrios <- merge(por_barrios, select( por_barrios.viviendas, neighbourhood,pos_airbnb_2018,pos_ratio2018), by.x="neighbourhood",by.y="neighbourhood" )
por_barrios <- full_join(por_barrios, select( por_barrios.viviendas, neighbourhood,pos_airbnb_2018,pos_ratio2018), by ="neighbourhood" )
por_barrios <- full_join(por_barrios, select( por_barrios.viviendas,coddistbar,ratio2018_room_type, pos_airbnb_viv_2018,pos_ratio_viv_2018), by ="coddistbar" )
por_barrios.room_type <- full_join(por_barrios.room_type, select( por_barrios.viviendas,coddistbar,pos_airbnb_viv_2018,pos_ratio_viv_2018), by ="coddistbar" )
por_barrios.room_type <- full_join(por_barrios.room_type, select( por_barrios, coddistbar,pos_ratio2018,pos_airbnb_2018), by ="coddistbar" )
plot1 <-
por_barrios %>% filter ( !(neighbourhood == "Can Peguera" | neighbourhood == "Ciutat Meridiana" | neighbourhood == "Montbau" | neighbourhood == "Torre Baró" )) %>%
ggplot(aes(x = reorder(neighbourhood, -pos_ratio2018), y = ratio2018_listings)) + #order by Value or by -pos_ratio2018
por_barrios %>% filter (!is.na(neighbourhood_cleansed) & !is.na(coddistbar)) %>%
ggplot(aes(x = reorder(neighbourhood_cleansed, -pos_ratio2018), y = ratio2018_listings)) + #order by Value or by -pos_ratio2018
geom_col(position = "dodge")+
scale_y_continuous(limits = c(0,21), expand = c(0,1)) +
scale_y_continuous(limits = c(0,15), expand = c(0,1)) +
theme_minimal(base_family = "Roboto Condensed", base_size = 16) +
theme(axis.title.x = element_text(margin = margin(20,0,0,0)),
plot.title = element_text(face = "bold", hjust = 0.5),
......@@ -662,41 +748,42 @@ ggplot(aes(x = reorder(neighbourhood, -pos_ratio2018), y = ratio2018_listings))
coord_flip()
# Barras numero de anuncios
plot2 <-
por_barrios %>% filter ( !(neighbourhood == "Can Peguera" | neighbourhood == "Ciutat Meridiana" | neighbourhood == "Montbau" | neighbourhood == "Torre Baró" )) %>%
ggplot(aes(x = reorder(neighbourhood, -pos_ratio2018), y = anuncios2018, fill=room_type)) + #order by Value or by -pos_ratio2018
plot2 <-
por_barrios.room_type %>% filter (!is.na(neighbourhood_cleansed) & !is.na(coddistbar)) %>%
ggplot(aes(x = reorder(neighbourhood_cleansed, -pos_ratio2018), y = count, fill=room_type)) + #order by Value or by -pos_ratio2018
geom_col(position = position_stack(reverse = TRUE)) +
scale_y_reverse(limits = c(2200,0), expand = c(0,0)) +
scale_y_reverse(limits = c(600,0), expand = c(0,50)) +
theme_minimal(base_family = "Roboto Condensed", base_size = 16) +
theme(
axis.title.x = element_text(margin = margin(20,0,0,0)),
plot.title = element_text(face = "bold", hjust = 0),
plot.subtitle = element_text(hjust = 0.2),
plot.caption = element_text(margin = margin(20,0,0,0)),
# axis.text.y = element_text(hjust = 0.5),
axis.text.y = element_blank(), #uncomment to plot the names of barrios
# axis.text.y = element_text(hjust = 0.5),#uncomment to plot the names of barrios
axis.text.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
legend.position="none",
plot.margin = unit(c(0.3,0,0.4,4), "cm")
plot.margin = unit(c(0.31,0,0.4,4), "cm")
) +
labs(title = "Presencia de Airbnb en barrios de Valencia",
subtitle = "Número de anuncios de Airbnb",
y = "nº anuncios Airbnb",
x = NULL,
caption = "") +
geom_text(aes(label = total2018,y = total2018+2), hjust = 1,
geom_text(aes(label = suma,y = suma+2), hjust = 1,
size=4,color="#777777") +
coord_flip()
png(filename="images/airbnb/barras-mariposa-n-y-ratio-airbnb-barrios-valencia-201819.png",width = 900,height = 1000)
png(filename="images/airbnb/barras-mariposa-n-y-ratio-airbnb-barrios-valencia-201902.png",width = 900,height = 1000)
grid.arrange(plot2,plot1,ncol=2,widths=c(2,3))
dev.off()
# Mariposa para solo viviendas -----------------------------------------------------------------------------
plot1v <- ggplot(por_barrios.viviendas,aes(x = reorder(neighbourhood, -pos_ratio2018), y = ratio2018_room_type)) + #order by Value or by -pos_ratio2018
plot1v <-
ggplot(por_barrios.viviendas,aes(x = reorder(neighbourhood_cleansed, -pos_ratio_viv_2018), y = ratio2018_room_type)) + #order by Value or by -pos_ratio2018
geom_col(position = "dodge")+
scale_y_continuous(limits = c(0,21), expand = c(0,0)) +
scale_y_continuous(limits = c(0,15), expand = c(0,1)) +
theme_minimal(base_family = "Roboto Condensed", base_size = 16) +
theme(axis.title.x = element_text(margin = margin(20,0,0,0)),
plot.title = element_text(face = "bold", hjust = 0.5),
......@@ -717,9 +804,10 @@ plot1v <- ggplot(por_barrios.viviendas,aes(x = reorder(neighbourhood, -pos_ratio
coord_flip()
# Barras numero de anuncios
plot2v <- ggplot(por_barrios.viviendas,aes(x = reorder(neighbourhood, -pos_ratio2018), y = anuncios2018, fill=room_type)) + #order by Value or by -pos_ratio2018
plot2v <-
ggplot(por_barrios.viviendas,aes(x = reorder(neighbourhood_cleansed, -pos_ratio_viv_2018), y = count, fill=room_type)) + #order by Value or by -pos_ratio2018
geom_col(position = "dodge")+
scale_y_reverse(limits = c(2200,0), expand = c(0,0)) + #invert axis
scale_y_reverse(limits = c(600,0), expand = c(0,50)) + #invert axis
theme_minimal(base_family = "Roboto Condensed", base_size = 16) +
theme(
axis.title.x = element_text(margin = margin(20,0,0,0)),
......@@ -739,12 +827,12 @@ plot2v <- ggplot(por_barrios.viviendas,aes(x = reorder(neighbourhood, -pos_ratio
y = "nº anuncios viviendas Airbnb",
x = NULL,
caption = "") +
geom_text(aes(label = anuncios2018,y = anuncios2018+5),
geom_text(aes(label = count,y = count+5),
position = position_dodge(width = 1), hjust = 1,
size=4,color="#777777") +
coord_flip()
png(filename="images/airbnb/barras-mariposa-n-y-ratio-viviendas-completas-airbnb-barrios-valencia-201819.png",width = 900,height = 1000)
png(filename="images/airbnb/barras-mariposa-n-y-ratio-viviendas-completas-airbnb-barrios-valencia-201902.png",width = 900,height = 1000)
grid.arrange(plot2v,plot1v,ncol=2,widths=c(2,3))
dev.off()
......@@ -754,88 +842,145 @@ library(tmap)
library(gpclib)
library(maptools)
barrios@data$id <- rownames(barrios@data)
sf.points <- fortify(barrios, region="id") # ¿quitar .sf de barrios?
# correr gpclibPermit() si es necesario para que gpclibPermitStatus() salga true. instalar install.packages("gpclib") si es necesario.
gpclibPermit()
# Visto e nhttps://stackoverflow.com/questions/21093399/how-to-turn-gpclibpermit-to-true
# convert to character
por_barrios.viviendas$neighbourhood <- as.character(por_barrios.viviendas$neighbourhood)
# adds data
barrios.df <- left_join(sf.points, por_barrios.viviendas,by = c("N_Barri" = "neighbourhood"))
# converts coddistbar to integer to allow join
barrios@data$coddistbar <- as.integer(barrios@data$coddistbar)
barrios@data <- left_join(barrios@data ,por_barrios.viviendas,by="coddistbar")
barrios@data <- left_join(barrios@data ,select(por_barrios,coddistbar,ratio2018_listings),by="coddistbar")
colores <- c("#ededed", "#0cb2ff")
breaks.n <- c(seq(0,2000,by = 250))
png(filename="images/airbnb/mapa-coropletas-numero-anuncios-valencia-201809.png",width = 500,height = 600)
tm_shape(barrios.df) +
tm_polygons(col="total2018",
palette = colores, breaks = breaks.n,
# png(filename="images/airbnb/mapa-coropletas-numero-anuncios-valencia-201809.png",width = 500,height = 600)
tm_shape(barrios) +
tm_polygons(col="count",
palette = colores,
# breaks = breaks.n,
title = "anuncios",
border.alpha = 1, lwd = 0.2, legend.show = T, legend.outside = T) +
tm_layout(between.margin = 5, frame = FALSE,
fontfamily = "Roboto Condensed",
title = "Anuncios airbnb" ,
title.fontface = "bold") +
tm_legend(legend.text.size = 1,
tm_legend(legend.text.size = 0.5,
legend.title.size = 2)
dev.off()
# dev.off()
# Ratio anuncios / 100 viviendas
# breaks.ratio <- c(0,5,10,15,20,25)
breaks.ratio <- c(0,2.5,5,7.5,10,12.5,15,17.5,20,22.5)
# breaks.ratio <- c(0,2.5,5,7.5,10,12.5,15,17.5,20,22.5)
breaks.ratio <- c(0,1.25,2.5,5,7.5,10,12.5,15)
# png(filename="images/airbnb/mapa-coropletas-ratio-anuncios-100viv-valencia-201902.png",width = 500,height = 600)
# tm_shape(barrios) +
# tm_polygons(col="ratio2018_listings",
# palette = colores,
# breaks = breaks.ratio,
# title = "ratio anuncios / 100 viviendas",
# border.alpha = 1, lwd = 0.2, legend.show = T, legend.outside = T) +
# tm_layout(between.margin = 5, frame = FALSE,
# fontfamily = "Roboto Condensed",
# title = "Ratio anuncios airbnb por 100 domicilios" ,
# title.fontface = "bold") +
# tm_legend(legend.text.size = 1,
# legend.title.size = 2)
# dev.off()
png(filename="images/airbnb/mapa-coropletas-ratio-anuncios-100viv-valencia-201809.png",width = 500,height = 600)
tm_shape(barrios.df) +
breaks.ratio <- c(0,2,4,6,8,10,12,14,16)
png(filename="images/airbnb/mapa-coropletas-ratio-anuncios-100viv-valencia-201902.png",width = 500,height = 700)
tm_shape(barrios) +
tm_polygons(col="ratio2018_listings",
palette = colores, breaks = breaks.ratio,
title = "ratio anuncios / 100 viviendas",
border.alpha = 1, lwd = 0.2, legend.show = T, legend.outside = T) +
palette = colores,
breaks = breaks.ratio,
title = "Ratio anuncios airbnb por 100 domicilios",
border.alpha = 1, lwd = 0.7) +
tm_shape(municipios) +
tm_borders(lwd=0.1, col = "black") +
tm_layout(between.margin = 5, frame = FALSE,
fontfamily = "Roboto Condensed",
title = "Ratio anuncios airbnb por 100 domicilios" ,
title.fontface = "bold") +
tm_legend(legend.text.size = 1,
legend.title.size = 2)
main.title = "Anuncios airbnb por 100 domicilios",
main.title.size = 1.1,
main.title.fontface = "bold",
title = "" ,
title.size = 2,
title.position = c("center","top"),
title.fontface = "bold",
legend.title.size = 1,
legend.text.size = 1,
legend.position = c("left","bottom"),
legend.bg.color = "white",
legend.bg.alpha = 0.8
)
dev.off()
# Ratio anuncios vivienda completa / 100 viviendas
png(filename="images/airbnb/mapa-coropletas-ratio-anuncios-vivcompletas-100viv-valencia-201809.png",width = 500,height = 600)
tm_shape(barrios.df) +
# png(filename="images/airbnb/mapa-coropletas-ratio-anuncios-vivcompletas-100viv-valencia-201902.png",width = 500,height = 600)
# tm_shape(barrios) +
# tm_polygons(col="ratio2018_room_type",
# palette = colores,
# breaks = breaks.ratio,
# title = "ratio anuncios / 100 viviendas",
# border.alpha = 1, lwd = 0.2, legend.show = T, legend.outside = T) +
# tm_layout(between.margin = 5, frame = FALSE,
# fontfamily = "Roboto Condensed",
# title = "Ratio anuncios viv. completa airbnb por 100 domicilios" ,
# title.fontface = "bold") +
# tm_legend(legend.text.size = 1,
# legend.title.size = 2)
# dev.off()
colores <- c("#eff3ff", "#084594")
breaks.ratio <- c(0,2,4,6,8,10,12,14,16)
png(filename="images/airbnb/mapa-coropletas-ratio-anuncios-vivcompletas-100viv-valencia-201902.png",width = 500,height = 700)
tm_shape(barrios) +
tm_polygons(col="ratio2018_room_type",
palette = colores, # breaks = breaks.ratio,
title = "ratio anuncios / 100 viviendas",
border.alpha = 1, lwd = 0.2, legend.show = T, legend.outside = T) +
palette = colores,
breaks = breaks.ratio,
title = "ratio anuncios / 100 viviendas",
border.alpha = 1, lwd = 0.7) +
tm_shape(municipios) +
tm_borders(lwd=0.1, col = "black") +
tm_layout(between.margin = 5, frame = FALSE,
fontfamily = "Roboto Condensed",
title = "Ratio anuncios viv. completa airbnb por 100 domicilios" ,
title.fontface = "bold") +
tm_legend(legend.text.size = 1,
legend.title.size = 2)
main.title = "Anuncios viv. completa airbnb por 100 domicilios",
main.title.size = 1.1,
main.title.fontface = "bold",
title = "" ,
title.size = 2,
title.position = c("center","top"),
title.fontface = "bold",
legend.title.size = 1,
legend.text.size = 1,
legend.position = c("left","bottom"),
legend.bg.color = "white",
legend.bg.alpha = 0.8
)
dev.off()
# Ratio anuncios / 100 viviendas. 2017-2018
png(filename="images/airbnb/mapa-coropletas-ratio-anuncios-100viv-valencia-201709-201809.png",width = 1000,height = 600)
tm_shape(barrios.df) +
tm_polygons(col=c("ratio2017_listings","ratio2018_listings"),
palette = colores, breaks = breaks.ratio,
title = "ratio anuncios / 100 viviendas",
border.alpha = 1, lwd = 0.2, legend.show = T, legend.outside = T) +
tm_layout(between.margin = 5, frame = FALSE,
fontfamily = "Roboto Condensed",
title = c("2017. Ratio anuncios airbnb por 100 domicilios","2018. Ratio anuncios airbnb por 100 domicilios"),
title.fontface = "bold") +
tm_legend(legend.text.size = 1,
legend.title.size = 2)
dev.off()
# png(filename="images/airbnb/mapa-coropletas-ratio-anuncios-100viv-valencia-201709-201809.png",width = 1000,height = 600)
# tm_shape(barrios.df) +
# tm_polygons(col=c("ratio2017_listings","ratio2018_listings"),
# palette = colores, breaks = breaks.ratio,
# title = "ratio anuncios / 100 viviendas",
# border.alpha = 1, lwd = 0.2, legend.show = T, legend.outside = T) +
# tm_layout(between.margin = 5, frame = FALSE,
# fontfamily = "Roboto Condensed",
# title = c("2017. Ratio anuncios airbnb por 100 domicilios","2018. Ratio anuncios airbnb por 100 domicilios"),
# title.fontface = "bold") +
# tm_legend(legend.text.size = 1,
# legend.title.size = 2)
# dev.off()
ver <- as.data.frame(barrios@data[,c("nombre","ratio2018_room_type","ratio2018_listings")])
# Prepare for cartograma -----------------------------------------------------
library(geojsonio)
barrios_json <- geojson_json(barrios.df)
# must convert to spatial before exporting
writeOGR(as(barrios.df, 'Spatial'), "tmp/test_geojson", layer="barrios", driver="GeoJSON")
barrios_json <- geojson_json(barrios)
# salva como geojson
geojson_write(barrios_json, file = "tmp/test_geojson")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment