>but the legend seems more tricky. Hopefully someone will come up with
>a solution.
This is probably my least favorite part of ggplot - that is, the lack of control over
the legend. USUALLY the program gets it right. But it's very hard to modify.
Peter
Peter L. Flom, PhD
Statistical Consultant
www DOT peterflomconsulting DOT com
It's my believe that it should almost always be possible to
programmatically determine the correct legend. If you have any
counter examples, I'd like to see them.
Unfortunately, this case is one I had not anticipated, so currently
can't see anyway to show the smooth on just one of the points on the
legend.
Hadley
library(ggplot2)
x <- rnorm(100)
group <- factor(rbinom(x, size=1, p=0.2), labels=c('control','patient'))
y <- 1.3*x + 3 + ifelse(group=='control', 0, -2) + rnorm(x, sd=1)
final <- data.frame(group,x,y)
# compute the regression for control subjects only
model <- lm(y~x, data=final, subset=group=='control')
# compute the predicted values for the confidence interval and the
# dotted line, using predict
pred.grid <- with(final, expand.grid(
x=seq(min(x), max(x), length=101),
group='control'))
pred <- predict(model, newdata=pred.grid,interval=('prediction'))
pred <- data.frame(pred.grid,pred)
# select only the lower values of a confidence interval (level=0.9),
# which corresponds to the lower 5%
pred$lwr.9 <- predict(model, newdata=pred,interval=('prediction'),level=0.9)[,2]
p <- ggplot(data = final, mapping = aes(x = x, y = y, fill=group)) +
geom_smooth(aes(y=fit, ymin=lwr, ymax=upr), data=pred, stat='identity') +
geom_line(aes(y=lwr.9), data=pred, linetype=2, colour="grey40")+
geom_point(aes(colour=group))+
scale_colour_manual(values=c("#377EB8","#E41A1C")) +
scale_fill_manual(values=c("#377EB8", NA))
GeomSmooth$draw_legend <- function (., data, params, ...)
{
data <- aesdefaults(data, .$default_aes(), list(...))
data$fill <- alpha(data$fill, data$alpha)
rectGrob(gp = gpar(col = NA, fill = data$fill))
}
p
# reload the default one
detach(package:ggplot2, unload=T)
library(ggplot2)
# p # usual behavior
>It's my believe that it should almost always be possible to
>programmatically determine the correct legend. If you have any
>counter examples, I'd like to see them.
>
OK, here's one I am having some trouble with .... excuse the length ....the idea
is to compare how Obama did and how Kerry did in different congressional districts,
using LM.
# FIRST, I SET UP THE DATA
library(foreign)
library(ggplot2)
origdata <- read.csv("c:/personal/dailykos/blackrepdem.csv")
state <- substr(origdata$district, 1, 2)
southern <- state == 'AL' | state == 'AR' | state == 'FL' | state == 'GA' | state == 'LA'|
state == 'MS' | state == 'NC' | state == 'SC'| state == 'TN' | state == 'VA'
party <- substr(origdata$Party,2,2)
testdata <- expand.grid(southern = c(TRUE, FALSE), Black = seq(0, 65, by = 10))
#NEXT, THE OBAMA EQUATION
ObamaRegionBlack <- with(origdata, ols(Obama~southern*Black))
Obamapredicted <- predict(ObamaRegionBlack, testdata)
Obamaprediction <- data.frame(cbind(testdata, Obamapredicted))
# AND A PLOT
plot2 <- qplot(data = Obamaprediction, Black, Obamapredicted, colour = southern,
main = "Obama vote predicted by region and pct Black.",
xlab = 'Percent Black',
ylab = 'Predicted Obama vote')
plot2 <- plot2 + geom_line() + aes(colour = southern) + scale_colour_manual(value = c("red", "blue"))
# SAME THING FOR KERRY
KerryRegionBlack <- with(origdata, ols(Kerry~southern*Black))
Kerrypredicted <- predict(KerryRegionBlack, testdata)
Kerryprediction <- data.frame(cbind(testdata, Kerrypredicted))
plot3 <- qplot(data = Kerryprediction, Black, Kerrypredicted, colour = southern,
main = "Kerry vote Kerrypredicted by region and pct Black.",
xlab = 'Percent Black',
ylab = 'Predicted Kerry vote')
plot3 <- plot3 + geom_line() + aes(colour = southern) + scale_colour_manual(value = c("red", "blue"))
# AND THOSE TWO ARE GOOD. JUST HOW I WOULD WANT THEM
# BUT TO COMBINE THEM?????? HERE'S WHAT I DID, BUT, WHILE IT'S OK IT ISN'T GREAT AT ALL
plot4 <- qplot(data = Kerryprediction, Black, Kerrypredicted, colour = southern,
main = "Kerry and Obama vote Kerrypredicted by region and pct Black.",
xlab = 'Percent Black',
ylab = 'Predicted Democratic vote',
ylim = c(30, 100))
plot4 <- plot4 + geom_line() + aes(colour = southern) + scale_colour_manual(value = c("red", "blue"))
plot4 <- plot4 + geom_abline(intercept = 49.42, slope = 0.682, colour = 'blue', linetype = 2)
plot4 <- plot4 + geom_abline(intercept = 49.42-13.88, slope = 0.682-.129, colour = 'red', linetype = 2)
####################
I can send the data sets, if need be.
TIA
Conceptually, you need to combine Obamaprediction and Kerryprediction
into a single data frame, adding a variable that differentiates
between the two, and using the same variable for the y axis. Then you
can do something like:
ggplot(both, aes(percent_black, prediction, colour = southern,
linetype = president)) +
geom_line()
(Assuming I'm guessing correctly what you want)
Hadley