different legends

55 views
Skip to first unread message

Daniele

unread,
Oct 6, 2010, 12:31:29 PM10/6/10
to ggplot2
Hello, everyone!
I have a question about the legend box in ggplot2...
Can I have only one legend box with labels in italic? Because I have
two boxes..
And how can I eliminate the NA values from the plots?
Thanks, I hope you can help me. I started with the ggplot have few
days...


Daniele Coelho Vigário
Acadêmica de Oceanografia
CENTRO DE ESTUDOS DO MAR - UNIVERSIDADE FEDERAL DO PARANÁ
Av. Beira Mar s/nº, Pontal do Sul, Caixa-Postal: 50002, CEP: 83255-000
Pontal do Paraná, PR - Brasil
Tel: +554133118613 - Laboratório de Ornitologia
Cel: +554199116215
email: daniele...@gmail.com

Brandon Hurr

unread,
Oct 6, 2010, 1:24:35 PM10/6/10
to Daniele, ggplot2
Daniele, 

Can you supply us with the code you are using to make your plot and some sample data? 

Thanks, 

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

Brandon Hurr

unread,
Oct 6, 2010, 1:31:45 PM10/6/10
to Daniele, ggplot2
If you want all of your legend labels in italics you can use legend.text in the options. 


/shameless self-promotion

#usage
plot + opts(legend.text = theme_text(face = 'italic'))

It's really hard to make out what you mean by two legends though. Usually if there are two legends it's because you have defined two separate scales (color, shape, size, linetype...). You can make them disappear, but I'm not sure if you can just combine legends. 

Brandon

Ista Zahn

unread,
Oct 6, 2010, 1:00:30 PM10/6/10
to Daniele, ggplot2
Hi Daniele,
Please provide a reproducible example.

Thanks,
Ista

> --
> 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
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Daniele

unread,
Oct 6, 2010, 2:34:24 PM10/6/10
to ggplot2
hi,Brandon..
here's my sample data. Can I separate the legend boxes, I only want
the "espécie" box to be in italic..
And for some barplots, how can I just not show the NA values?
Thanks for everyone who's helping me :)




espécie<-as.factor(c(rep("A. australis",49),rep("A. tropicalis",32)))
sexo<-
as.factor(c("F","M","ind","ind","M","M","M","M","ind","M","ind","F","M","ind","ind","ind","M","F","M","ind","ind","ind","ind","ind","ind","ind","M","ind","F","M","M","M","M","ind","ind","M","M","M","M","M","M","M","M","M","M","M","M","ind","M","M","ind","M","F","M","M","ind","ind","M","M","M","M","M","ind","F","M","M","ind","ind","M","ind","ind","M","ind","M","M","M","M","M","M","F","M"))
ct<-c(95.3,89.5,NA,NA,92,NA,NA,90,NA,NA,NA,NA,87,94,91,NA,
87,NA,NA,NA,NA,NA,66,NA,84,97,NA,NA,NA,NA,
88,84,83,84,95,96.5,91,87,86,91,93,92,93,170,NA,95,96,NA,87,174.1,NA,
122.6,145,162,176,113.4,NA,156,160.1,NA,148,161.6,NA,NA,
147,NA,NA,NA,NA,NA,NA,NA,150,NA,75,99,160,88,158,120,144)
ccb<-c(165.89,164.73,196.24,NA,172.35,182.8,214.9,164.74,NA,NA,NA,NA,
164.09,NA,NA,NA,
162.17,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
158.67,161.03,166.38,164.55,166.3,170.18,NA,NA,NA,NA,175.26,NA,
170.81,NA,
163.17,223.95,148.6,186.53,188.4,217.05,220.56,182.99,221.78,224.11,221.69,168.02,210.05,222.92,NA,
210.18,214.25,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,172.66,NA,
155,220.29,NA,NA)
lr<-c(32.34,33.48,33,NA,32.04,33.1,42.18,33.31,NA,NA,NA,NA,
30.82,NA,NA,NA,
30.34,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
26.44,31.94,34.05,30.28,32.12,31.39,NA,NA,NA,NA,32.09,32.34,32.66,NA,
33.79,47.69,27.43,34.98,30.77,42.41,45.45,30.41,46.4,46.1,46.1,30.94,43.47,41.3,NA,
44.38,43.29,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,33.01,NA,30.87,46.08,NA,NA)

