ggplot legend list is larger than page
Asked Answered
D

1

11

I have a plot in R which has a very large number of sample groups, and therefore the legend is larger than the page size and is cut off. I understand that this is not publication quality, but I need to know the colours to be able to make the legend in Illustrator.

Is there a way to make the page size much bigger or somehow change the legend format so that I can include all the keys? The reason for this is so that I can open the PDF in Illustrator and get the colours for each sample to create a new legend that will be for publication. I thought that maybe there is a clipping mask, and that the actual legend will be preserved, but when I opened in Illustrator, the legend was actually cut at the page ends1.

Here is one exmple where the legend goes beyond the graph and the page size and is therefore cut off.

As was suggested in the comments below I gave nrow a try which helped break the legends up but now the entire page is just legends.

ggplot(purine.n, aes(x=variable, y=value, colour=metabolite_gene, shape=variable))
+geom_abline(slope=0)
+geom_point(size=4, position=position_dodge(width=0.08))
+scale_y_continuous(limit=c(-3.5,5.5), breaks=c(-3,-2,-1,0,1,2,3,4,5))
+scale_shape_manual(values=c(16,17,17), guide=F)
+theme_bw()
+theme(legend.key=element_blank(), legend.key.size=unit(1,"point"))
+guides(colour=guide_legend(nrow=16))
Disaccustom answered 6/5, 2015 at 16:56 Comment(8)
Does using the nrow or ncol argument help? (imo (fwiw) having this many factors / colours is not going to produce a very informative plot / legend)Badoglio
Do you need to have a different color for each category? Unless the categories are ordinal, this could be very hard for someone to read effectively. You may want to figure out a different way of differentiating categories.Combine
Thanks, that led to this #25622420 but unfortunately it did not resolve the issue. I think what you pointed out is the right track tho! I've updated the question to reflect your suggestions.Disaccustom
@user1362215 I do need the colours as I use the colours to seperate out subcategories within the sample. Actually, that will be the next thing I need to figure out is how to get category A and it's subcategories one shade of colour, B and it's subcategories another, and so on so forth.Disaccustom
@Badoglio nrows worked but now I have a different problem. The entire page is just legends >.<Disaccustom
@kennethPhough, ggplot is kind of bad when it comes to non-intuitive color schemes. You can use scale_color_manual, aided with some color-handling functions, to work with them. hsv2col and col2hsv might be useful in this case.Combine
@user1362215 Ok, thanks for the advice! I'll definitely give that a try.Disaccustom
@Badoglio Yes, thank you! The rnow attribute worked!Disaccustom
D
16

As was suggested in the comments, nrow was the answer to my problem. I had to adjust the value to get the right number of rows to fit my legend. Below is the completed code that worked. There's more tweaking I need to do, like change page size to help make things look better, but that is out of the scope of this question.

ggplot(data.n, aes(x=variable, y=value, colour=metabolite_gene, shape=variable))
+geom_abline(slope=0)+geom_point(size=4, position=position_dodge(width=0.08))
+scale_y_continuous(limit=c(-3.5,5.5), breaks=c(-3,-2,-1,0,1,2,3,4,5))
+scale_shape_manual(values=c(16,17,17), guide=F)
+theme_bw()
+theme(legend.key=element_blank(), legend.key.size=unit(1,"point"))
+guides(colour=guide_legend(nrow=30))

fixed legend with nrow

Disaccustom answered 6/5, 2015 at 17:17 Comment(1)
Does this also work when you have guides for color and shape? I like to have a line for each (so instead of your 30, I would only need 2 - but cannot figure out how to do this.)Mighty

© 2022 - 2024 — McMap. All rights reserved.