Los hombres y mujeres que contestaron un cuestionario acerca de las diferencias de género están clasificados en tres grupos, según sus respuestas a la primera pregunta:
# Matriz de datos original
datos <- matrix(
c(37, 49, 72, 7, 50, 3),
nrow = 2,
byrow = TRUE,
dimnames = list(
Género = c("Hombres", "Mujeres"),
Grupo = c("Grupo 1", "Grupo 2", "Grupo 3")
)
)
# Convertir a formato largo
library(tidyr)
datos_largos <- as.data.frame(datos) %>%
tibble::rownames_to_column("Género") %>%
pivot_longer(cols = -Género, names_to = "Grupo", values_to = "Conteo")
knitr::kable(datos, caption = "Tabla de contingencia original")
| Grupo 1 | Grupo 2 | Grupo 3 | |
|---|---|---|---|
| Hombres | 37 | 49 | 72 |
| Mujeres | 7 | 50 | 3 |
library(plotly)
library(dplyr)
# Función para crear gráficos de pastel con estilo profesional
crear_pastel <- function(genero, colores) {
datos_genero <- datos[genero, ]
plot_ly(
labels = names(datos_genero),
values = datos_genero,
type = "pie",
hole = 0.4,
marker = list(colors = colores),
textinfo = "percent+label",
hoverinfo = "text+value",
text = ~paste("</br>Género:", genero, "</br>Grupo:", names(datos_genero)),
showlegend = FALSE
) %>%
layout(
title = paste("Distribución de", genero),
font = list(family = "Arial", size = 12),
annotations = list(
text = paste("Total:", sum(datos_genero)),
x = 0.5, y = 1.1, showarrow = FALSE
)
)
}
# Crear gráficos
pastel_hombres <- crear_pastel("Hombres", c("#3B9AB2", "#78B7C5", "#EBCC2A"))
pastel_mujeres <- crear_pastel("Mujeres", c("#F21A00", "#E58601", "#46ACC8"))
htmltools::tagList(pastel_hombres, pastel_mujeres)
library(ggplot2)
library(ggthemes)
ggplot(datos_largos, aes(x = Grupo, y = Conteo, fill = Género)) +
geom_bar(stat = "identity", position = position_dodge2(preserve = "single"), width = 0.7) +
geom_text(aes(label = Conteo), position = position_dodge2(width = 0.7), vjust = -0.5, size = 4) +
scale_fill_manual(values = c("Hombres" = "#3B9AB2", "Mujeres" = "#F21A00")) +
labs(
title = "Distribución de respuestas por género y grupo",
subtitle = "Comparación de conteos absolutos",
caption = "Fuente: Datos del ejercicio 1"
) +
theme_clean() +
theme(
plot.title = element_text(face = "bold", size = 16, hjust = 0.5),
axis.title = element_text(size = 12),
legend.position = "top"
)
# Calcular proporciones
datos_prop <- datos_largos %>%
group_by(Grupo) %>%
mutate(Prop = Conteo / sum(Conteo))
ggplot(datos_prop, aes(x = Grupo, y = Prop, fill = Género)) +
geom_bar(stat = "identity") +
geom_text(aes(label = scales::percent(Prop, accuracy = 0.1)),
position = position_stack(vjust = 0.5),
color = "white", size = 5) +
scale_fill_manual(values = c("Hombres" = "#3B9AB2", "Mujeres" = "#F21A00")) +
scale_y_continuous(labels = scales::percent) +
labs(
title = "Composición porcentual por género",
subtitle = "Proporción relativa en cada grupo",
y = "Porcentaje"
) +
theme_economist() +
theme(
plot.title = element_text(face = "bold", size = 14),
axis.title.x = element_blank()
)
La gráfica más efectiva vendría a ser la de barras apiladas, puesto que hay una compración directa entre los grupos,
precio <- rep(10:14, each = 2)
ventas <- c(23, 21, 19, 18, 15, 17, 19, 20, 25, 24)
datos_vinos <- data.frame(Precio = precio, Ventas = ventas)
knitr::kable(
matrix(ventas, ncol = 2, byrow = TRUE,
dimnames = list(paste("$", 10:14), c("Venta 1", "Venta 2"))),
caption = "Cajas vendidas por 10,000 habitantes"
)
| Venta 1 | Venta 2 | |
|---|---|---|
| $ 10 | 23 | 21 |
| $ 11 | 19 | 18 |
| $ 12 | 15 | 17 |
| $ 13 | 19 | 20 |
| $ 14 | 25 | 24 |
library(plotly)
library(ggplot2)
# Versión estática con ggplot2
gg <- ggplot(datos_vinos, aes(x = Precio, y = Ventas)) +
geom_point(size = 3, color = "#E41A1C", alpha = 0.8) +
geom_smooth(method = "lm", formula = y ~ poly(x, 2),
color = "#377EB8", se = FALSE) +
labs(
title = "Relación entre Precio y Demanda de Vino",
subtitle = "Patrón no lineal: demanda aumenta en precios altos",
x = "Precio por botella (Dolar)",
y = "Cajas vendidas/10,000 hab..."
) +
theme_minimal() +
scale_x_continuous(breaks = 10:14) +
theme(plot.title = element_text(face = "bold", size = 16))
# Convertir a Plotly para interactividad
ggplotly(gg) %>%
layout(
hoverlabel = list(bgcolor = "white"),
annotations = list(
text = "Click y arrastrar para hacer zoom, doble click para deshacer",
xref = "paper", yref = "paper",
x = 0.05, y = 0.95, showarrow = FALSE
)
)
Demanda inicialmente disminuye de $10 a $12, luego aumenta en $13 y $14, indicando un mínimo de ventas de 15 a 17 cajas y un máximo de 24 a 25, respectivamente.