Skip to content

Instantly share code, notes, and snippets.

@becarioenmoncloa
Created September 15, 2011 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save becarioenmoncloa/1220249 to your computer and use it in GitHub Desktop.
Save becarioenmoncloa/1220249 to your computer and use it in GitHub Desktop.
Simulacion Elecciones 2011 (grafico)
## R-file: simulacion de resultados de las elecciones de 2011
## Autor: becarioenmoncloa.com
library(ggplot2)
theme_L_border <- function(colour = "black", size = 1, linetype = 1) {
# From Rudolf Cardinal, March 2011, http://www.psychol.cam.ac.uk/statistics/R/
# use with e.g.: ggplot(...) + opts( panel.border=theme_L_border() ) + ...
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
polylineGrob(
x=c(x+width, x, x), y=c(y,y,y+height), ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
data <- read.csv("http://www.becarioenmoncloa.com/datos_grafico.csv", sep="\t")
data$supuesto <- as.factor(data$supuesto)
p <- ggplot(data, aes(x=diferencia, y=dhondt, group=group))
pq <- p + geom_step(aes(color=partido, linetype=supuesto)) +
scale_colour_manual("Partido", breaks=c("PSOE", "PP", "Otros"),
values=c("darkgreen", "blue", "red")) +
scale_linetype_discrete(name = "Votos del \n PSOE a...", breaks=c(1,2,3),
labels=c("PP", "Abstención", "PP, Abst. \n y Otros")) +
geom_hline(aes(yintercept=175), linetype="dashed") +
annotate("text", x=0.178, y = 170, label = "mayoría absoluta",
colour="grey30", size=2.5) +
geom_hline(aes(yintercept=125), size=0.2) +
annotate("text", x=-.0295, y = 131, label = "PSOE, 2000",
colour="grey50", size=2.5) +
geom_hline(aes(yintercept=183), size=0.2) +
annotate("text", x=-.032, y = 189, label = "PP, 2000",
colour="grey50", size=2.5) +
scale_y_continuous("Numero de escaños", limits=c(0,225)) +
scale_x_continuous("Diferencia entre PP y PSOE", formatter="percent") +
opts(panel.border=theme_L_border(),
axis.title.x=theme_text(size=10),
axis.title.y=theme_text(size=10, angle=90),
legend.key.height = unit(0.9, "cm"),
plot.background=theme_rect(fill="#fbfbfb", colour=NA))
svg("grafico1.svg", width=7, height=4)
pq
grid.text(label="becarioenmoncloa.com", x=0.85, y=0.05, gp=gpar(fontsize=8, alpha=0.5, fontfamily="Helvetica", fontface="bold"))
dev.off()
ggsave(pq, file="grafico1.png", width=7, height=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment