Adam
unread,Mar 7, 2012, 2:50:02 PM3/7/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ggplot2
I am trying to produce a figure showing the fitted relationship
between two variables (a, b). I have multiple years of data. I have
broken these data down into two periods that I call "past" and
"present". I would like to use stat_smooth to show the two fitted
lines for the two periods, however, I would like to show the data
points by year. Is it possible to produce this type of figure so that
the legend shows the appropriate line type with the respective year
symbol? Below I have some hypothetical data and my attempt to achieve
my objectives. As you can see, I get stuck at stat_smooth. Any help
is greatly appreciated.
a <- c(5,6,7,8,9,5,6,7,8,9,5,6,7,8,9)
b <- c(500,600,700,800,900,501,599,701,799,901,400,500,600,700,800)
year <-
c("1940","1940","1940","1940","1940","1960","1960","1960","1960","1960","2010","2010","2010","2010","2010")
period <-
c("past","past","past","past","past","past","past","past","past","past","present","present","present","present","present")
data <- data.frame(cbind(age,tl,year,period))
data
plot <- ggplot(data=data, aes(x=a, y=b))
plot + geom_point(aes(color=factor(year), shape=factor(year)),
size=5) +
scale_colour_manual(values=c("black","black","black"),
breaks=c("1940","1960","2010"), labels=c("1940","1960","2010")) +
scale_shape_manual(values=c(1,2,16),
breaks=c("1940","1960","2010"), labels=c("1940", "1960","2010")) +
stat_smooth(method='lm', aes(color=factor(period),
line=factor(period))) +
scale_linetype_manual(values=c(2,7), breaks=c("past","present"),
labels=c("Past", "Present"))