-
Notifications
You must be signed in to change notification settings - Fork 0
/
flexdash.Rmd
245 lines (182 loc) · 6.11 KB
/
flexdash.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
59
60
61
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
---
title: "Índice de Precios"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
# import the data
library(readxl)
library(xts)
library(ggplot2)
library(TSstudio)
data <- readxl::read_xlsx("indice_precios_de_viviendas_nuevas.xlsx")
# transform each column in a distinct time series except the first three columns
df_xts <- ts(data[, -1], start=c(2016,1), frequency = 4)
#df_xts <- xts(data[, -1], )
by_apartment <- c()
by_all_cities <- c("Bogotá","Medellín", "Cali", "Bucaramanga", "Cartagena", "Barranquilla", "Manizales", "Pereira", "Villavicencio", "Pasto", "Neiva", "Cucuta", "Armenia", "Popayan", "Ibague")
by_important_cities <- c("Bogotá","Medellín", "Cali", "Bucaramanga", "Cartagena", "Barranquilla")
by_apartment <- c("barranquilla_apartamentos", "bogotacundi_apartamentos", "bucaramanga_apartamentos", "cali_apartamentos", "cartagena_apartamentos", "medellin_apartamentos")
by_casas <- c("barranquilla_casas", "bogotacundi_casas", "bucaramanga_casas", "cali_casas", "cartagena_casas", "medellin_casas")
by_high <- c("Alto/Barranquilla", "Alto/Bogotá", "Alto/Bucaramanga", "Alto/Cali", "Alto/Cartagena", "Alto/Medellín")
by_medium <- c("Medio/Barranquilla", "Medio/Bogotá", "Medio/Bucaramanga", "Medio/Cali", "Medio/Cartagena", "Medio/Medellín")
by_low <- c("Bajo/Barranquilla", "Bajo/Bogotá", "Bajo/Bucaramanga", "Bajo/Cali", "Bajo/Cartagena", "Bajo/Medellín")
```
Column
-----------------------------------------------------------------------
### Gráfico de Lineas
```{r}
ts_plot(df_xts[, by_important_cities], title= "")
```
### Barranquilla City Heatmap
```{r}
ts_heatmap(df_xts[, "Barranquilla"], title = "Barranquilla", color="viridis")
```
### Distribuciones
```{r}
library(tidyr)
library(plotly)
long_form <- data[ , by_important_cities] |>
pivot_longer(cols = everything(), names_to = "series", values_to = "value")
graph <- ggplot(long_form, aes(x = series, y = value)) +
geom_jitter(aes(color = series), width = 0.2, size = 3, alpha= 0.7) +
theme_minimal() +
scale_color_viridis_d()
ggplotly(graph)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart C
```{r}
library(highcharter)
library(viridis)
# Asegúrate de que tu dataframe tiene una columna 'year'
#
# color light blueish
color <- "#7cb5ec"
#Map list of coordinates for every city
coordinates <- list(
Bogotá = c(4.7110, -74.0721),
Medellín = c(6.2442, -75.5812),
Cali = c(3.4516, -76.5320),
Bucaramanga = c(7.1254, -73.1198),
Cartagena = c(10.3910, -75.4794),
Barranquilla = c(10.9639, -74.7964)
)
coordinates_of_all_cities <- list(
Bogotá = c(4.7110, -74.0721),
Medellín = c(6.2442, -75.5812),
Cali = c(3.4516, -76.5320),
Bucaramanga = c(7.1254, -73.1198),
Cartagena = c(10.3910, -75.4794),
Barranquilla = c(10.9639, -74.7964),
Manizales = c(5.0689, -75.5174),
Pereira = c(4.8133, -75.6961),
Villavicencio = c(4.1420, -73.6266),
Pasto = c(1.2136, -77.2811),
Neiva = c(2.9273, -75.2819),
Cucuta = c(7.8939, -72.5078),
Armenia = c(4.5341, -75.6757),
Popayan = c(2.4540, -76.6093),
Ibague = c(4.4377, -75.2421)
)
data_for_second_column = coordinates_of_all_cities
cities_to_plot = by_all_cities
# Crear un dataframe con las coordenadas y los valores
map_data <- data.frame(
city = names(data_for_second_column),
latitude = sapply(data_for_second_column, `[`, 1),
longitude = sapply(data_for_second_column, `[`, 2),
value = unlist(data[3, cities_to_plot]) # Asegúrate de que los índices se alinean correctamente
)
options(highcharter.download_map_data = TRUE)
hcmap("countries/co/co-all", showInLegend = FALSE) |>
hc_add_series(
data = map_data,
name = "Precios de vivienda",
type = "mapbubble",
minSize = "1%",
maxSize = "10%",
color = color,
zIndex = 1,
#dataLabels = list(enabled = TRUE, format = '{point.city}: {point.value}'),
mapping = hcaes(
lat = latitude, # Coordenadas de latitud
lon = longitude, # Coordenadas de longitud
z = value, # Valores para ajustar el tamaño de las burbujas
name = city
)
) |>
hc_mapNavigation(enabled = TRUE) |>
#
hc_colorAxis(
stops = color_stops(colors = viridis(max(map_data[, "value"]))) # Define la escala de colores Viridis
)
```
### Chart D {data-height=225}
```{r}
library(plotly)
# bar grapht with plotly
barplot <- ggplot(map_data, aes(x = city, y = value, fill=value)) +
geom_bar(stat = "identity") +
scale_fill_viridis() +
labs(x = "Ciudades", y = "Indice de precios") +
theme_minimal()
ggplotly(barplot)
```
Column
-----------------------------------------------------------------------
### card E
```{r}
sliderInput("quarter", "Selecciona el trimestre:",
min = as.Date("2016-01-01"),
max = as.Date("2024-04-01"),
value = as.Date("2020-01-01"),
width = "100%",
timeFormat = "%D", # Formato de trimestre
step = 90, # Aproximadamente 3 meses (un trimestre)
animate = TRUE # Esto añade un botón de play
)
```
```{r}
selectInput("city", "Selecciona la ciudad", choices = by_all_cities)
```
```{r}
selectInput("stratum", "Selecciona el estrato", choices = c("Alto", "Medio", "Bajo"))
```
```{r}
selectInput("type", "Selecciona el tipo de vivienda", choices = c("Apartamentos", "Casas"))
```
```{r}
```
### Pronóstico
```{r}
library(TSstudio)
library(forecast)
## Modelo ARIMA (1,0,1) univariado
fcp <- data |>
select("Barranquilla")|>
ts(start=c(2016,1), frequency = 4) |>
arima(order = c(0,2,0)) |>
forecast(h=4)
plot <- plot_forecast(fcp)
plot
```
### Prónostico Backtested
```{r}
library(forecast)
library(TSstudio)
fc_span <- 4
quilla <- data |>
select("Barranquilla") |>
ts(start=c(2016,1), frequency = fc_span)
quillas <- ts_split(quilla, sample.out = fc_span)
train <- quillas$train
test <- quillas$test
fc <- arima(train, order = c(0,2,0)) |>
forecast(h = fc_span)
test_forecast(actual = quilla, forecast.obj = fc, test = test)
```