Combining legend.position = "bottom" and guides(col = guide_legend(nrow = N))

3,731 views
Skip to first unread message

Lighton Phiri

unread,
Feb 4, 2013, 5:45:54 AM2/4/13
to ggplot2
I have a plot whose legend I want to appear at the bottom and in two
rows, however, guide_legend(nrow=2) appears not to be working. What am
I doing wrong in my code?

Incidentally, I came across this post [1] but no solution was provided.

### Code ###

require(ggplot2)
require(reshape2)
require(grid)
require(scales)
library(gridExtra)

p3dataset <- read.csv(text="Aspect,Feature,Scale,Count
X,A,Legend #1,4
X,A,Legend #2,12
X,A,Legend #3,7
X,A,Legend #4,3
X,A,Legend #5,0
Y,A,Legend #1,2
Y,A,Legend #2,16
Y,A,Legend #3,3
Y,A,Legend #4,5
Y,A,Legend #5,0
X,B,Legend #1,2
X,B,Legend #2,10
X,B,Legend #3,8
X,B,Legend #4,6
X,B,Legend #5,0
Y,B,Legend #1,5
Y,B,Legend #2,11
Y,B,Legend #3,7
Y,B,Legend #4,3
Y,B,Legend #5,0
"
)


# p3 --plot #3
# S& U
##p3dataset <- read.table("ggplot2-legend-rows.dat", header=TRUE, sep=",")
p3experimentdataset <- p3dataset


ggplot(p3experimentdataset, aes(x=Feature, y=Count, fill=Scale,
width=0.75)) + geom_bar(stat="identity") + labs(x="", y="") +
facet_wrap(~ Aspect,ncol=1) + theme(legend.title=element_blank(),
legend.position="bottom", axis.text.x=element_blank(),
axis.ticks.x=element_blank(), axis.ticks.y=element_blank()) +
scale_fill_brewer(palette="Dark2") + ggtitle("The Plot in Question") +
coord_flip() + guides(col = guide_legend(nrow = 2))


[1] https://groups.google.com/d/topic/ggplot2/lHVelyk2DCg/discussion

Lighton Phiri
http://lightonphiri.org

Dennis Murphy

unread,
Feb 4, 2013, 5:57:33 AM2/4/13
to Lighton Phiri, ggplot2
Hi:

In the guides() call, change col to fill. Your aesthetic is fill, not
color. (A common mistake, BTW :)

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Lighton Phiri

unread,
Feb 4, 2013, 6:08:12 AM2/4/13
to Dennis Murphy, ggplot2
Aha! Thank you for this. Got mixed up with one of the examples here
[1]; does that mean the syntax is different for qplot or am I missing
something here?

:
:
# multiple row/col legends
p <- qplot(1:20, 1:20, colour = letters[1:20]) p + guides(col =
guide_legend(nrow = 8))
:
:

[1] http://docs.ggplot2.org/0.9.3/guide_legend.html
Lighton Phiri
http://lightonphiri.org
Reply all
Reply to author
Forward
0 new messages