Created
April 2, 2014 18:41
-
-
Save sotelo/9940408 to your computer and use it in GitHub Desktop.
Código para graficar un scatter de cualesquiera dos variables de una tabla en formato long. Incluye código para graficar medianas.
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
scatter.expectativas <- function(Variables, nombres = '', xmin = FALSE, obs = NA, reg = F) | |
{ | |
Datos <-read.csv('Encuestas.csv') | |
Datos <- subset(Datos, Datos$Horizonte %in% Variables, drop = TRUE) | |
Datos$FechaEnc <- as.Date(sapply(Datos$FechaEncuesta, as.fecha)) | |
if(xmin) Datos <- subset(Datos, Datos$FechaEnc >= xmin, drop = TRUE) | |
if(!is.na(obs)) Datos <- subset(Datos, Datos$FechaEnc %in% obs, drop = TRUE) | |
if(nombres == '') nombres = Variables | |
Datos$Horizonte = factor(Datos$Horizonte, levels = Variables) | |
levels(Datos$Horizonte) = c("varX", "varY") | |
datos = dcast(Datos, Analista + FechaEnc ~ Horizonte, value.var = "Pronostico") | |
datos = datos[complete.cases(datos),] | |
datos$FechaEnc = factor(datos$FechaEnc) | |
labels = format(as.Date(levels(datos$FechaEnc)), "%b-%y") | |
resumen = ddply(datos, .(FechaEnc), here(summarize), | |
varX = median(varX, na.rm = T), | |
varY = median(varY, na.rm = T)) | |
P <- ggplot() | |
P <- P + geom_point(data = resumen, aes(x= varX, y = varY, color = FechaEnc), size = 12, alpha = .3) | |
P <- P + geom_point(data = datos, aes(x= varX, y = varY, color = FechaEnc), size = 4, position = "jitter") | |
if(length(reg) == 2) | |
{ | |
position = data.frame(x=reg[1], y = reg[2]) | |
P <- P + geom_smooth(data = datos, aes( x= varX, y = varY), method = "lm", se = F, color = "black") | |
P <- P + geom_text(aes(x = x, y = y, label = lm_eqn(lm( as.formula(paste("varY", "varX", sep = "~")), datos))), parse = TRUE, data = position) | |
} | |
P <- P + theme_bw() | |
P <- P + labs(color = "Fecha de la Encuesta") | |
P <- P + scale_color_hue(l=50, labels = labels) | |
if(length(nombres) == 2) | |
{ | |
P <- P + xlab(nombres[1]) | |
P <- P + ylab(nombres[2]) | |
} | |
return(P) | |
} | |
# Helper Function | |
lm_eqn = function(m, y = "y", x = "x") | |
{ | |
l <- list(a = format(coef(m)[1], digits = 2), | |
b = format(abs(coef(m)[2]), digits = 2), | |
r2 = format(summary(m)$r.squared, digits = 2), | |
x = x, | |
y = y); | |
if (coef(m)[2] >= 0) { | |
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,l) | |
} else { | |
eq <- substitute(italic(y) == a - b %.% italic(x)*","~~italic(r)^2~"="~r2,l) | |
} | |
as.character(as.expression(eq)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment