showing lines and points using different factors

39 views
Skip to first unread message

Adam

unread,
Mar 7, 2012, 2:50:02 PM3/7/12
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"))

Brandon Hurr

unread,
Mar 7, 2012, 3:30:47 PM3/7/12
to Adam, ggplot2
Your example wasn't really reproducible, but I patched it together...

age <- c(5,6,7,8,9,5,6,7,8,9,5,6,7,8,9)
tl <- 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")
data1 <- data.frame(cbind(age,tl,year,period))
data1
ggplot(data=data1, aes(x=age, y=tl)) +
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), group=period)) +
scale_linetype_manual(values=c(2,7), breaks=c("past","present"), labels=c("Past", "Present"))


I think your problem was the group= argument. 

B



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Mar 7, 2012, 4:43:15 PM3/7/12
to Adam, ggplot2
It's a good question. Usually to merge legends they have to have the same breaks and labels. 


ggplot(data=data1, aes(x=age, y=tl)) +
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(year), line=factor(year), group=year)) +

scale_linetype_manual(values=c(2,2,7),breaks=c("1940","1960","2010"), labels=c("1940", "1960","2010"))

The trouble is that when you do this you get individual lines for 1940 and 1960... Is that ok?

I'm not sure I know how to split it. When I tried group=period I got the following error:
Error in grid.Call.graphics(L_polygon, x$x, x$y, index) : 
  invalid line type

On this machine I'm a little behind on my updates so cum grano salis...
R version 2.14.0 (2011-10-31)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6     

loaded via a namespace (and not attached):
[1] digest_0.5.1

On Wed, Mar 7, 2012 at 21:32, Adam <adam...@gmail.com> wrote:
I really appreciate your help Brandon.  Is there any way to join the
two legends so that the line types are represented appropriately with
the symbols?

Adam
--
________________________________________________________
Adam C. Peer
PhD Candidate
University of Maryland
Center for Environmental Science
Chesapeake Biological Laboratory
One Williams St, P.O. Box 38
Solomons, MD 20688  USA
Tel. 410-326-7373
FAX  410-326-7318

Brandon Hurr

unread,
Mar 7, 2012, 5:48:22 PM3/7/12
to Adam, ggplot2, Dennis Murphy
Perhaps someone else might know a workaround?

On Wednesday, March 7, 2012, Adam <adam...@gmail.com> wrote:
> My hope was to have the lines fitted by period and have the symbols by
> year, but it looks like that's not possible...at least with a combined
> legend.  I really appreciate all of your help.
>
> Thanks a lot,
> Adam
Reply all
Reply to author
Forward
0 new messages