Hi:
One way to do it is to create and save the plot, extract out the loess
info, do a few simple modifications, concatenate the loess data with
the original data, make a couple of adjustments and do the plot...it's
a little easier than it sounds. The game is to hook in the loess data
with the original. Here's my approach:
DF <- data.frame(group = factor(rep(1:3, each = 100)),
weight = c(rnorm(100, 20, 10), rnorm(100, 30, 15),
rnorm(100, 15, 10)),
height= c(rnorm(100, 20, 6), rnorm(100, 40, 8), rnorm(100, 60, 5)))
# Initial plot
# The linetypes aren't really necessary, but they do no harm.
p <- ggplot(DF, aes(x = weight, y=height)) +
geom_line(aes(linetype = group)) +
geom_smooth(data = subset(DF, group==1),
aes(linetype=group),
method='loess', se = TRUE)
## do str(ggplot_build(p)) to see what it returns; the second component of
## $data returns info regarding the loess smooth
d1 <- ggplot_build(p)$data[[2]]
# Keep the height, weight, ymin and ymax variables
d1 <- d1[, 2:5]
# Rename x and y to weight and height
names(d1)[1:2] <- c('weight', 'height')
# Create a group variable with a single value
d1 <- transform(d1, group = 'Smoothed\npredictions')
# Add ymin and ymax variables to DF with NA values
DF <- transform(DF, ymin = NA, ymax = NA)
# Now concatenate DF and d1
DF2 <- rbind(DF, d1)
# Redefine group in the new data frame and set its levels
DF2$group <- factor(DF2$group, levels = c('1',
'Smoothed\npredictions', '2', '3'))
# Revised plot:
ggplot(DF2, aes(x = weight, y = height, linetype = group)) +
geom_line(aes(color = group, size = group)) +
geom_ribbon(aes(ymin = ymin, ymax = ymax, fill = group), alpha = 0.2) +
scale_linetype_manual('Legend', breaks = levels(DF2$group),
values = c('solid', 'solid', 'dashed', 'longdash')) +
scale_colour_manual('Legend', breaks = levels(DF2$group),
values = c('black', 'red', 'black', 'black')) +
scale_size_manual('Legend', breaks = levels(DF2$group),
values = c(0.5, 1, 0.5, 0.5)) +
scale_fill_manual('Legend', breaks = levels(DF2$group),
values = c(NA, 'red', NA, NA))
I used \n in the 'Smoothed predictions' label so that the legend takes
up less horizontal space. If you don't want that, remove the \n in the
two places where it appears.
You could do the same thing with a little more work by fitting a loess
model to the data first and then creating a data frame of predictions,
but ggplot_build() does this more quickly and efficiently so that's
why I did it this way.
HTH,
Dennis
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
>
https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email
ggp...@googlegroups.com
> To unsubscribe: email
ggplot2+u...@googlegroups.com
> More options:
http://groups.google.com/group/ggplot2