ANÁLISIS DE PRECIPITACIÓN - INGENIERÍA

————————————

1. Cargar librerías

Si no están instaladas, la primera vez debes correr: install.packages(“tidyverse”)

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)

2. Cargar los datos

datos_lluvia <- read_csv("data/precipitacion_sucre.csv")
## Rows: 8 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): Estacion
## dbl  (1): Precipitacion_mm
## date (1): Fecha
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

3. Procesamiento y análisis

analisis_mensual <- datos_lluvia %>%
  mutate(Mes = month(Fecha, label = TRUE, abbr = FALSE)) %>%
  group_by(Mes) %>%
  summarise(PrecipitacionTotal_mm = sum(Precipitacion_mm))
print("Resumen de precipitación mensual:")
## [1] "Resumen de precipitación mensual:"
print(analisis_mensual)
## # A tibble: 8 × 2
##   Mes      PrecipitacionTotal_mm
##   <ord>                    <dbl>
## 1 January                      5
## 2 February                     2
## 3 March                       15
## 4 April                       80
## 5 May                        150
## 6 June                       220
## 7 July                       180
## 8 August                     160

4. Visualización

grafico_precipitacion <- ggplot(analisis_mensual, aes(x = Mes, y = PrecipitacionTotal_mm)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  labs(
    title = "Precipitación Mensual Total en la Estación 'La Mojana'",
    subtitle = "Año 2024 (Datos parciales)",
    x = "Mes",
    y = "Precipitación Total (mm)"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
print(grafico_precipitacion)

5. Guardar el gráfico

if (!dir.exists("output")) dir.create("output")
ggsave("output/grafico_precipitacion_mensual.png", plot = grafico_precipitacion, width = 8, height = 6)

print("¡Análisis completado! El gráfico se ha guardado en la carpeta 'output'.")
## [1] "¡Análisis completado! El gráfico se ha guardado en la carpeta 'output'."