Missing legend?

465 views
Skip to first unread message

waschbaer

unread,
Feb 12, 2010, 12:57:35 AM2/12/10
to ggplot2
Hi @all

I have just created the following plot:

read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip.csv") ->
precipall
read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv") ->
precip2
read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv") ->
precip1
precipall$Month<-factor(precipall$Month,
levels=c("1","2","3","4","5","6","7","8","9","10","11","12"))
p<-ggplot(precipall, aes(Month, Rainfall))
p + geom_bar(stat="identity", color="blue", fill="blue")+
geom_point(data=precip2) +geom_line(data=precip2, colour="red") +
geom_point(data=precip1) +geom_line(data=precip1, colour="red")+
scale_x_discrete(expand=c(0,0), breaks =
c("1","2","3","4","5","6","7","8","9","10","11","12"), labels =
c("January","February","March","April","May","June","July","August","September","October","November","December"))

I tried to find out why there is no legend appearing in my graph but
all the changes I did, didn't help it. Any ideas? Furthermore, how can
I adjust the y-scale so that there is no space between it and the x-
scale?

Thanks for your help in advance.

Martin

smu

unread,
Feb 12, 2010, 3:29:37 AM2/12/10
to waschbaer, ggplot2
Hi,

On Thu, Feb 11, 2010 at 09:57:35PM -0800, waschbaer wrote:
> Hi @all
>
> I have just created the following plot:
>
> read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip.csv") ->
> precipall
> read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv") ->
> precip2
> read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv") ->
> precip1
> precipall$Month<-factor(precipall$Month,
> levels=c("1","2","3","4","5","6","7","8","9","10","11","12"))
> p<-ggplot(precipall, aes(Month, Rainfall))
> p + geom_bar(stat="identity", color="blue", fill="blue")+
> geom_point(data=precip2) +geom_line(data=precip2, colour="red") +
> geom_point(data=precip1) +geom_line(data=precip1, colour="red")+
> scale_x_discrete(expand=c(0,0), breaks =
> c("1","2","3","4","5","6","7","8","9","10","11","12"), labels =
> c("January","February","March","April","May","June","July","August","September","October","November","December"))
>
> I tried to find out why there is no legend appearing in my graph but
> all the changes I did, didn't help it. Any ideas? Furthermore, how can

you have to map variables to aethetic, e.g:

p<-ggplot(precipall, aes(Month, Rainfall))
p + geom_bar(stat="identity", color="blue", fill="blue")+

geom_point(data=precip2) +geom_line(data=precip2, aes(colour="precip2"))

Now you have to set the colours manually

last_plot() + scale_colour_manual("",values=c("precip2"="red"))


> I adjust the y-scale so that there is no space between it and the x-
> scale?

last_plot() + scale_y_continuous("",expand=c(0,0))


regards,
stefan

waschbaer

unread,
Feb 12, 2010, 11:16:48 PM2/12/10
to ggplot2
Hi Stefan

That works perfectly, however, I am still not sure how to actually
control the output of a legend in general in ggplot. I understand that
it is affected by the aestetics command and scale_* command. But
please have a look at the code for the previous graph:

In this graph I managed to bring in the legend for the lines, points
and bars. How do I control the colour of the bars because if I use
aes(colour=precipall) then the colour will be set automatically.
Furthermore, if I use aes() within the barchart call then the legend
symbol for the lines will have boxes around them. How do I control
those outputs in general? Is there a help file where I can read about
this further?

read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip.csv") ->
precipall
read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv") ->
precip2
read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv") ->
precip1
precipall$Month<-factor(precipall$Month,
levels=c("1","2","3","4","5","6","7","8","9","10","11","12"))


p + geom_bar(stat="identity", aes(colour="precipall"))
+geom_point(data=precip2) +geom_line(data=precip2,
aes(colour="precip2")) + geom_point(data=precip1)
+geom_line(data=precip1, aes(colour="precip1"))+


scale_x_discrete(expand=c(0,0), breaks =
c("1","2","3","4","5","6","7","8","9","10","11","12"), labels =
c("January","February","March","April","May","June","July","August","September","October","November","December"))

+ scale_y_continuous("",expand=c(0,0))
last_plot()+ scale_colour_manual("Legend", values=c("precip2"="red",
"precip1"="blue", "precipall"="blue"))


