Código
library(tidyverse)
library(lubridate)
library(plotly)
library(tidyverse)
library(lubridate)
library(plotly)
<- read_csv("incendios-ca-historicos-2013-2025.csv")
incendios
<-
incendios_anio |>
incendios mutate(
anio = year(ALARM_DATE)
|>
) group_by(anio) |>
summarise(
n = n(), # cantidad de incendios
area_quemada_total = sum(GIS_ACRES, na.rm = TRUE), # área quemada total
temperatura_2m_promedio = mean(TEMPERATURE_2M_ALARM_DATE, na.rm = TRUE), # temperatura promedio
.groups = "drop"
)
<-
incendios_mes |>
incendios mutate(
anio = year(ALARM_DATE),
mes = month(ALARM_DATE, label = TRUE, abbr = TRUE)
|>
) group_by(anio, mes) |>
summarise(
n = n(), # cantidad de incendios
area_quemada_total = sum(GIS_ACRES, na.rm = TRUE), # área quemada total
temperatura_2m_promedio = mean(TEMPERATURE_2M_ALARM_DATE, na.rm = TRUE), # temperatura promedio
.groups = "drop"
)
# Asegurar que los meses queden en orden enero-diciembre
$mes <- factor(
incendios_mes$mes,
incendios_meslevels = month(ymd("2025-01-01") + months(0:11), label = TRUE, abbr = TRUE)
)
# Gráfico de barras agrupadas
<-
g ggplot(incendios_anio, aes(x = anio, y = n)) +
geom_col(fill = "red") +
labs(
x = "Año",
y = "Cantidad de incendios"
+
) theme_minimal(base_size = 12) +
theme(
panel.grid.major.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)
)
# Convertir a interactivo
<- ggplotly(g, tooltip = c("anio", "n"))
g_interactivo
# Mostrar
g_interactivo
# Gráfico de barras agrupadas
<-
g ggplot(incendios_anio, aes(x = anio, y = area_quemada_total)) +
geom_col(fill = "red") +
labs(
x = "Año",
y = "Área quemada (acres)"
+
) scale_y_continuous(labels = scales::comma) +
theme_minimal(base_size = 12) +
theme(
panel.grid.major.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)
)
# Convertir a interactivo
<- ggplotly(g, tooltip = c("anio", "n"))
g_interactivo
# Mostrar
g_interactivo
# Gráfico de barras agrupadas
<-
g ggplot(incendios_anio, aes(x = anio, y = temperatura_2m_promedio)) +
geom_col(fill = "red") +
labs(
x = "Año",
y = "Temperatura promedio (C)"
+
) scale_y_continuous() +
theme_minimal(base_size = 12) +
theme(
panel.grid.major.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)
)
# Convertir a interactivo
<- ggplotly(g, tooltip = c("anio", "temperatura_2m_promedio"))
g_interactivo
# Mostrar
g_interactivo
# Gráfico de barras agrupadas
<-
g ggplot(incendios_mes, aes(x = mes, y = n, fill = factor(anio))) +
geom_col(position = position_dodge(width = 0.8)) +
labs(
x = "Mes",
y = "Cantidad de incendios",
fill = "Año"
+
) scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
scale_fill_brewer(palette = "YlOrRd") +
theme_minimal(base_size = 12) +
theme(
panel.grid.major.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)
)
# Convertir a interactivo
<- ggplotly(g, tooltip = c("anio", "mes", "n")) g_interactivo
Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette YlOrRd is 9
Returning the palette you asked for with that many colors
# Mostrar
g_interactivo
# Gráfico de barras agrupadas
<-
g ggplot(incendios_mes, aes(x = mes, y = area_quemada_total, fill = factor(anio))) +
geom_col(position = position_dodge(width = 0.8)) +
labs(
x = "Mes",
y = "Área quemada (acres)",
fill = "Año"
+
) scale_y_continuous(labels = scales::comma) +
scale_fill_brewer(palette = "YlOrRd") +
theme_minimal(base_size = 12) +
theme(
panel.grid.major.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)
)
# Convertir a interactivo
<- ggplotly(g, tooltip = c("anio", "mes", "area_quemada_total")) g_interactivo
Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette YlOrRd is 9
Returning the palette you asked for with that many colors
# Mostrar
g_interactivo
# Gráfico de barras agrupadas
<-
g ggplot(incendios_mes, aes(x = mes, y = temperatura_2m_promedio, fill = factor(anio))) +
geom_col(position = position_dodge(width = 0.8)) +
labs(
x = "Mes",
y = "Temperatura promedio (C)",
fill = "Año"
+
) scale_y_continuous() +
scale_fill_brewer(palette = "YlOrRd") +
theme_minimal(base_size = 12) +
theme(
panel.grid.major.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)
)
# Convertir a interactivo
<- ggplotly(g, tooltip = c("anio", "mes", "temperatura_2m_promedio")) g_interactivo
Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette YlOrRd is 9
Returning the palette you asked for with that many colors
# Mostrar
g_interactivo