Adjust legend

35 views
Skip to first unread message

thomas

unread,
Sep 21, 2010, 10:07:10 AM9/21/10
to ggplot2
Hi,

in the screenshot you can see the output of my code. All I want is a
legend with only 2 entries and a different title. Name of the two
entries should be different as well. and i have absolutely no idea how
to do it.

screenshot: http://bit.ly/aWP5Aa
r-code: http://pastebin.com/eRSvcfrj

Thanks in advance.
Thomas

thomas

unread,
Sep 21, 2010, 10:11:57 AM9/21/10
to ggplot2
and i forgot: I would like to reposition the legend because on the
right side it creates a massive whitespace.

Brandon Hurr

unread,
Sep 21, 2010, 10:20:34 AM9/21/10
to thomas, ggplot2
Thomas, 

Thanks for giving a reproducible example and a sample image. 

The following should give you an idea how to specify the title of the legend in this case as well as relabeling the colors themselves. 

ggplot(data,aes(fill=fcolor_two,y=f1,x=x)) + geom_bar(position="dodge", stat="identity") +  scale_x_continuous(labels=c("100k","200k","b100k"),breaks=c(1,2,3)) + scale_fill_discrete(name='Title', labels=c('color 1', 'color 2'), breaks=c("#00a37c","#2dab00"))

I am confused by what you mean by, " All I want is a legend with only 2 entries and a different title." You only have two colors and no more in the legend. 

As far as moving the legend... I'm not sure how to do that one. 

Brandon

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

thomas

unread,
Sep 21, 2010, 10:26:06 AM9/21/10
to ggplot2
Hi Brandon,

thanks for the reply. breaks set to the colors... no wonder I never
figured that one out ;)
As to the "All I want is a legend with only 2 entries", that I wrote
before I was able to get the number of entries down to two. Please
disregard that.

Is there actually a way to have the minimum of y set to 50, so the
bars start at 50 rather than 0?

Thomas
> > To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com >
> > More options:http://groups.google.com/group/ggplot2

ONKELINX, Thierry

unread,
Sep 21, 2010, 10:26:23 AM9/21/10
to thomas, ggplot2
Thomas,

Learn to use factors. That will make things much easier

library(ggplot2)
data <-
data.frame(Corpus=factor(c(1,2,1,2,1,2)),f1=c(70,75,80,84,90,95),x=facto
r(c(1,1,2,2,3,3), labels = c("100k", "200k", "b100k")))
ggplot(data,aes(fill=Corpus,y=f1,x=x)) +

geom_bar(position="dodge", stat="identity")

Use scale to tweak the content of the legend

ggplot(data,aes(fill=Corpus,y=f1,x=x)) +

geom_bar(position="dodge", stat="identity") +

scale_fill_manual("Other title", value = c("purple",
"white"), labels = c("A", "B"), breaks = 1:2)

More examples at the ggplot2 website

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

> -----Oorspronkelijk bericht-----
> Van: ggp...@googlegroups.com
> [mailto:ggp...@googlegroups.com] Namens thomas
> Verzonden: dinsdag 21 september 2010 16:07
> Aan: ggplot2
> Onderwerp: Adjust legend

> --
> You received this message because you are subscribed to the
> ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com

> More options: http://groups.google.com/group/ggplot2
>

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.

Luciano Selzer

unread,
Sep 21, 2010, 10:36:25 AM9/21/10
to thomas, ggplot2
Hello thomas, to move the legend aroud just add this to your plot. However, legends on top or bottom look ugly IMHO. 


p + opts(legend.position = "left")

HTH


Luciano


2010/9/21 thomas <thk....@gmail.com>

Brandon Hurr

unread,
Sep 21, 2010, 10:40:24 AM9/21/10
to thomas, ggplot2
Normally, you'd set the limits of the axis using the scale_*_** but it doesn't work because geom_bar() requires the y_min to be 0 (I think this is why it plots nothing). 

ggplot(data,aes(fill=fcolor_two,y=f1,x=x)) + geom_bar(position="dodge", stat="identity") +  scale_x_continuous(labels=c("100k","200k","b100k"),breaks=c(1,2,3)) + scale_fill_discrete(name='Title', labels=c('color 1', 'color 2'), breaks=c("#00a37c","#2dab00")) + scale_y_continuous(limits=c(50,100))

You'll have to "zoom" in using coord_cartesian(). 

ggplot(data,aes(fill=fcolor_two,y=f1,x=x)) + geom_bar(position="dodge", stat="identity") +  scale_x_continuous(labels=c("100k","200k","b100k"),breaks=c(1,2,3)) + scale_fill_discrete(name='Title', labels=c('color 1', 'color 2'), breaks=c("#00a37c","#2dab00")) + coord_cartesian(ylim = c(50,100))