On Feb 12, 6:29 pm, smu <s...@z107.de> wrote:
> Hi,
>
>
>
>
>
> On Thu, Feb 11, 2010 at 09:57:35PM -0800, waschbaer wrote:
> > Hi @all
>
> > I have just created the following plot:
>
> > read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip.csv") ->
> > precipall
> > read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv") ->
> > precip2
> > read.csv("http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv") ->
> > precip1
> > precipall$Month<-factor(precipall$Month,
> > levels=c("1","2","3","4","5","6","7","8","9","10","11","12"))
> > p<-ggplot(precipall, aes(Month, Rainfall))
> > p + geom_bar(stat="identity", color="blue", fill="blue")+
> > geom_point(data=precip2) +geom_line(data=precip2, colour="red") +
> > geom_point(data=precip1) +geom_line(data=precip1, colour="red")+
> > scale_x_discrete(expand=c(0,0), breaks =
> > c("1","2","3","4","5","6","7","8","9","10","11","12"), labels =

> > c("January","February","March","April","May","June","July","August","Septem ber","October","November","December"))

fernando

unread,
Feb 13, 2010, 3:44:23 AM2/13/10
to waschbaer, ggplot2
Hi Waschbaer,

I think you can use fill instead of colour, note that the colour aesthetic
was included in geom_point, so they are managed together by
scale_color_manual

p <- ggplot(precipall, aes(Month, Rainfall))
p + geom_bar(stat="identity", aes(fill="precipall")) +
geom_point(data=precip2, aes(colour = "precip2")) +
geom_line(data=precip2, aes(colour="precip2")) +
geom_point(data=precip1, aes(colour = "precip1")) +
geom_line(data=precip1, aes(colour="precip1")) +