tab<-data.frame(espécie,sexo,ct,lr,ccb)
attach(tab)
p1<-ggplot(tab,aes(ct,ccb,colour=espécie,shape=sexo))
+geom_point(size=2.5)+scale_shape("Sexo")+labs(x="Comprimento padrão
(cm)",y="Comprimento côndilo-basal (mm)")+opts(legend.position="none")
p2<-ggplot(tab,aes(ccb,lr,colour=espécie,shape=sexo))
+geom_point(size=2.5)+scale_shape("Sexo")+labs(x="Comprimento côndilo-
basal (mm)",y="Largura rostral (mm)")+opts(legend.position="none")
legend<-ggplot(tab,aes(ccb,lr,colour=espécie,shape=sexo))
+geom_point(size=2.5)+scale_shape("Sexo")+labs(x="Comprimento côndilo-
basal (mm)",y="Largura rostral (mm)")+opts(keep="legend_box")
+opts(legend.text=theme_text(face ="italic"))
arrange(p1,p2,legend,ncol=3)





On 6 out, 14:31, Brandon Hurr <bhiv...@gmail.com> wrote:
> If you want all of your legend labels in italics you can use legend.text in
> the options.
>
> http://github.com/hadley/ggplot2/wiki/Legend-Attributes
>
> /shameless self-promotion
>
> #usage
> plot + opts(legend.text = theme_text(face = 'italic'))
>
> It's really hard to make out what you mean by two legends though. Usually if
> there are two legends it's because you have defined two separate scales
> (color, shape, size, linetype...). You can make them disappear, but I'm not
> sure if you can just combine legends.
>
> Brandon
>
>
>
> On Wed, Oct 6, 2010 at 18:24, Brandon Hurr <bhiv...@gmail.com> wrote:
> > Daniele,
>
> > Can you supply us with the code you are using to make your plot and some
> > sample data?
>
> > Thanks,
>
> > Brandon
>
> > On Wed, Oct 6, 2010 at 17:31, Daniele <danieleviga...@gmail.com> wrote:
>
> >> Hello, everyone!
> >> I have a question about the legend box in ggplot2...
> >> Can I have only one legend box with labels in italic? Because I have
> >> two boxes..
> >> And how can I eliminate the NA values from the plots?
> >> Thanks, I hope you can help me. I started with the ggplot have few
> >> days...
>
> >> Daniele Coelho Vigário
> >> Acadêmica de Oceanografia
> >> CENTRO DE ESTUDOS DO MAR - UNIVERSIDADE FEDERAL DO PARANÁ
> >> Av. Beira Mar s/nº, Pontal do Sul, Caixa-Postal: 50002, CEP: 83255-000
> >> Pontal do Paraná, PR - Brasil
> >> Tel: +554133118613 - Laboratório de Ornitologia
> >> Cel: +554199116215
> >> email: danieleviga...@gmail.com
>
> >> --
> >> 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<ggplot2%2Bunsu...@googlegroups.com >
> >> More options:http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Oct 6, 2010, 3:26:02 PM10/6/10
to Daniele, ggplot2
Daniele, 

With multiple legends things become more complicated when you want to operate on just one (as far as I can tell anyway). Also, I notice that you are trying to arrange 3 graphs. 

I think arrange() has been repurposed for use in (plyr?). I believe the call you're looking for is grid.arrange() and is part of the gridExtra package. 

Sadly, these two things are beyond my current skill level. Hopefully, Ista or someone has some ideas. 

Brandon

Dennis Murphy

unread,
Oct 6, 2010, 4:13:00 PM10/6/10
to Daniele, ggplot2
Hi:

Is this perhaps (close to) what you wanted?

p1<-ggplot(tab,aes(ct,ccb,colour=espécie,shape=sexo))
q1 <- p1 + geom_point(size=2.5) + scale_shape("Sexo") +
     labs(x="Comprimento padrão (cm)", y="Comprimento côndilo-basal (mm)") +
     opts(legend.position="none")
p3 <- ggplot(tab, aes(ccb, lr, colour=espécie, shape=sexo))
q2 <- p3 + geom_point(size=2.5) + scale_shape("Sexo") +
     labs(x="Comprimento côndilo-basal (mm)",y="Largura rostral (mm)") +
     opts(legend.text=theme_text(face ="italic"))

grid.newpage()
vp1 <- viewport(width = 0.4, height = 1, x = 0.2, y = 0.5)
vp2 <- viewport(width = 0.6, height = 1, x = 0.7, y = 0.5)
print(q1, vp = vp1)
print(q2, vp = vp2)

The page rendering uses the grid package; the code is a modification of the examples in section 8.4 of Hadley's book. The warnings will tell you that the observations with missing values are deleted from the plot.

On Wed, Oct 6, 2010 at 11:34 AM, Daniele <daniele...@gmail.com> wrote:
hi,Brandon..
here's my sample data. Can I separate the legend boxes, I only want
the "espécie" box to be in italic..

It's not obvious to me how one would do it since the font family cannot be set within scale_*. (But I'd be interested to see how it's done if they indeed can have separate fonts :) You could try to separate the two legends - the shapes with one plot and the species with the other, but I had some problems getting that to work and don't have the time to play with it. If you do that, however, you're basically stealing space from the plots to devote to the legends and you have to ask yourself if it's worth the trouble.

HTH,
Dennis
 

Ista Zahn

unread,
Oct 6, 2010, 4:33:44 PM10/6/10
to Dennis Murphy, Daniele, ggplot2
Hi all,
Regarding italic legend lables: you should be able to use expressions
in legend labels, except there is a bug in the way. See

http://groups.google.com/group/ggplot2/browse_thread/thread/fe4ed06dd5cb3dac/c8d6d17fcfe8f70c?lnk=raot&pli=1

and

http://github.com/hadley/ggplot2/issues/issue/124

-Ista

--

Luciano Selzer

unread,
Oct 6, 2010, 4:50:52 PM10/6/10
to Ista Zahn, Dennis Murphy, Daniele, ggplot2

Daniele

unread,
Oct 6, 2010, 4:38:54 PM10/6/10
to ggplot2
Maybe you're right, I'm only wasting my time with this stuff hahaha
thank you!



On 6 out, 17:13, Dennis Murphy <djmu...@gmail.com> wrote:
> Hi:
>
> Is this perhaps (close to) what you wanted?
>
> p1<-ggplot(tab,aes(ct,ccb,colour=espécie,shape=sexo))
> q1 <- p1 + geom_point(size=2.5) + scale_shape("Sexo") +
>      labs(x="Comprimento padrão (cm)", y="Comprimento côndilo-basal (mm)") +
>
>      opts(legend.position="none")
> p3 <- ggplot(tab, aes(ccb, lr, colour=espécie, shape=sexo))
> q2 <- p3 + geom_point(size=2.5) + scale_shape("Sexo") +
>      labs(x="Comprimento côndilo-basal (mm)",y="Largura rostral (mm)") +
>      opts(legend.text=theme_text(face ="italic"))
>
> grid.newpage()
> vp1 <- viewport(width = 0.4, height = 1, x = 0.2, y = 0.5)
> vp2 <- viewport(width = 0.6, height = 1, x = 0.7, y = 0.5)
> print(q1, vp = vp1)
> print(q2, vp = vp2)
>
> The page rendering uses the grid package; the code is a modification of the
> examples in section 8.4 of Hadley's book. The warnings will tell you that
> the observations with missing values are deleted from the plot.
>
> On Wed, Oct 6, 2010 at 11:34 AM, Daniele <danieleviga...@gmail.com> wrote:
> > hi,Brandon..
> > here's my sample data. Can I separate the legend boxes, I only want
> > the "espécie" box to be in italic..
>
> It's not obvious to me how one would do it since the font family cannot be
> set within scale_*. (But I'd be interested to see how it's done if they
> indeed can have separate fonts :) You could try to separate the two legends
> - the shapes with one plot and the species with the other, but I had some
> problems getting that to work and don't have the time to play with it. If
> you do that, however, you're basically stealing space from the plots to
> devote to the legends and you have to ask yourself if it's worth the
> trouble.
>
> HTH,
> Dennis
>
>
>
> > And for some barplots, how can I just not show the NA values?
> > Thanks for everyone who's helping me :)
>
> > espécie<-as.factor(c(rep("A. australis",49),rep("A. tropicalis",32)))
> > sexo<-
>
> > as.factor(c("F","M","ind","ind","M","M","M","M","ind","M","ind","F","M","in d","ind","ind","M","F","M","ind","ind","ind","ind","ind","ind","ind","M","i nd","F","M","M","M","M","ind","ind","M","M","M","M","M","M","M","M","M","M" ,"M","M","ind","M","M","ind","M","F","M","M","ind","ind","M","M","M","M","M ","ind","F","M","M","ind","ind","M","ind","ind","M","ind","M","M","M","M"," M","M","F","M"))
> > <ggplot2%2Bunsu...@googlegroups.com<ggplot2%252Bunsubscribe@googlegroup s.com>>

Dennis Murphy

unread,
Oct 6, 2010, 5:11:56 PM10/6/10
to Luciano Selzer, Ista Zahn, Daniele, ggplot2
Hi:

Thanks to Luciano and Ista for pointing me in the right direction - I had done some searching of the list archives but hadn't found what I needed. The following replacement seems to work for the q2 plot:


q2 <- p3 + geom_point(size=2.5) + scale_shape("Sexo") +
     labs(x="Comprimento côndilo-basal (mm)",y="Largura rostral (mm)") +
     scale_colour_hue(breaks = levels(espécie),
              labels=parse(text=c(paste('italic("',unique(espécie)[1:2],'")'))))

Substitute this into my earlier script and hopefully that gets what Daniele wanted.

Dennis
daniela.png

Daniele

unread,
Oct 6, 2010, 5:13:22 PM10/6/10
to ggplot2
OMG Dennis! Thank you so much!!!!!!



On 6 out, 18:11, Dennis Murphy <djmu...@gmail.com> wrote:
> Hi:
>
> Thanks to Luciano and Ista for pointing me in the right direction - I had
> done some searching of the list archives but hadn't found what I needed. The
> following replacement seems to work for the q2 plot:
>
> q2 <- p3 + geom_point(size=2.5) + scale_shape("Sexo") +
>      labs(x="Comprimento côndilo-basal (mm)",y="Largura rostral (mm)") +
>      scale_colour_hue(breaks = levels(espécie),
>
> labels=parse(text=c(paste('italic("',unique(espécie)[1:2],'")'))))
>
> Substitute this into my earlier script and hopefully that gets what Daniele
> wanted.
>
> Dennis
>
> On Wed, Oct 6, 2010 at 1:50 PM, Luciano Selzer <luciano.sel...@gmail.com>wrote:
>
>
>
> > Well, this works for me.
>
> >http://groups.google.com/group/ggplot2/browse_thread/thread/6418465a3...
>
> > HTH
>
> > <http://groups.google.com/group/ggplot2/browse_thread/thread/6418465a3...>
> > Luciano
>
> > 2010/10/6 Ista Zahn <iz...@psych.rochester.edu>
>
> > Hi all,
> >> Regarding italic legend lables: you should be able to use expressions
> >> in legend labels, except there is a bug in the way. See
>
> >>http://groups.google.com/group/ggplot2/browse_thread/thread/fe4ed06dd...
>
> >> and
>
> >>http://github.com/hadley/ggplot2/issues/issue/124
>
> >> -Ista
>
> >> On Wed, Oct 6, 2010 at 4:13 PM, Dennis Murphy <djmu...@gmail.com> wrote:
> >> > Hi:
>
> >> > Is this perhaps (close to) what you wanted?
>
> >> > p1<-ggplot(tab,aes(ct,ccb,colour=espécie,shape=sexo))
> >> > q1 <- p1 + geom_point(size=2.5) + scale_shape("Sexo") +
> >> >      labs(x="Comprimento padrão (cm)", y="Comprimento côndilo-basal
> >> (mm)") +
> >> >      opts(legend.position="none")
> >> > p3 <- ggplot(tab, aes(ccb, lr, colour=espécie, shape=sexo))
> >> > q2 <- p3 + geom_point(size=2.5) + scale_shape("Sexo") +
> >> >      labs(x="Comprimento côndilo-basal (mm)",y="Largura rostral (mm)") +
> >> >      opts(legend.text=theme_text(face ="italic"))
>
> >> > grid.newpage()
> >> > vp1 <- viewport(width = 0.4, height = 1, x = 0.2, y = 0.5)
> >> > vp2 <- viewport(width = 0.6, height = 1, x = 0.7, y = 0.5)
> >> > print(q1, vp = vp1)
> >> > print(q2, vp = vp2)
>
> >> > The page rendering uses the grid package; the code is a modification of
> >> the
> >> > examples in section 8.4 of Hadley's book. The warnings will tell you
> >> that
> >> > the observations with missing values are deleted from the plot.
>
> >> > On Wed, Oct 6, 2010 at 11:34 AM, Daniele <danieleviga...@gmail.com>
> >> wrote:
>
> >> >> hi,Brandon..
> >> >> here's my sample data. Can I separate the legend boxes, I only want
> >> >> the "espécie" box to be in italic..
>
> >> > It's not obvious to me how one would do it since the font family cannot
> >> be
> >> > set within scale_*. (But I'd be interested to see how it's done if they
> >> > indeed can have separate fonts :) You could try to separate the two
> >> legends
> >> > - the shapes with one plot and the species with the other, but I had
> >> some
> >> > problems getting that to work and don't have the time to play with it.
> >> If
> >> > you do that, however, you're basically stealing space from the plots to
> >> > devote to the legends and you have to ask yourself if it's worth the
> >> > trouble.
>
> >> > HTH,
> >> > Dennis
>
> >> >> And for some barplots, how can I just not show the NA values?
> >> >> Thanks for everyone who's helping me :)
>
> >> >> espécie<-as.factor(c(rep("A. australis",49),rep("A. tropicalis",32)))
> >> >> sexo<-
>
> >> as.factor(c("F","M","ind","ind","M","M","M","M","ind","M","ind","F","M","in d","ind","ind","M","F","M","ind","ind","ind","ind","ind","ind","ind","M","i nd","F","M","M","M","M","ind","ind","M","M","M","M","M","M","M","M","M","M" ,"M","M","ind","M","M","ind","M","F","M","M","ind","ind","M","M","M","M","M ","ind","F","M","M","ind","ind","M","ind","ind","M","ind","M","M","M","M"," M","M","F","M"))
> >> <ggplot2%2Bunsu...@googlegroups.com<ggplot2%252Bunsubscribe@googlegroup s.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<ggplot2%2Bunsu...@googlegroups.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<ggplot2%2Bunsu...@googlegroups.com >
> >> > More options:http://groups.google.com/group/ggplot2
>
> >> --
> >> Ista Zahn
> >> Graduate student
> >> University of Rochester
> >> Department of Clinical and Social Psychology
> >>http://yourpsyche.org
>
> >> --
> >> 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<ggplot2%2Bunsu...@googlegroups.com >
> >> More options:http://groups.google.com/group/ggplot2
>
>  daniela.png
> 83KExibirDownload
Reply all
Reply to author
Forward
0 new messages