Legend Background Width

379 views
Skip to first unread message

Lighton Phiri

unread,
Dec 14, 2012, 11:55:15 PM12/14/12
to ggplot2
Which theme element needs to be altered for me to change the width of
the legend background? Legend text is overlapping with the
background[1]. I am using this dataset [2]. I used this code [3] to
generate the plot.

[1] See attachement (ggplot2-legend-entries.png)
[2] See attachement (ggplot2-legend.dat)
[3] http://pastebin.com/KUfnvbAS

Lighton Phiri
http://lightonphiri.org
ggplot2-legend-entries.png
ggplot2-legend.dat

Ista Zahn

unread,
Dec 15, 2012, 11:43:15 AM12/15/12
to Lighton Phiri, ggplot2
The one named 'legend.key.width'.
> --
> 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

Lighton Phiri

unread,
Dec 15, 2012, 1:39:57 PM12/15/12
to Ista Zahn, ggplot2
I tried that out before and it changes the legend key width, not the
entire background width.

Look [1] at what happens when I modify my code as shown below.

theme(legend.key.width=unit(10, "cm"))

[1] See attachement (ggplot2-legend-entries2.png)

Lighton Phiri
http://lightonphiri.org


On 15 December 2012 18:43, Ista Zahn <ista...@gmail.com> wrote:
> legend.key.width
ggplot2-legend-entries2.png

Ista Zahn

unread,
Dec 15, 2012, 2:20:43 PM12/15/12
to Lighton Phiri, ggplot2
Well it does change the legend key width, including the background.
That said, I can't replicate your original problem. When I run your
code I get the attached tst.png. Here is my sessionInfo:

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] reshape2_1.2.2 ggplot2_0.9.3

loaded via a namespace (and not attached):
[1] colorspace_1.2-0 compiler_2.15.2 dichromat_1.2-4 digest_0.6.0
[5] gtable_0.1.2 labeling_0.1 MASS_7.3-22 munsell_0.4
[9] plyr_1.8 proto_0.3-9.2 RColorBrewer_1.0-5 scales_0.2.3
[13] stringr_0.6.2 tools_2.15.2
tst.png

Lighton Phiri

unread,
Dec 15, 2012, 2:49:04 PM12/15/12
to Ista Zahn, ggplot2
I think I forgot to include an important detail in my original post...
see legend only gets messed up when I generate my plots with tikZ via
Sweave. The only work around for now is manually tell ggplot2 to
explicitly assign a desired width for background.

I have had this issue when generating plots with tikZ and what I do
with R base graphics is manually set legend width using 'text.width'.
Please see example code below. What I have been looking for is the
ggplot2 equivalent of base graphics 'text.with' legend attribute

dataset <- c(20, 10)
barplot(dataset, las=2, col=rainbow(2))
legend("topright", c("Male", "Female"), text.width=1)

Lighton Phiri
http://lightonphiri.org

Ista Zahn

unread,
Dec 15, 2012, 4:47:06 PM12/15/12
to Lighton Phiri, ggplot2
Ah, I see. Is tikZ still working? I gave up on it since it appears to
be unmaintained for some time now.

Anyway, sorry for my incorrect original suggestion. You're right that
legend.key.width controls the actual legend key, not the legend
itself. I don't see a way to change the size of the whole legend using
theme elements, but you can force it using something like

fill=paste(Concept, " ")

Hope that helps,
Ista

Lighton Phiri

unread,
Dec 15, 2012, 5:49:32 PM12/15/12
to Ista Zahn, ggplot2
>Hope that helps,
It sure did, thank you very much Ista; forgot all about good old
'paste' command. I had to make a minor change though... seeing that
tikZ converts R code into equivalent LaTeX command, and that spaces
are treated uniformly in LaTeX, I used math mode spacing instead
--primitive, but it works.

paste(Concept, "$\\;\\;\\;\\;$")

\; === large space

> Is tikZ still working?
Yes it is still working; except for a few minor issues like the one
you helped solve.

Thanks again!

Lighton Phiri
http://lightonphiri.org
Reply all
Reply to author
Forward
0 new messages