scale_x_discrete(expand=c(0,0), breaks =
c("1","2","3","4","5","6","7","8","9","10","11","12"),
labels =

c("January","February","March","April","May","June","July","August","Septemb

er","October","November","December")) +

scale_y_continuous("",expand=c(0,0)) +

scale_colour_manual("Legend", values=c("precip2"="red",

"precip1"="blue")) +
scale_fill_manual("Legend", values = c("lightblue"))

in theory, by putting the same name to the scales, they should be joined in
the same legend, although not in this case.

Hope this helps,
fernando

-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
waschbaer
Enviado el: sábado, 13 de febrero de 2010 5:17
Para: ggplot2
Asunto: Re: Missing legend?

Hi Stefan

--
You received this message because you are subscribed to the ggplot2 mailing
list.
To post to this group, send email to ggp...@googlegroups.com
To unsubscribe from this group, send email to
ggplot2+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ggplot2

waschbaer

unread,
Feb 13, 2010, 3:59:33 AM2/13/10
to ggplot2
Hi Fernando

Thanks for that. It looks quite similar to the legend I would like to
have. Can you imagine why both legends are not merged? Furthermore, do
you know how to expand the top space within the graph a bit further
because the dot that is above 500 is not entirely shown there.

Again, thanks a lot for your help.

Cheers
Martin

Felipe Carrillo

unread,
Feb 13, 2010, 5:28:00 PM2/13/10
to waschbaer, ggplot2
# save datasets to your current working directory and then do:
     read.csv("precipal.csv") -> precipall
     read.csv("precip2.csv") -> precip2
     read.csv("precip1.csv") -> precip1

      # add a column to each dataset (to be able to merge legends) and bind them together

      precipall$set <- rep("precipall",nrow(precipall))
       precip2$set <- rep("precip2",nrow(precip2))
       precip1$set <- rep("precip1",nrow(precip1))
    mdf <- rbind(precipall,precip2,precip1);str(mdf)
      mdf

       # could select original dataset but since a column was added,
       # subset from mdf for consistancy and be able to use the newly added column 'set'
    mdf_precip2 <- subset(mdf,set=='precip2')
    mdf_precip1 <- subset(mdf,set=='precip1')
    mdf_precipall <- subset(mdf,set=='precipall')
# Create the plot with merged legends
    p <- ggplot(mdf_precipall,aes(Month,Rainfall,fill=set,colour=set)) + geom_bar(stat='identity')
     p <- p + geom_line(data=mdf_precip2) + geom_point(data=mdf_precip2)
     p <- p +  geom_line(data=mdf_precip1) + geom_point(data=mdf_precip1);p

Just add the labels and break manually with scale_colour_manual if desired


Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

----- Original Message ----
> From: waschbaer <lab...@gmail.com>
> To: ggplot2 <ggp...@googlegroups.com>
> Sent: Sat, February 13, 2010 12:59:33 AM
> Subject: Re: Missing legend?
>
> Hi Fernando

Thanks for that. It looks quite similar to the legend I would
> like to
have. Can you imagine why both legends are not merged? Furthermore,
> do
you know how to expand the top space within the graph a bit
> further
because the dot that is above 500 is not entirely shown
> there.

Again, thanks a lot for your
> help.

Cheers
Martin

On Feb 13, 6:44 pm, "fernando" <> ymailto="mailto:fgtabo...@gmail.com"

> De: > ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com [mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com] En nombre

> <> href="mailto:s...@z107.de">s...@z107.de>

> wrote:
>
>
>
> > Hi,
>
> > On Thu, Feb
> 11, 2010 at 09:57:35PM -0800, waschbaer wrote:
> > > Hi
> @all
>
> > > I have just created the following
> plot:
>

> > > read.csv("> href="http://dl.dropbox.com/u/4236038/seasonal_precip.csv" target=_blank

> > > read.csv("> href="http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv" target=_blank

> > > read.csv("> href="http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv" target=_blank

> send email to > href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com


> To
> unsubscribe from this group, send email to

> ggplot2+> ymailto="mailto:unsub...@googlegroups.com"
> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com


>
> For more options, visit this group
> athttp://groups.google.com/group/ggplot2

--

You received this message
> because you are subscribed to the ggplot2 mailing list.
To post to this

> group, send email to > href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com


To
> unsubscribe from this group, send email to

ggplot2+> ymailto="mailto:unsub...@googlegroups.com"
> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com


For
> more options, visit this group at

> href="http://groups.google.com/group/ggplot2" target=_blank
> >http://groups.google.com/group/ggplot2


waschbaer

unread,
Feb 13, 2010, 5:41:58 PM2/13/10
to ggplot2
Hi Felipe

Thanks. But now there i a square for each set in the legend and no
lines with dots for the two lines. Can I change those keys somehow or
does ggplot take those keys from the aestetics and you can't really
change the shape?

Felipe Carrillo

unread,
Feb 13, 2010, 7:21:21 PM2/13/10
to waschbaer, ggplot2
Don't add the column to the dataset manually. The code below will do that for you:

 precipall$set <- rep("precipall",nrow(precipall))
 precip2$set <- rep("precip2",nrow(precip2))
 precip1$set <- rep("precip1",nrow(precip1))
 
I get a nice graphic, not sure what you mean by square for each set
I'll attach the graphic.

 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

----- Original Message ----
> From: waschbaer <lab...@gmail.com>
> To: ggplot2 <ggp...@googlegroups.com>
> Sent: Sat, February 13, 2010 2:41:58 PM
> Subject: Re: Missing legend?
>

> Hi Felipe

Thanks. But now there i a square for each set in the legend and
> no
lines with dots for the two lines. Can I change those keys somehow
> or
does ggplot take those keys from the aestetics and you can't
> really
change the shape?

On Feb 14, 8:28 am, Felipe Carrillo

> <> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com>

> > From: waschbaer <> href="mailto:lab...@gmail.com">lab...@gmail.com>
> > To: ggplot2
> <> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com>


>
> > Sent: Sat, February 13, 2010 12:59:33 AM
> > Subject: Re: Missing
> legend?
>
> > Hi Fernando
>
> Thanks for that. It
> looks quite similar to the legend I would> like to
>
> have. Can
> you imagine why both legends are not merged? Furthermore,> do
>
>
> you know how to expand the top space within the graph a bit>
> further
>
> because the dot that is above 500 is not entirely
> shown
>
> > there.
>
> Again, thanks a lot for
> your
>
> > help.
>
> Cheers
>
> Martin
>
> On Feb 13, 6:44 pm, "fernando" <>

> ymailto="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com"
>
>
>
>
>
>
> > href="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com">> ymailto="mailto:fgtabo...@gmail.com"

> ymailto="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"

> ymailto="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"

> > <> href="mailto:> href="mailto:s...@z107.de">s...@z107.de">> href="mailto:s...@z107.de">s...@z107.de>


> >
> wrote:
>
> > > Hi,
>
> > > On Thu,
> Feb
> > 11, 2010 at 09:57:35PM -0800, waschbaer wrote:
> >
> > > Hi
> > @all
>
> > > > I have just
> created the following
> > plot:
>
> > > >

> read.csv("> href="> href="http://dl.dropbox.com/u/4236038/seasonal_precip.csv" target=_blank
> >http://dl.dropbox.com/u/4236038/seasonal_precip.csv" target=_blank


>
> > >> target=_blank >http://dl.dropbox.com/u/4236038/seasonal_precip.csv")
> ->
>
> > > precipall
> > > > read.csv(">

> href="> target=_blank >http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv"
> target=_blank


> > >> href="http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv" target=_blank
> >http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv")
> ->
>
> > > precip2
> > > > read.csv(">

> href="> target=_blank >http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv"
> target=_blank

> email to > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com
> >

> To
> > unsubscribe from this group, send email to
> >

> ggplot2+> ymailto="mailto:> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com"
>
> > href="mailto:> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com">> ymailto="mailto:unsub...@googlegroups.com"

> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com
>
>
> > For more options, visit this group
> >
> athttp://groups.google.com/group/ggplot2
>
> --
> You received
> this message> because you are subscribed to the ggplot2 mailing list.
>
> To post to this

> > group, send email to > href="mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com
>

> To
> > unsubscribe from this group, send email to
>
>

> ggplot2+> ymailto="mailto:> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com"
>
>
>
>
> > href="mailto:> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com">> ymailto="mailto:unsub...@googlegroups.com"

> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com
>
> For
> > more options, visit this group at

> > href="> href="http://groups.google.com/group/ggplot2" target=_blank
> >http://groups.google.com/group/ggplot2" target=_blank

graph.pdf

waschbaer

unread,
Feb 14, 2010, 3:13:14 AM2/14/10
to ggplot2
Hi Felipe

That's the graph I get as well. However, I don't know if it is
possible, but I would like to represent the lines by coloured lines in
the legend and not just by coloured squares. What do you think?

Cheers
Martin

> > > adjust the y-scale so that there is...
>
> read more »
>
>  graph.pdf
> 20KViewDownload

Felipe Carrillo

unread,
Feb 14, 2010, 3:15:03 PM2/14/10
to waschbaer, ggplot2
The lines and points show nicely in the legend but once the fill=set is applied the geom_bar
throws everything from the legend. Try the code below, I don't think is what you want but close enough(maybe)

# save datasets to your current working directory and then do:
     read.csv("precipal.csv") -> precipall
     read.csv("precip2.csv") -> precip2
     read.csv("precip1.csv") -> precip1
      # add a column to each dataset (to be able to merge legends) and bind them together

      precipall$set <- rep("precipall",nrow(precipall));precipall


       precip2$set <- rep("precip2",nrow(precip2))
       precip1$set <- rep("precip1",nrow(precip1))
    mdf <- rbind(precipall,precip2,precip1);str(mdf)
      mdf
      

# Create the plot with merged legends

    p <- ggplot(data=subset(mdf,set=='precipall'),aes(Month,Rainfall,colour=set))
    p <- p +  geom_bar(stat='identity',fill='pink');p
   pp <- p +  geom_line(data=subset(mdf,set!='precipall'),legend=F) ;pp
   ppp <- pp +  geom_point(data=subset(mdf,set!='precipall'));ppp
 

 


----- Original Message ----
> From: waschbaer <lab...@gmail.com>
> To: ggplot2 <ggp...@googlegroups.com>
> Sent: Sun, February 14, 2010 12:13:14 AM
> Subject: Re: Missing legend?
>

> Hi Felipe

That's the graph I get as well. However, I don't know if it
> is
possible, but I would like to represent the lines by coloured lines
> in
the legend and not just by coloured squares. What do you
> think?

Cheers
Martin

On Feb 14, 10:21 am, Felipe

> Carrillo <> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com>

> wrote:
> Don't add the column to the dataset manually. The code below will
> do that for you:
>  precipall$set <-
> rep("precipall",nrow(precipall))
>  precip2$set <-
> rep("precip2",nrow(precip2))
>  precip1$set <-
> rep("precip1",nrow(precip1))
>  
> I get a nice graphic, not sure
> what you mean by square for each set
> I'll attach the
> graphic.
>
>  
> Felipe D. Carrillo
> Supervisory
> Fishery Biologist
> Department of the Interior
> US Fish &
> Wildlife Service
> California, USA
>
> ----- Original Message
> ----

> > From: waschbaer <> href="mailto:lab...@gmail.com">lab...@gmail.com>
> > To: ggplot2
> <> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com>
>

> > Sent: Sat, February 13, 2010 2:41:58 PM
> > Subject: Re: Missing
> legend?
>
> > Hi Felipe
>
> Thanks. But now there i a
> square for each set in the legend and> no
>
> lines with dots for
> the two lines. Can I change those keys somehow> or
>
> does
> ggplot take those keys from the aestetics and you can't>
> really
>
> change the shape?
>
> On Feb 14, 8:28 am,
> Felipe Carrillo
>
>
>

> > <> href="mailto:> ymailto="mailto:mazatlanmex...@yahoo.com"
> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com">> ymailto="mailto:mazatlanmex...@yahoo.com"

> <> href="mailto:> href="mailto:lab...@gmail.com">lab...@gmail.com">> ymailto="mailto:lab...@gmail.com"

> href="mailto:lab...@gmail.com">lab...@gmail.com>
> > > To:
> ggplot2

> > <> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"

> > ymailto="mailto:> href="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com">> ymailto="mailto:fgtabo...@gmail.com"
> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com"
>
> >
> > href="mailto:> href="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com">> ymailto="mailto:fgtabo...@gmail.com"

> > > href="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"

> > ymailto="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"

> > > href="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"

> > > <> href="mailto:> href="mailto:> ymailto="mailto:s...@z107.de" href="mailto:s...@z107.de">s...@z107.de">> ymailto="mailto:s...@z107.de"
> href="mailto:s...@z107.de">s...@z107.de">> href="mailto:> ymailto="mailto:s...@z107.de" href="mailto:s...@z107.de">s...@z107.de">> ymailto="mailto:s...@z107.de"

> href="mailto:s...@z107.de">s...@z107.de>
>
> >
> wrote:
>
> > > > Hi,
>
> > > > On
> Thu,
> > Feb
> > > 11, 2010 at 09:57:35PM -0800, waschbaer
> wrote:
>
> > > > Hi
> > > @all
>
>
> > > > > I have just
> > created the following
> >
> > plot:
>

> > read.csv("> href="> href="> href="http://dl.dropbox.com/u/4236038/seasonal_precip.csv" target=_blank

> target=_blank
>
> > > >> target=_blank >> href="http://dl.dropbox.com/u/4236038/seasonal_precip.csv" target=_blank

> >http://dl.dropbox.com/u/4236038/seasonal_precip.csv")
> >
> ->
>
> > > > precipall
> > > > >
> read.csv(">

> > href="> target=_blank >> href="http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv" target=_blank
> >http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv"
> >
> target=_blank
> > > >> href="> href="http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv" target=_blank

> > href="> target=_blank >> href="http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv" target=_blank
> >http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv"
> >
> target=_blank
> > > >> href="> href="http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv" target=_blank

--

You
> received this message because you are subscribed to the ggplot2 mailing
> list.

To post to this group, send email to > ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com


To
> unsubscribe from this group, send email to
ggplot2+> ymailto="mailto:unsub...@googlegroups.com"
> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com
For
> more options, visit this group at

http://groups.google.com/group/ggplot2


Felipe Carrillo

unread,
Feb 14, 2010, 3:31:00 PM2/14/10
to waschbaer, ggplot2
The lines and points show nicely in the legend but once the fill=set is applied the geom_bar
throws everything from the legend. Try the code below, I don't think is what you want but close enough(maybe)

# save datasets to your current working directory and then do:


     read.csv("precipal.csv") -> precipall
     read.csv("precip2.csv") -> precip2
     read.csv("precip1.csv") -> precip1
      # add a column to each dataset (to be able to merge legends) and bind them together

      precipall$set <- rep("precipall",nrow(precipall));precipall


       precip2$set <- rep("precip2",nrow(precip2))
       precip1$set <- rep("precip1",nrow(precip1))
    mdf <- rbind(precipall,precip2,precip1);str(mdf)
      mdf
      

# Create the plot with merged legends

    p <- ggplot(data=subset(mdf,set=='precipall'),aes(Month,Rainfall,colour=set))
    p <- p +  geom_bar(stat='identity',fill='pink');p
   pp <- p +  geom_line(data=subset(mdf,set!='precipall'),legend=F) ;pp
   ppp <- pp +  geom_point(data=subset(mdf,set!='precipall'));ppp
 


Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

----- Original Message ----
> From: waschbaer <lab...@gmail.com>
> To: ggplot2 <ggp...@googlegroups.com>
> Sent: Sun, February 14, 2010 12:13:14 AM
> Subject: Re: Missing legend?
>

> Hi Felipe

That's the graph I get as well. However, I don't know if it
> is
possible, but I would like to represent the lines by coloured lines
> in
the legend and not just by coloured squares. What do you
> think?

Cheers
Martin

On Feb 14, 10:21 am, Felipe

> Carrillo <> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com>

> wrote:
> Don't add the column to the dataset manually. The code below will
> do that for you:
>  precipall$set <-
> rep("precipall",nrow(precipall))
>  precip2$set <-
> rep("precip2",nrow(precip2))
>  precip1$set <-
> rep("precip1",nrow(precip1))
>  
> I get a nice graphic, not sure
> what you mean by square for each set
> I'll attach the
> graphic.
>
>  
> Felipe D. Carrillo
> Supervisory
> Fishery Biologist
> Department of the Interior
> US Fish &
> Wildlife Service
> California, USA
>
> ----- Original Message
> ----

> > From: waschbaer <> href="mailto:lab...@gmail.com">lab...@gmail.com>
> > To: ggplot2
> <> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com>
>

> > Sent: Sat, February 13, 2010 2:41:58 PM
> > Subject: Re: Missing
> legend?
>
> > Hi Felipe
>
> Thanks. But now there i a
> square for each set in the legend and> no
>
> lines with dots for
> the two lines. Can I change those keys somehow> or
>
> does
> ggplot take those keys from the aestetics and you can't>
> really
>
> change the shape?
>
> On Feb 14, 8:28 am,
> Felipe Carrillo
>
>
>

> > <> href="mailto:> ymailto="mailto:mazatlanmex...@yahoo.com"
> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com">> ymailto="mailto:mazatlanmex...@yahoo.com"

> <> href="mailto:> href="mailto:lab...@gmail.com">lab...@gmail.com">> ymailto="mailto:lab...@gmail.com"

> href="mailto:lab...@gmail.com">lab...@gmail.com>
> > > To:
> ggplot2

> > <> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"

> > ymailto="mailto:> href="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com">> ymailto="mailto:fgtabo...@gmail.com"
> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com"
>
> >
> > href="mailto:> href="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com">> ymailto="mailto:fgtabo...@gmail.com"

> > > href="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"

> > ymailto="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"

> > > href="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"

> > > <> href="mailto:> href="mailto:> ymailto="mailto:s...@z107.de" href="mailto:s...@z107.de">s...@z107.de">> ymailto="mailto:s...@z107.de"
> href="mailto:s...@z107.de">s...@z107.de">> href="mailto:> ymailto="mailto:s...@z107.de" href="mailto:s...@z107.de">s...@z107.de">> ymailto="mailto:s...@z107.de"

> href="mailto:s...@z107.de">s...@z107.de>
>
> >
> wrote:
>
> > > > Hi,
>
> > > > On
> Thu,
> > Feb
> > > 11, 2010 at 09:57:35PM -0800, waschbaer
> wrote:
>
> > > > Hi
> > > @all
>
>
> > > > > I have just
> > created the following
> >
> > plot:
>

> > read.csv("> href="> href="> href="http://dl.dropbox.com/u/4236038/seasonal_precip.csv" target=_blank

> target=_blank
>
> > > >> target=_blank >> href="http://dl.dropbox.com/u/4236038/seasonal_precip.csv" target=_blank

> >http://dl.dropbox.com/u/4236038/seasonal_precip.csv")
> >
> ->
>
> > > > precipall
> > > > >
> read.csv(">

> > href="> target=_blank >> href="http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv" target=_blank
> >http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv"
> >
> target=_blank
> > > >> href="> href="http://dl.dropbox.com/u/4236038/seasonal_precip_09.csv" target=_blank

> > href="> target=_blank >> href="http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv" target=_blank
> >http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv"
> >
> target=_blank
> > > >> href="> href="http://dl.dropbox.com/u/4236038/seasonal_precip_08.csv" target=_blank

--

You
> received this message because you are subscribed to the ggplot2 mailing
> list.

To post to this group, send email to > ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com


To
> unsubscribe from this group, send email to
ggplot2+> ymailto="mailto:unsub...@googlegroups.com"
> href="mailto:unsub...@googlegroups.com">unsub...@googlegroups.com
For
> more options, visit this group at

http://groups.google.com/group/ggplot2


waschbaer

unread,
Feb 14, 2010, 7:16:59 PM2/14/10
to ggplot2
Hi Felipe

Thanks again for the effort you put in this. I really appreciate your
help.

Please have a look at this graph: http://dl.dropbox.com/u/4236038/sample_plot.pdf.
It shows you what I meant by having the lines in the legend. I think I
will go with your other option where I will have two legends and then
merge those two graphically in Inkscape because I think as ggplot is
using the aestetics for the legend there is no other option if I want
to have the lines as lines in the legend.

Cheers
Martin

> > > > it is...
>
> read more »

fernando

unread,
Feb 14, 2010, 7:58:30 PM2/14/10
to waschbaer, ggplot2
Hi Martin,

I feel this is cheating, but worked ... I realized that after seeing
Felipe's code (thanks).
The trick was to add variables with height set to zero except for precipall,
and with size set to zero for all the records. Then you draw a bar using the
first auxiliary variable, but set the wide of the border to zero using the
aesthetic size and the second variable (note that this aesthetic is not used
with line or point) ... it is not quite elegant, but I'm happy :) ... hope
also you,

Best,
fernando

# load libraries
library(ggplot2)

precipall$Month <- factor(precipall$Month,
levels=c("1","2","3","4","5","6","7","8","9","10","11","12"))


# add a column to each dataset (to be able to merge legends) and bind them
together
precipall$set <- rep("precipall",nrow(precipall))
precipall
precip2$set <- rep("precip2",nrow(precip2))
precip1$set <- rep("precip1",nrow(precip1))
mdf <- rbind(precipall,precip2,precip1);str(mdf)
mdf

mdf$barheight <- mdf$Rainfall # auxiliary to do not draw bars for precip1
and precip2
mdf$barheight[mdf$set != "precipall"] <- 0
mdf$barsize <- 0 # width of the border of bars
mdf$set <- factor(mdf$set) # should be a factor
mdf$Month <- as.numeric(mdf$Month) # should be continuous to draw lines
correctly

qplot(x = Month, y = barheight, geom = "bar",
stat = "identity", fill = set, size = barsize, data = mdf) +
geom_line(aes(x = Month, y = Rainfall, colour = set), size = 1, data
= mdf) +
geom_point(aes(x = Month, y = Rainfall, colour = set), size = 2,
data = mdf) +


scale_colour_manual("Legend", values=c("precip2"="red",

"precip1"="blue", "precipall" = NA)) +
scale_fill_manual("Legend", values = c("precipall" = "lightblue",
"precip1" = NULL, "precip2" = NULL)) +
scale_size(legend = FALSE) +
scale_y_continuous("",expand=c(0,0), limits = c(0, 600)) +
scale_x_continuous(expand=c(0,0), breaks = c(1:12),
labels =
c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
"Oct","Nov","Dec"))

# using ggplot
ggplot(aes(x = Month, y = barheight), data = mdf) +
geom_bar(stat = "identity", aes(fill = set, size = barsize)) +
geom_line(aes(y = Rainfall, colour = set), size = 1, data = mdf) +
geom_point(aes(x = Month, y = Rainfall, colour = set), size = 2,
data = mdf) +


scale_colour_manual("Legend", values=c("precip2"="red",

"precip1"="blue", "precipall" = NA)) +
scale_fill_manual("Legend", values = c("precipall" = "lightblue",
"precip1" = NULL, "precip2" = NULL)) +
scale_size(legend = FALSE) +
scale_y_continuous("",expand=c(0,0), limits = c(0, 600)) +
scale_x_continuous(expand=c(0,0), breaks = c(1:12),
labels =
c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
"Oct","Nov","Dec"))


-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
waschbaer
Enviado el: lunes, 15 de febrero de 2010 1:17
Para: ggplot2
Asunto: Re: legend

Hi Felipe

Cheers
Martin

--

You received this message because you are subscribed to the ggplot2 mailing
list.

To post to this group, send email to ggp...@googlegroups.com


To unsubscribe from this group, send email to

ggplot2+u...@googlegroups.com

waschbaer

unread,
Feb 14, 2010, 8:04:04 PM2/14/10
to ggplot2
Hi Fernando

Gee, isn't that a bit too much for a graph, which should just show
average monthly rainfalls over a 50 year period as bars together with
avg. monthly rainfall for the last year as lines? I thought ggplot and
R should make producing graphs easier and not more complicated?

Cheers
Martin

> > > > href="mailto:> href="mailto:>...
>
> read more »

Felipe Carrillo

unread,
Feb 14, 2010, 8:38:48 PM2/14/10
to waschbaer, ggplot2
You could also add expand_limits to see the point hidding above 500, put a white background color
so you don't have to see the legends white squares and also move the legends to the plot area and make
it appear as if it was only one legend. Good luck


read.csv("precipal.csv") ->precipall
read.csv("precip2.csv") ->precip2
read.csv("precip1.csv") ->precip1

    p<- ggplot(precipall,aes(Month, Rainfall)) + theme_bw()
p + geom_bar(stat="identity",aes(fill="precipall")) + geom_point(data=precip2,aes(colour ="precip2"))+
           geom_line(data=precip2,aes(colour="precip2"))+


         geom_point(data=precip1, aes(colour = "precip1")) +
geom_line(data=precip1, aes(colour="precip1"))+

         scale_x_discrete(expand=c(0,0), breaks =c("1","2","3","4","5","6","7","8","9","10","11","12"),
           labels =c("January","February","March","April","May","June","July","August","September","October","November","December")) +
scale_y_continuous("",expand=c(0,0)) +
         scale_colour_manual("Precipitation",values=c("precip2"="red","precip1"="blue"))+
scale_fill_manual("",values =c("lightblue")) + expand_limits(y=c(0,600)) +
opts(legend.position=c(0.90,0.85))
 

----- Original Message ----
> From: waschbaer <lab...@gmail.com>
> To: ggplot2 <ggp...@googlegroups.com>
> Sent: Sun, February 14, 2010 4:16:59 PM
> Subject: Re: legend
>
> Hi Felipe

Thanks again for the effort you put in this. I really
> appreciate your
help.

Please have a look at this graph:
> http://dl.dropbox.com/u/4236038/sample_plot.pdf.
It shows you what I meant by
> having the lines in the legend. I think I
will go with your other option
> where I will have two legends and then
merge those two graphically in
> Inkscape because I think as ggplot is
using the aestetics for the legend
> there is no other option if I want
to have the lines as lines in the
> legend.

Cheers
Martin

On Feb 15, 6:31 am, Felipe

> > From: waschbaer <> href="mailto:lab...@gmail.com">lab...@gmail.com>
> > To: ggplot2
> <> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com>
>

> > Sent: Sun, February 14, 2010 12:13:14 AM
> > Subject: Re: Missing
> legend?
>
> > Hi Felipe
>
> That's the graph I get as
> well. However, I don't know if it> is
>
> possible, but I would
> like to represent the lines by coloured lines> in
>
> the legend
> and not just by coloured squares. What do you
>
> >
> think?
>
> Cheers
> Martin
>
> On Feb 14,
> 10:21 am, Felipe
>
>
>
> > Carrillo <>

> href="mailto:> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com">> ymailto="mailto:mazatlanmex...@yahoo.com"

> <> href="mailto:> href="mailto:lab...@gmail.com">lab...@gmail.com">> ymailto="mailto:lab...@gmail.com"
> href="mailto:lab...@gmail.com">lab...@gmail.com>
> > > To:
> ggplot2
> > <> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com>
>
>

> > > Sent: Sat, February 13, 2010 2:41:58 PM
> > > Subject: Re:
> Missing
> > legend?
>
> > > Hi Felipe
>
>
> > Thanks. But now there i a
> > square for each set in the legend
> and> no
>
> > lines with dots for
> > the two lines.
> Can I change those keys somehow> or
>
> > does
> >
> ggplot take those keys from the aestetics and you can't>
> >
> really
>
> > change the shape?
>
> > On Feb 14,
> 8:28 am,
> > Felipe Carrillo
>
> > > <>

> href="mailto:> ymailto="mailto:> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com"
>
> > href="mailto:> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com">> ymailto="mailto:mazatlanmex...@yahoo.com"
> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com">>
> ymailto="mailto:> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com"
>
> > href="mailto:> href="mailto:mazatlanmex...@yahoo.com">mazatlanmex...@yahoo.com">> ymailto="mailto:mazatlanmex...@yahoo.com"

> > <> href="mailto:> href="mailto:> ymailto="mailto:lab...@gmail.com"

> href="mailto:lab...@gmail.com">lab...@gmail.com">> ymailto="mailto:lab...@gmail.com"

> href="mailto:lab...@gmail.com">lab...@gmail.com">> ymailto="mailto:> ymailto="mailto:lab...@gmail.com"

> href="mailto:lab...@gmail.com">lab...@gmail.com"
> >
> href="mailto:> href="mailto:lab...@gmail.com">lab...@gmail.com">> ymailto="mailto:lab...@gmail.com"
> href="mailto:lab...@gmail.com">lab...@gmail.com>
> > > >
> To:
> > ggplot2
> > > <> href="mailto:>

> ymailto="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">>
> ymailto="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com>
>
>

> href="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com">> ymailto="mailto:fgtabo...@gmail.com"
> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com">>
> ymailto="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com"
> >
> href="mailto:> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com">> ymailto="mailto:fgtabo...@gmail.com"
> href="mailto:fgtabo...@gmail.com">fgtabo...@gmail.com"
>
> >

> > href="mailto:> href="mailto:> href="mailto:> ymailto="mailto:fgtabo...@gmail.com"

> > href="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">>
> ymailto="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
>

> > > > href="mailto:> href="mailto:> ymailto="mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"

> > ymailto="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
>
> > > href="mailto:> href="mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">>
> ymailto="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com
> >
> [mailto:>
> > > ymailto="mailto:> href="mailto:>

> ymailto="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">>
> ymailto="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
>

> > > > href="mailto:> href="mailto:> ymailto="mailto:> ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com"
>
> > href="mailto:> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com">> ymailto="mailto:ggp...@googlegroups.com"

--

You received this message because you are subscribed to the
> ggplot2 mailing list.

To post to this group, send email to > ymailto="mailto:ggp...@googlegroups.com"
> href="mailto:ggp...@googlegroups.com">ggp...@googlegroups.com

waschbaer

unread,
Feb 15, 2010, 12:24:03 AM2/15/10
to ggplot2
Thanks for that.

On Feb 15, 11:38 am, Felipe Carrillo <mazatlanmex...@yahoo.com> wrote:
> You could also add expand_limits to see the point hidding above 500, put a white background color
> so you don't have to see the legends white squares and also move the legends to the plot area and make
> it appear as if it was only one legend. Good luck
>
> read.csv("precipal.csv") ->precipall
> read.csv("precip2.csv") ->precip2
> read.csv("precip1.csv") ->precip1
>     p<- ggplot(precipall,aes(Month, Rainfall)) + theme_bw()
> p + geom_bar(stat="identity",aes(fill="precipall")) + geom_point(data=precip2,aes(colour ="precip2"))+
>            geom_line(data=precip2,aes(colour="precip2"))+
>          geom_point(data=precip1, aes(colour = "precip1")) +
> geom_line(data=precip1, aes(colour="precip1"))+
>          scale_x_discrete(expand=c(0,0), breaks =c("1","2","3","4","5","6","7","8","9","10","11","12"),

>            labels =c("January","February","March","April","May","June","July","August","Septe mber","October","November","December")) +
> scale_y_continuous("",expand=c(0,0)) +
>          scale_colour_manual("Precipitation",values=c("precip2"="red","precip1"="blu e"))+

> > href="mailto:>...
>
> read more »

Reply all
Reply to author
Forward
0 new messages