# Librerias: 

library(readxl)
library(ggplot2)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(stringr)
# Leer el archivo completo sin saltar filas
datosPJ <- read_excel("C:/Users/Keytty/OneDrive/Escritorio/Estudios de opinión y opinión pública/Vídeo/Valoración política poder judicial (2014 a 2024).xlsx", skip = 0)
# Renombrar columnas
colnames(datosPJ) <- c("Fecha", "Valoracion")

# Convertir puntuación a numérica
datosPJ$Valoracion <- as.numeric(datosPJ$Valoracion)

# Limpiar y convertir fechas: "Noviembre 2014:" -> "Noviembre 2024"
datosPJ <- datosPJ %>%
  mutate(
    Fecha = str_replace(Fecha, ":", ""),
    Fecha = paste("01", Fecha),  # añadir día ficticio
    Fecha = as.Date(Fecha, format = "%d %B %Y")
  )
ggplot(datosPJ, aes(x = Fecha, y = Valoracion)) +
  geom_line(color = "pink", linewidth = 1.2) +
  geom_point(color = "black") +
  scale_x_date(
    breaks = datosPJ$Fecha,                             # usar solo las fechas reales
    labels = format(datosPJ$Fecha, "%b %Y"),            # mostrar como "Jul 2014", etc.
    expand = c(0.01, 0.01)
  ) +
  labs(title = "Valoración Política del Poder Judicial (2014–2024)",
       x = "Fecha",
       y = "Puntuación") +
  theme_minimal(base_size = 12) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # gira etiquetas 45°

Se realizará una desviación estándar para responder a la hipotesis de este trabajo.

head(datosPJ)
## # A tibble: 6 × 2
##   Fecha      Valoracion
##   <date>          <dbl>
## 1 2014-07-01        6.8
## 2 2015-04-01        6.3
## 3 2016-04-01        6.7
## 4 2017-11-01        5.9
## 5 2018-11-01        6.2
## 6 2019-11-01        6.3
summary(datosPJ)
##      Fecha              Valoracion   
##  Min.   :2014-07-01   Min.   :5.900  
##  1st Qu.:2016-04-01   1st Qu.:6.200  
##  Median :2018-11-01   Median :6.300  
##  Mean   :2018-11-04   Mean   :6.311  
##  3rd Qu.:2020-11-01   3rd Qu.:6.300  
##  Max.   :2023-09-01   Max.   :6.800
# Se calculan medidas estándar: 

mean(datosPJ$Valoracion)    # Promedio
## [1] 6.311111
median(datosPJ$Valoracion)  # Mediana
## [1] 6.3
mode_val <- as.numeric(names(sort(table(datosPJ$Valoracion), decreasing=TRUE)[1]))  # Moda
sd(datosPJ$Valoracion)       # Desviación estándar
## [1] 0.2803767
quantile(datosPJ$Valoracion) # Cuartiles
##   0%  25%  50%  75% 100% 
##  5.9  6.2  6.3  6.3  6.8
# Regresión Líneal por tendencia: 

modelo <- lm(Valoracion ~ Fecha, data = datosPJ)
summary(modelo)
## 
## Call:
## lm(formula = Valoracion ~ Fecha, data = datosPJ)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.46004 -0.11151  0.02315  0.17050  0.27790 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.683e+00  1.351e+00   6.428 0.000358 ***
## Fecha       -1.330e-04  7.558e-05  -1.759 0.121972    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2496 on 7 degrees of freedom
## Multiple R-squared:  0.3065, Adjusted R-squared:  0.2075 
## F-statistic: 3.094 on 1 and 7 DF,  p-value: 0.122
library(lubridate)
## 
## Adjuntando el paquete: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
# Extraer el año de la fecha
datosPJ$Fecha <- year(as.Date(datosPJ$Fecha))

# Verifica si ahora tienes solo los años
unique(datosPJ$Fecha)
## [1] 2014 2015 2016 2017 2018 2019 2020 2022 2023
ggplot(datosPJ, aes(x = Fecha, y = Valoracion)) +
  geom_point(color = "blue", size = 3) +  # Puntos
  geom_smooth(method = "lm", se = TRUE, color = "red", fill = "pink", alpha = 0.3) +  # Línea de regresión
  scale_x_continuous(breaks = seq(2014, 2024, by = 1), limits = c(2014, 2024)) +  # Asegura que se muestren 2014 y 2015
  labs(title = "Relación entre Fecha y Valoración del Poder Judicial",
       subtitle = "Línea de regresión lineal con intervalo de confianza del 95%",
       x = "Año", y = "Valoración Promedio") +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", size = 16),
    plot.subtitle = element_text(size = 12),
    axis.title = element_text(size = 14),
    axis.text = element_text(size = 12)
  )
## `geom_smooth()` using formula = 'y ~ x'