palabras.R 9.4 KB
Newer Older
1 2 3 4 5 6 7 8
# palabras

# Instala y carga librerías ----
library(tidyverse)

# load data ----
local_activo <- read.delim("tmp/listings-palabras.csv",sep = ",")

9
# Transform data para distritos ------------------------------------------------------------
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
distritos.count <- local_activo %>% group_by(neighbourhood_group_cleansed) %>% summarise(total = n() )
playa.desc <- local_activo %>% group_by(neighbourhood_group_cleansed,descripcion.tiene.playa) %>% summarise(count = n() ) %>% ungroup()
centro.desc <- local_activo %>% group_by(neighbourhood_group_cleansed,descripcion.tiene.centro) %>% summarise(count = n() ) %>% ungroup()
playa.tit <- local_activo %>% group_by(neighbourhood_group_cleansed,titulo.tiene.playa) %>% summarise(count = n() ) %>% ungroup()
centro.tit <- local_activo %>% group_by(neighbourhood_group_cleansed,titulo.tiene.centro) %>% summarise(count = n() ) %>% ungroup()

names(distritos.count) <- c("distrito","total")
names(playa.desc) <- c("distrito","tiene","anuncios.playa.desc")
names(centro.desc) <- c("distrito","tiene","anuncios.centro.desc")
names(playa.tit) <- c("distrito","tiene","anuncios.playa.tit")
names(centro.tit) <- c("distrito","tiene","anuncios.centro.tit")

distritos.count <- full_join(distritos.count, playa.desc %>% filter(tiene==TRUE) %>% select(distrito,anuncios.playa.desc),by = "distrito")
distritos.count <- full_join(distritos.count, centro.desc %>% filter(tiene==TRUE) %>% select(distrito,anuncios.centro.desc),by = "distrito")
distritos.count <- full_join(distritos.count, playa.tit %>% filter(tiene==TRUE) %>% select(distrito,anuncios.playa.tit),by = "distrito")
distritos.count <- full_join(distritos.count, centro.tit %>% filter(tiene==TRUE) %>% select(distrito,anuncios.centro.tit),by = "distrito")

distritos.count$playa.desc.p <- round(100* distritos.count$anuncios.playa.desc / distritos.count$total, digits=1)
distritos.count$centro.desc.p <- round(100* distritos.count$anuncios.centro.desc/ distritos.count$total, digits=1)
distritos.count$playa.tit.p <- round(100* distritos.count$anuncios.playa.tit / distritos.count$total, digits=1)
distritos.count$centro.tit.p <- round(100* distritos.count$anuncios.centro.tit / distritos.count$total, digits=1)

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
# Transform data para barrios ------------------------------------------------------------
barrios.count <- local_activo %>% group_by(neighbourhood_cleansed) %>% summarise(total = n() )
playa.desc <- local_activo %>% group_by(neighbourhood_cleansed,descripcion.tiene.playa) %>% summarise(count = n() ) %>% ungroup()
centro.desc <- local_activo %>% group_by(neighbourhood_cleansed,descripcion.tiene.centro) %>% summarise(count = n() ) %>% ungroup()
playa.tit <- local_activo %>% group_by(neighbourhood_cleansed,titulo.tiene.playa) %>% summarise(count = n() ) %>% ungroup()
centro.tit <- local_activo %>% group_by(neighbourhood_cleansed,titulo.tiene.centro) %>% summarise(count = n() ) %>% ungroup()

names(barrios.count) <- c("barrio","total")
names(playa.desc) <- c("barrio","tiene","anuncios.playa.desc")
names(centro.desc) <- c("barrio","tiene","anuncios.centro.desc")
names(playa.tit) <- c("barrio","tiene","anuncios.playa.tit")
names(centro.tit) <- c("barrio","tiene","anuncios.centro.tit")

barrios.count <- full_join(barrios.count, playa.desc %>% filter(tiene==TRUE) %>% select(barrio,anuncios.playa.desc),by = "barrio")
barrios.count <- full_join(barrios.count, centro.desc %>% filter(tiene==TRUE) %>% select(barrio,anuncios.centro.desc),by = "barrio")
barrios.count <- full_join(barrios.count, playa.tit %>% filter(tiene==TRUE) %>% select(barrio,anuncios.playa.tit),by = "barrio")
barrios.count <- full_join(barrios.count, centro.tit %>% filter(tiene==TRUE) %>% select(barrio,anuncios.centro.tit),by = "barrio")

barrios.count$playa.desc.p <- round(100* barrios.count$anuncios.playa.desc / barrios.count$total, digits=1)
barrios.count$centro.desc.p <- round(100* barrios.count$anuncios.centro.desc/ barrios.count$total, digits=1)
barrios.count$playa.tit.p <- round(100* barrios.count$anuncios.playa.tit / barrios.count$total, digits=1)
barrios.count$centro.tit.p <- round(100* barrios.count$anuncios.centro.tit / barrios.count$total, digits=1)

barrios.count %>% filter(!is.na(playa.tit.p)) %>%
ggplot(aes(reorder(barrio,playa.tit.p),playa.tit.p)) +
57 58
  geom_col() +
  coord_flip() +
