Skip to content

Instantly share code, notes, and snippets.

@pablobarbera
Created October 21, 2011 20:03
Show Gist options
  • Select an option

  • Save pablobarbera/1304808 to your computer and use it in GitHub Desktop.

Select an option

Save pablobarbera/1304808 to your computer and use it in GitHub Desktop.
Simulación Congreso de 300 escaños (gráfico)
## R-file: simulacion de distribucion de diputados en funcion del tamano
## del Congreso de Diputados
## Autor: Pablo Barbera (NYU)
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.pablobarbera.com/data/datos_grafico_300.csv", sep="\t")
p <- ggplot(data, aes(x=diputados, y=percent, group=partido))
pq <- p + geom_line(aes(color=partido)) + scale_colour_manual("Partido",
breaks=c("PSOE", "PP", "Otros"), values=c("darkgreen", "blue", "red")) +
geom_vline(aes(xintercept=350), linetype="dashed") +
scale_y_continuous("Porcentaje de escaños", formatter="percent") +
scale_x_continuous("Tamaño del Congreso, en escaños") +
annotate("text", x=303, y = .09, label = "7.3% (22)",
size=3) +
annotate("text", x=303, y = .42, label = "44.0% (132)",
size=3) +
annotate("text", x=303, y = .475, label = "48.7% (146)",
size=3) +
annotate("text", x=356, y = .093, label = "7.7% (27)",
size=3) +
annotate("text", x=357.5, y = .425, label = "44.0% (154)",
size=3) +
annotate("text", x=357.5, y = .467, label = "48.3% (169)",
size=3) +
annotate("text", x=399, y = .10, label = "8.8% (35)",
size=3) +
annotate("text", x=398, y = .42, label = "43.2% (173)",
size=3) +
annotate("text", x=398, y = .47, label = "48.0% (192)",
size=3) +
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="pablobarbera.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