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
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/
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
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>:
Best Regards,
Xie Chao