59
  labs(title="Porcentaje anuncios lleva la 'palabra' playa en título",
60
       # subtitle = paste("Airbnb en ", local_activo_name, ". ",fecha_simple,".",sep=""),
61
       x="barrio",
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
       y="porcentaje anuncios",
       caption = caption_1) +
  theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
  theme(
    panel.grid.minor.y = element_blank(),
    panel.grid.major.y = element_blank(),
    legend.position = "top"
  ) 


# maps ------------------------------
# read shapes
library(rgdal)
library(ggmap) #for theme nothing
library(reshape)
library(gridExtra)
library(gsubfn) # select text in the parenthesis with regex
# Mapas de coropletas 
library(tmap)
library("tmaptools")
# to save interactive maps
library(htmlwidgets)
# para sprintf
library(htmltools)

# shapes
88
barrios <-   readOGR("data/original/shapes/barrios.valencia.wgs84.geojson")
89 90
distritos <- readOGR("data/original/shapes/distritos.valencia.wgs84.geojson")
# municipios <-  distritos
91
municipios <- readOGR("data/original/shapes/municipios.provincia.valencia.geojson")
92 93 94 95 96 97

# Define paleta de color
palette1 <- c("#4292c6","#6baed6","#9ecae1","#fee5d9")
colores <- c("#eff3ff", "#084594")


98
# Añade datos a contornos--------------------------------------------------------------
99
distritos@data <- full_join(distritos@data, distritos.count,by =c("nombre" = "distrito"))
100
barrios@data <- full_join(barrios@data, barrios.count,by =c("nombre" = "barrio"))
101

102 103
# mapa para distritos -----------------
png(filename="images/airbnb/palabras/playa-centro-mapa-distritos-descripcion.png",width = 600,height = 600)
104 105 106 107 108 109
tm_shape(distritos) +
  tm_polygons(col=c("playa.desc.p","centro.desc.p"),
              palette = colores,
              # breaks = breaks,
              title = "% anuncios",
              border.alpha = 1, lwd = 0.7) +
110 111
  tm_shape(municipios) +
    tm_borders(lwd=0.1, col = "black") +
112 113 114
  tm_layout(between.margin = 5, frame = FALSE,
            fontfamily = "Roboto Condensed", 
            main.title = "% de anuncios con la palabra en descripción...",
115
            main.title.size = 2.2,
116
            title = c("playa","centro") ,
117 118
            title.size = 2,
            title.position = c("center","top"),
119 120
            title.fontface = "bold",
            legend.title.size = 1,
121
            legend.text.size = 1,
122 123
            legend.position = c("left","bottom"),
            legend.bg.color = "white",
124
            legend.bg.alpha = 0.8
125
  ) 
126
dev.off()
127

128
png(filename="images/airbnb/palabras/playa-centro-mapa-distritos-titulo.png",width = 600,height = 600)
129 130 131 132 133 134
tm_shape(distritos) +
  tm_polygons(col=c("playa.tit.p","centro.tit.p"),
              palette = colores,
              # breaks = breaks,
              title = "% anuncios",
              border.alpha = 1, lwd = 0.7) +
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  tm_shape(municipios) +
    tm_borders(lwd=0.1, col = "black") +
  tm_layout(between.margin = 5, frame = FALSE,
            fontfamily = "Roboto Condensed", 
            main.title = "% de anuncios con la palabra en título...",
            main.title.size = 2.2,
            title = c("playa","centro") ,
            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()

# mapa para barrios -----------------
png(filename="images/airbnb/palabras/playa-centro-mapa-barrios-descripcion.png",width = 600,height = 600)
tm_shape(barrios) +
  tm_polygons(col=c("playa.desc.p","centro.desc.p"),
              palette = colores,
              # breaks = breaks,
              title = "% anuncios",
              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", 
            main.title = "% de anuncios con la palabra en descripción...",
            main.title.size = 2.2,
            title = c("playa","centro") ,
            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()

png(filename="images/airbnb/palabras/playa-centro-mapa-barrios-titulo.png",width = 600,height = 600)
tm_shape(barrios) +
  tm_polygons(col=c("playa.tit.p","centro.tit.p"),
              palette = colores,
              # breaks = breaks,
              title = "% anuncios",
              border.alpha = 1, lwd = 0.7) +
  tm_shape(municipios) +
    tm_borders(lwd=0.1, col = "black") +
188 189 190
  tm_layout(between.margin = 5, frame = FALSE,
            fontfamily = "Roboto Condensed", 
            main.title = "% de anuncios con la palabra en título...",
191
            main.title.size = 2.2,
192
            title = c("playa","centro") ,
193 194
            title.size = 2,
            title.position = c("center","top"),
195 196
            title.fontface = "bold",
            legend.title.size = 1,
197
            legend.text.size = 1,
198 199
            legend.position = c("left","bottom"),
            legend.bg.color = "white",
200
            legend.bg.alpha = 0.8
201
  ) 
202
dev.off()