Separate legend for colour of two geoms

540 views
Skip to first unread message

Xie Chao

unread,
Mar 11, 2010, 5:27:48 AM3/11/10
to ggplot2
Hi,

I have two geoms in the same plot (point and segment). They are both
coloured, but based on different variables. My problem is that the
legend for colour is grouped together. Is there any easy way to get
separate legend for the two geoms? Thanks a lot.

#############################
pc <- prcomp(iris[,1:4], scale=T)
rotation <- data.frame(pc$rotation)
df <- data.frame(predict(pc))
df$species <- iris$Species

q <- qplot(PC1, PC2, data=df, colour=species) + coord_equal()
q + geom_segment(aes(0, 0, xend=PC1, yend=PC2,
colour=rownames(rotation)), rotation,
arrow=arrow(length=unit(0.2, 'cm')))
##############################

Best Regards,
Xie Chao

hadley wickham

unread,
Mar 12, 2010, 8:50:10 AM3/12/10
to Xie Chao, ggplot2
Hi Xie,

Sorry, that's not currently possible. Each scale only produces a
single legend.

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Nicholas Nagle

unread,
Mar 12, 2010, 9:06:36 AM3/12/10
to hadley wickham, Xie Chao, ggplot2
As a complicated hack, would it be possible to do something like

q1 <- qplot(PC1, PC2, data=df, colour=species) + coord_equal()
q1.legend <- q1 + opts(keep= "legend_box")
q1 <- q1 + opts(legend.position='none')

q2 <- ggplot()+ geom_segment(aes(0, 0, xend=PC1, yend=PC2,

colour=rownames(rotation)), rotation, arrow=arrow(length=unit(0.2,
'cm')))

q2.legend <- q2 + opts(keep= "legend_box")
q2 <- q2 + opts(legend.position='none')

q <- q1+q2 # Not sure about this step, but the idea is to get both
plots on the same page without any legends.

and then use grid with three viewports, one for q, another for
q1.legend and the last for q2.legend?

Nicholas

takahashi kohske

unread,
Mar 13, 2010, 3:41:34 AM3/13/10
to Nicholas Nagle, hadley wickham, Xie Chao, ggplot2
My workaround:

pc <- prcomp(iris[,1:4], scale=T)
rotation <- data.frame(pc$rotation)
df <- data.frame(predict(pc))
df$species <- iris$Species

# "main" layout without geom
q <- ggplot(df, aes(PC1, PC2))+opts(legend.position="none")+coord_equal()

# point geom whose color is specified by 'species'
q.g1<-geom_point(aes(colour=species))

# segment geom whose color is specified by 'rotation'
q.g2<-geom_segment(aes(0, 0, xend=PC1, yend=PC2,


colour=rownames(rotation)), rotation, arrow=arrow(length=unit(0.2,
'cm')))

grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2,
width=unit(c(3,1),c("null","null")))))

# "main" plot.
print(q+q.g1+q.g2,vp=viewport(layout.pos.col=1, layout.pos.row=1:2))

# legend for point geom
print(q+q.g1+opts(keep="legend_box",
legend.position=c(0.5,0.5)),vp=viewport(layout.pos.col=2,
layout.pos.row=1))
# legend for segment geom
print(q+q.g2+opts(keep="legend_box",
legend.position=c(0.5,0.5)),vp=viewport(layout.pos.col=2,
layout.pos.row=2))

HTH.

2010/3/12 Nicholas Nagle <nna...@utk.edu>:

Xie Chao

unread,
Mar 13, 2010, 10:06:34 AM3/13/10
to takahashi kohske, Nicholas Nagle, hadley wickham, ggplot2
Thanks all for the help. Especially the opts(keep="legend_box").

Best Regards,
Xie Chao

Stéphane Laurent

unread,
Oct 14, 2013, 2:47:18 PM10/14/13
to ggp...@googlegroups.com, Xie Chao
Wouldn't it be possible to create a new scale like "linecolor", which would allow to simultaneously have a color-based legend for lines and another color-based legend (for points for example) ? 
Reply all
Reply to author
Forward
0 new messages