Last active
March 26, 2024 23:14
-
-
Save danieltomasz/4c14f9b7ebfbb186bf42b37ee74fdf01 to your computer and use it in GitHub Desktop.
Plot - sondaże nt preferencji wyborczych na prezydenta Warszawy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(dplyr) | |
library(gridExtra) | |
library(grid) | |
# Create data frame | |
data <- data.frame( | |
Candidate = c( | |
"Rafał Trzaskowski", "Magdalena Biejat", "Tobiasz Bocheński", "Przemysław Wipler", | |
"Janusz Korwin-Mikke", "Romuald Starosielec", "Nie wiem", "Nie wziąłbym/wzięłabym \nudziału w wyborach" | |
), | |
Percentage = c(48, 12, 9, 5, 2, 1, 18, 4) | |
) | |
filling <- factor(data$Candidate %in% c("Nie wiem", "Nie wziąłbym/wzięłabym \nudziału w wyborach"), | |
labels = c("Osoby kandydujące", "Inne odpowiedzi") | |
) | |
) | |
data$Candidate <- factor(data$Candidate, levels = rev(unique(data$Candidate))) | |
# Plot | |
poll <- ggplot(data, aes(x = Candidate, y = Percentage, fill = filling)) + | |
geom_col(width = 0.5) + # Adjust bar width | |
geom_text(aes(label = paste0(Percentage, "%")), position = position_nudge(y = 0.3), size = 3.5, color = "black", hjust = 0) + | |
coord_flip() + | |
labs( | |
title = "Preferencje wyborcze - Prezydent Warszawy", | |
x = NULL, y = "Procent", | |
caption = "Źródlo: TVP3 Warszawa, Wizualizacja: @danieltomasz" | |
) + # Remove X axis label, clarify Y | |
theme_minimal() + | |
theme( | |
legend.position = "bottom", | |
plot.margin = margin(1, 1, 1, 1, "cm") | |
) + # Adjust plot margins | |
theme(plot.background = element_rect(fill = "white", colour = "white")) + | |
scale_fill_manual(values = c("Osoby kandydujące" = "steelblue", "Inne odpowiedzi" = "gray50")) + | |
guides(fill = guide_legend(title = "")) + #+ # Set legend title to empty | |
scale_y_continuous(expand = expansion(mult = c(0, .1))) # + # Add space for labels + | |
ggsave("poll_Warszawa.png", plot = poll, width = 8, height = 6, dpi = 300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment