# Instalar si es necesario: install.packages("ggplot2")
library(ggplot2)
library(tidyr)
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
# 1. Crear el dataframe con los datos
data <- data.frame(
  Enunciado = factor(1:10),
  Pretest = c(53.3, 20.0, 73.3, 70.0, 80.0, 63.3, 23.3, 83.3, 70.0, 70.0),
  Postest = c(96.7, 93.3, 100.0, 96.7, 100.0, 96.7, 96.7, 100.0, 93.3, 96.7)
)

# 2. Transformar los datos a formato largo (long format) para ggplot
data_long <- data %>%
  pivot_longer(cols = c("Pretest", "Postest"), 
               names_to = "Momento", 
               values_to = "Porcentaje")

# 3. Crear el gráfico
ggplot(data_long, aes(x = Enunciado, y = Porcentaje, fill = Momento)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() + # Voltear ejes para lectura horizontal como en tu imagen
  theme_minimal() +
  labs(title = "Nivel de Conocimiento Correcto por Enunciado",
       x = "N.º Enunciado",
       y = "Porcentaje de aciertos") +
  scale_fill_manual(values = c("Pretest" = "#E69F00", "Postest" = "#009E73")) +
  geom_text(aes(label = paste0(Porcentaje, "%")), 
            position = position_dodge(width = 0.9), 
            hjust = -0.1, size = 3)

# Instalar si es necesario: install.packages(c("ggplot2", "dplyr"))
library(ggplot2)
library(dplyr)

# --- 1. Gráfica para Tabla 1 (Edad) ---
df_edad <- data.frame(
  Rango = c("20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60+"),
  Frecuencia = c(2, 5, 4, 4, 3, 2, 3, 4, 3)
)

g1 <- ggplot(df_edad, aes(x = Rango, y = Frecuencia)) +
  geom_bar(stat = "identity", fill = "#4c72b0") +
  theme_minimal() +
  labs(title = "Distribución por Rangos de Edad", x = "Rangos (Años)", y = "Nº de Personas") +
  geom_text(aes(label = Frecuencia), vjust = -0.5)

# --- 2. Gráfica para Tabla 2 (Sexo) ---
df_sexo <- data.frame(
  Sexo = c("Femenino", "Masculino"),
  Frecuencia = c(21, 9)
)

g2 <- ggplot(df_sexo, aes(x = "", y = Frecuencia, fill = Sexo)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  theme_void() +
  labs(title = "Distribución por Sexo") +
  scale_fill_manual(values = c("#f781bf", "#377eb8")) +
  geom_text(aes(label = paste0(round(Frecuencia/30*100), "%")), position = position_stack(vjust = 0.5))

# --- 3. Gráfica para Tabla 4 (Satisfacción) ---
df_sat <- data.frame(
  Item = c("Tema claro", "Info. útil", "Tiempo", "Motivación", "Recomendación"),
  Porcentaje = c(96.7, 100.0, 93.3, 96.7, 100.0)
)

g3 <- ggplot(df_sat, aes(x = Item, y = Porcentaje)) +
  geom_bar(stat = "identity", fill = "#55a868") +
  theme_minimal() +
  coord_flip() +
  ylim(80, 105) +
  labs(title = "Evaluación de Satisfacción (% SÍ)", x = "", y = "Porcentaje") +
  geom_text(aes(label = paste0(Porcentaje, "%")), hjust = -0.1)

# Imprimir las gráficas
print(g1); print(g2); print(g3)

## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_bar()`).