Mucks with the y axis breaks a bit. 

Brandon


ONKELINX, Thierry

unread,
Sep 21, 2010, 10:42:42 AM9/21/10
to thomas, ggplot2
Bars not starting at 0 is a bad idea. Because people tend to compare the relative height of the bars. The relative height of the bars change if you let the bars start at 50 instead of 0. Hence the interpretation can change.

----------------------------------------------------------------------------


ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

> -----Oorspronkelijk bericht-----
> Van: ggp...@googlegroups.com
> [mailto:ggp...@googlegroups.com] Namens thomas

> Verzonden: dinsdag 21 september 2010 16:26
> Aan: ggplot2
> Onderwerp: Re: Adjust legend

> ggplot2+u...@googlegroups.com<ggplot2%2Bunsubscribe@googlegro
> > > ups.com > More options:http://groups.google.com/group/ggplot2


>
> --
> You received this message because you are subscribed to the
> ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com

thomas

unread,
Sep 21, 2010, 11:12:00 AM9/21/10
to ggplot2
hi luciano,

well, since i only have two legend entries, i would like to position
them horizontally instead of vertically. If i put it on top and
position it vertically it does look ugly.

do you know where i can find a list of all the options in opts() ?

Thomas

Brandon Hurr

unread,
Sep 21, 2010, 11:14:36 AM9/21/10
to thomas, ggplot2
As far as I know, if you want to position things horizontally you're looking at drawing your own legend using grobs. I'd love to be wrong on this one though. 

Brandon


Thomas

thomas

unread,
Sep 21, 2010, 11:35:44 AM9/21/10
to ggplot2
Thierry,

I will have a look at factors, don't know about them yet.
About the chart starting at 50 instead of 0: true but the relative
height between the three x points doesn't matter, only the one between
the two groups of each x-point. But the more I experiment with
ggplot2, the more I am looking into just publishing a table instead to
be honest.

Brandon,
bummer, would be mighty useful ;) I will try inkscape then, input/
output will be eps anyway.

Thomas

thomas

unread,
Sep 21, 2010, 11:40:58 AM9/21/10
to ggplot2
Almost overlooked that reply. Thanks. Will play around with it.

Thomas

Dennis Murphy

unread,
Sep 21, 2010, 6:13:41 PM9/21/10
to thomas, ggplot2
Hi:

Is this what you were after? Note: I modified the data to make Corpus and x factors, which has an impact on the choice of scale function in the plot.

data2 <- data.frame(Corpus = factor(rep(1:2, 3)),
                         x = factor(rep(1:3, each = 2)),
                        f1 = c(70,75,80,84,90,95))

q <- ggplot(data2, aes(x = x, y = f1, fill = Corpus))
q + geom_bar(position = 'dodge', stat = 'identity') +
    scale_fill_manual('Corpus', value = c('1' = '#00a37c', '2' = '#2dab00')) +
    coord_flip() + opts(legend.position = c(0.9, 0.15))

legend.position() uses coordinates relative to [0, 1] x [0, 1] rather than the native coordinates.

HTH,
Dennis

thomas

unread,
Sep 21, 2010, 6:26:53 PM9/21/10
to ggplot2
Hi Dennis,

yes, that's it, thank you.
interesting option you are using. Is there a way to get all possible
options as a list?

Best,
Thomas

Brandon Hurr

unread,
Sep 22, 2010, 4:14:49 AM9/22/10
to thomas, ggplot2
I'm sure one exists in Hadley or Baptiste's brains, but I've not seen
a complete list of +opts(). You could start one on the wiki and
everyone could contribute to it.

Brandon

> More options: http://groups.google.com/group/ggplot2
>

Brandon Hurr

unread,
Sep 22, 2010, 4:57:37 AM9/22/10
to thomas, ggplot2
http://www.slideshare.net/hadley/20-polishing

Slide 34 has a pretty good list of opts() that we could start with. 

Dennis Murphy

unread,
Sep 22, 2010, 5:12:19 AM9/22/10
to Brandon Hurr, ggplot2
Hi:

A list of opts() along with numerous examples of their use is in Chapter 8 of the ggplot2 book; in particular, Table 8.1 contains the most common ones. I know that legend.position and legend.justification are missing from that list in the book, but discussion about them has taken place in this forum in the past. Between the 19 in the book and the two mentioned above, that makes 21 known opts() of which I'm aware. I'm happy to hear about others...

Cheers,
Dennis

Brandon Hurr

