library(rio)
## Warning: package 'rio' was built under R version 4.3.3
data = import("respaldo.xlsx")
data2 = import("columnaaa.xlsx")
str(data2)
## 'data.frame': 10 obs. of 4 variables:
## $ Partido : chr "Nuevas Ideas" "Partido de Concertación Nacional" "Alianza Republicana Nacionalista" "Vamos" ...
## $ Numero_votos : num 2200332 101516 227357 91675 97660 ...
## $ Porcentaje_votos: chr " 70.57 %" " 3.26 %" " 7.29 %" " 2.94 %" ...
## $ Numero_escaños : num 54 2 2 1 1 0 0 0 0 0
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
tabla = data2 %>%
select(Partido, Numero_escaños) %>%
group_by(Partido)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.3.3
# Gráfico de Pastel
ggplot(tabla, aes(x = "", y = Numero_escaños, fill = Partido)) +
geom_bar(width = 1, stat = "identity") +
coord_polar(theta = "y") +
theme_void() +
labs(title = "Distribución de escaños por partido") +
theme(legend.title = element_blank()) +
geom_text_repel(aes(label = paste0(round(Numero_escaños / sum(Numero_escaños) * 100, 1), "%")),
position = position_stack(vjust = 0.5),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'grey50') +
scale_fill_brewer(palette = "Set3") # Usar una paleta de colores predefinida
## Warning: ggrepel: 9 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:rio':
##
## export
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
# Calcular los porcentajes por partido
tabla <- transform(tabla, Porcentaje = Numero_escaños / sum(Numero_escaños) * 100)
# Crear el gráfico de pastel en 3D
plot_ly(tabla, labels = ~Partido, values = ~Numero_escaños, type = "pie",
text = ~paste0(round(Porcentaje, 1), "%"),
textinfo = "text+label",
marker = list(colors = rainbow(length(unique(tabla$Partido))), line = list(color = 'grey', width = 1))) %>%
layout(title = "Distribución de escaños por partido",
scene = list(aspectmode = "cube"))
str(data$`Nayib_Bukele`)
## num [1:16] 84.9 81.4 75 81.3 81.3 ...
library(ggplot2)
library(dplyr)
library(scales)
## Warning: package 'scales' was built under R version 4.3.3
tabla <- tabla %>%
arrange(desc(Numero_escaños)) %>%
mutate(Partido = factor(Partido, levels = Partido))
ggplot(tabla, aes(x = Partido, y = Numero_escaños, fill = Partido)) +
geom_bar(stat = "identity") +
geom_text(aes(label = paste0(round(Porcentaje, 1), "%")), vjust = -0.1, color = "black", size = 3) +
ggtitle(" Porcentaje del número de escaños por partido político") +
xlab("Partido político") +
ylab("Número de escaños") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 44, hjust = 1), legend.position = "none") # Ajusta el ángulo y el espaciado de los nombres en el eje X
# Paleta de colores suaves
library(tayloRswift) #opcional (una ventaja de que R sea software libre)
ggplot(para_grafico, aes(x=p04, y=Porcentaje, fill=p04)) + geom_bar(stat = “identity”) + ggtitle(“Percepción de desigualdad económica”) + xlab(“¿Qué tan desigual cree que es el Perú económicamente”) + ylab(“Porcentaje”)+ geom_text(aes(label=round(Porcentaje,1)), vjust=1.30, color=“black”, size=3)+ theme(panel.background=element_rect(fill = “white”, colour = “white”)) + scale_fill_taylor(palette = “Red”) #fearless, speakNow, Red
para_grafico = data %>%
select(departamento, Nayib_Bukele) %>%
group_by(departamento)
library(ggplot2)
# Gráfico de Barras
ggplot(para_grafico, aes(x = departamento, y = Nayib_Bukele, fill = departamento)) +
geom_bar(stat = "identity") +
ggtitle("Porcentaje de votos por Nayib Bukele por departamentos") +
xlab("Departamentos") +
ylab("Porcentaje de votos por Nayib Bukele") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) + # Ajusta el ángulo y el espaciado de los nombres en el eje X
scale_fill_brewer(palette = "Set3") # Agrega colores usando una paleta predefinida
## Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Set3 is 12
## Returning the palette you asked for with that many colors
library(ggplot2)
library(taylor)
## Warning: package 'taylor' was built under R version 4.3.3
# Ordenar los datos de manera descendente según el porcentaje de votos por Nayib Bukele
para_grafico <- para_grafico[order(para_grafico$Nayib_Bukele, decreasing = TRUE),]
# Gráfico de Barras con valores numéricos en las barras
ggplot(para_grafico, aes(x = reorder(departamento, Nayib_Bukele), y = Nayib_Bukele, fill = departamento)) +
geom_bar(stat = "identity") +
geom_text(aes(label = paste0(round(Nayib_Bukele), "%")), vjust = -0.5, size = 3, color = "black") + # Mostrar los valores numéricos en las barras
ggtitle("Porcentaje de votos por Nayib Bukele por departamentos") +
xlab("Departamentos") +
ylab("Porcentaje de votos por Nayib Bukele") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Ajustar el ángulo y el espaciado de los nombres en el eje X