unread,
Sep 22, 2010, 12:28:51 PM9/22/10
to Dennis Murphy, ggplot2
Well, I started one... apologies to Hadley for plagiarizing page 143 of the ggplot2 book. Not sure how else to present them to be honest. 

Luciano Selzer

unread,
Sep 22, 2010, 12:43:40 PM9/22/10
to Brandon Hurr, ggplot2
Very useful, I just added a couple of things.
Luciano


2010/9/22 Brandon Hurr <bhi...@gmail.com>

Brian Diggs

unread,
Sep 22, 2010, 4:57:40 PM9/22/10
to ggplot2
I thought I'd browse the source code to see if there were any more,
and it seems that theme_gray() is the canonical reference (theme_set
in theme.r gets its list of valid names from theme_gray()).
names(theme_gray()) gives the list of possible options. However, I
took it a step further to pull out what types of things they can
take. The following code summarizes them:

tg <- theme_gray()
op <- data.frame(opt = names(tg), class = laply(tg, class))
op[op$class=="theme","type"] <- laply(tg[op$class=="theme"], attr,
"type")
op
# opt class type
# 1 axis.line theme any
# 2 axis.text.x theme text
# 3 axis.text.y theme text
# 4 axis.ticks theme segment
# 5 axis.title.x theme text
# 6 axis.title.y theme text
# 7 axis.ticks.length unit <NA>
# 8 axis.ticks.margin unit <NA>
# 9 legend.background theme box
# 10 legend.key theme box
# 11 legend.key.size unit <NA>
# 12 legend.text theme text
# 13 legend.title theme text
# 14 legend.position character <NA>
# 15 panel.background theme box
# 16 panel.border theme any
# 17 panel.grid.major theme line
# 18 panel.grid.minor theme line
# 19 panel.margin unit <NA>
# 20 strip.background theme box
# 21 strip.text.x theme text
# 22 strip.text.y theme text
# 23 plot.background theme box
# 24 plot.title theme text
# 25 plot.margin unit <NA>

For the type (which is only defined for the themes), "box" is set with
theme_rect; "any" is a theme_blank() in theme_gray(), so you can fall
back to the printed documentation for what theme type it should be.

In particular, the additional ones not already listed on the wiki are:

axis.ticks.length (unit)
axis.ticks.margin (unit)
legend.key.size (unit)
panel.margin (unit)
plot.margin (unit)

As to further description of some of the options:

legend.position is either a numeric vector of length two or a
character value in "top", "bottom", "left", "right", or "none". If it
is a numeric vector of length 2, they are the x and y coordinates of
the legend (not sure in what units/scale) and and additional option,
legend.justification, is used as the justification of the viewport
(just arugment of viewport()). [Derived from reading plot-render.r]


On Sep 22, 9:43 am, Luciano Selzer <luciano.sel...@gmail.com> wrote:
> Very useful, I just added a couple of things.
> Luciano
>
> 2010/9/22 Brandon Hurr <bhiv...@gmail.com>
>
> > Well, I started one... apologies to Hadley for plagiarizing page 143 of the
> > ggplot2 book. Not sure how else to present them to be honest.
>
> >http://github.com/hadley/ggplot2/wiki/+opts()-List
>
> > On Wed, Sep 22, 2010 at 10:12, Dennis Murphy <djmu...@gmail.com> wrote:
>
> >> Hi:
>
> >> A list of opts() along with numerous examples of their use is in Chapter 8
> >> of the ggplot2 book; in particular, Table 8.1 contains the most common ones.
> >> I know that legend.position and legend.justification are missing from that
> >> list in the book, but discussion about them has taken place in this forum in
> >> the past. Between the 19 in the book and the two mentioned above, that makes
> >> 21 known opts() of which I'm aware. I'm happy to hear about others...
>
> >> Cheers,
> >> Dennis
>
> >> On Wed, Sep 22, 2010 at 1:57 AM, Brandon Hurr <bhiv...@gmail.com> wrote:
>
> >>>http://www.slideshare.net/hadley/20-polishing
>
> >>> Slide 34 has a pretty good list of opts() that we could start with.
>
> >>> On Wed, Sep 22, 2010 at 09:14, Brandon Hurr <bhiv...@gmail.com> wrote:
>
> >>>> I'm sure one exists in Hadley or Baptiste's brains, but I've not seen
> >>>> a complete list of +opts(). You could start one on the wiki and
> >>>> everyone could contribute to it.
>
> >>>> Brandon
>
> >>>> <ggplot2%2Bunsu...@googlegroups.com<ggplot2%252Buns...@googlegroups.com>>
Reply all
Reply to author
Forward
0 new messages