How to add legend for multiple lines in GGPLOT2

1,649 views
Skip to first unread message

ernes...@gmail.com

unread,
Oct 9, 2017, 2:51:19 PM10/9/17
to ggplot2

Hi

How can I add legend for multiple lines in GGPLOT2?
i.e I want put one line to represent say predictions for model Z
   another line to represent say biomass for model W.

Kindly assist if you have codes on this

Brandon Hurr

unread,
Oct 9, 2017, 2:57:32 PM10/9/17
to ernes...@gmail.com, ggplot2
Help us out with an example. 
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

Takes some of the difficulty away for us to help you. 

Thanks,
B

--
--
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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Crump, Ron

unread,
Oct 10, 2017, 3:21:03 AM10/10/17
to ernes...@gmail.com, ggplot2
Hi,
It would be easier if you had some better indication of what you want to
achieve. A reproducible example and the code of your attempts so far
would help.

Does this help to get you started?

library(ggplot2)
DF<-data.frame(x=c(1,2,3,1,2,3),pred=c(1,2.5,2,1.5,2,2.5),model=rep(c("Z","W"),each=3))
ggplot(DF)+geom_line(aes(x=x,y=pred,colour=model))

If not, how do you want it to be different?

Try taking a look at http://www.cookbook-r.com/Graphs/ too.

Regards,
Ron.

Crump, Ron

unread,
Oct 10, 2017, 5:39:14 AM10/10/17
to ERNEST MAUYA, ggplot2
Dear Ernest,

> Thank you for your response, here is my codes

Some indication of your data would be useful to go with the code
would be good.
> ggplot(hdata, aes(x=d,y=h)) + geom_point() +
>   geom_line(size=1.1,colour = "red",aes(x = d, y
> =HDnaslund(d,1.7550006,0.1566448))) +
>   geom_line(size=1.1,colour = "blue",aes(x = d, y
> =HDcurtis(d,32.345704,9.696651)))+
>   geom_line(size=1.1,colour = "green",aes(x = d, y
> =HDmeyer(d,72.410000000,0.007907796)))
>
> I want to have have a legend showing that line which is red= HDnaslund,
> and line 2 which is blue= HDcurtis

This isn't a reproducible example. There is no data (and it doesn't
need to be your real data, or all of it) and you are not providing all
the info that might be needed - for example I had to search the web to
find that the functions you are using are part of the lmfor package.

You can move towards what you need by setting up your data.frame
something like:

d <- 1:100 # no idea of the real range of these values
hdata <- data.frame(d=c(d,d,d),
h=c(HDnaslund(d,1.7550006,0.1566448), # I'm assuming that these
HDcurtis(d,32.345704,9.696651), # functions produce a vector
HDmeyer(d,72.410000000,0.007907796)), # of h predictions based on d
f=rep(c("HDnaslund","HDcurtis","HDmeyer"),each=length(d)),
stringsAsFactors=FALSE)
hdata$f <- factor(hdata$f,levels=c("HDnaslund","HDcurtis","HDmeyer"))

ggplot(hdata)+geom_line(aes(x=d,y=h,colour=f))+scale_colour_manual(values=c("red","blue","green"))

> Can you assist on this?

See above. But it isn't tested. But you could have worked this out if
you'd looked at the web site I referenced before, or just the little bit
of code I gave.

Regards,
Ron.

> Does this help to get you started?
>
> library(ggplot2)
> DF<-data.frame(x=c(1,2,3,1,2,3),pred=c(1,2.5,2,1.5,2,2.5),model=rep(c("Z","W"),each=3))
> ggplot(DF)+geom_line(aes(x=x,y=pred,colour=model))
>
> If not, how do you want it to be different?
>
> Try taking a look at http://www.cookbook-r.com/Graphs/
> <http://www.cookbook-r.com/Graphs/> too.
>

方操

unread,
Oct 10, 2017, 6:22:15 AM10/10/17
to Brandon Hurr, ernestmauya, ggplot2

Hello Markdown Here!



------------------ 原始邮件 ------------------
发件人: "Brandon Hurr"<brando...@gmail.com>;
发送时间: 2017年10月10日(星期二) 凌晨2:57
收件人: "ernestmauya"<ernes...@gmail.com>;
抄送: "ggplot2"<ggp...@googlegroups.com>;
主题: Re: How to add legend for multiple lines in GGPLOT2

Help us out with an example. 
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

Takes some of the difficulty away for us to help you. 

Thanks,
B

On Mon, Oct 9, 2017 at 5:37 AM, <ernes...@gmail.com> wrote:

Hi

How can I add legend for multiple lines in GGPLOT2?
i.e I want put one line to represent say predictions for model Z
   another line to represent say biomass for model W.

Kindly assist if you have codes on this

--
--
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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

Crump, Ron

unread,
Oct 16, 2017, 7:11:51 AM10/16/17
to Ernest Mauya, ggplot2
Dear Ernest,

Please include the ggplot list in your queries. Someone else might be
able to help too, or have a better suggestion.

Also, sending examples as Word documents isn't helpful or data as CSV
(unless you provide the exact command you use to read that file).
Inline code in a plain text email with data as the output from dput
is the way to go for future reference.

Anyway, on to the query...

> Dear Ron
>
> Thank you very much for your codes they work. However I have one challenge when I want to plot the points
> to see how the models perform. With my previous codes it works bu the challenge is i do not have legend. I have
> attached the data and word document that shows the figures.

I forgot the geom_point call. I've updated the code, assuming
observations at each x-value.

This will give you some data points, three predicted lines and
a single legend with a point for the data points and lines of
different colour for each prediction method.

hdata<-data.frame(
# I've assumed data observations at each point in d
d=c(d,d,d,d),
# A column of observations, actual then all missing corresponding to
# where the predictions are
o=c(rnorm(length(d),mean=d),rep(NA,3*length(d))),
# Column of predicted values, NA where the observed data are
h=c(rep(NA,length(d)),HDnaslund(d,1.7550006,0.1566448),
HDcurtis(d,32.345704,9.696651),
HDmeyer(d,72.410000000,0.007907796)),
# Identifier to tag Observed and Predicted values
f=rep(c("Observed","HDnaslund","HDcurtis","HDmeyer"),each=length(d)),
stringsAsFactors=FALSE)

# Change int oa factor with the levels in the order I want them to be
hdata$f <- factor( hdata$f,
levels=c("Observed","HDnaslund","HDcurtis","HDmeyer") )

# plot, using scales to and the factor f to have everything in
# the legend, but some bits "turned off"
ggplot(hdata)+
geom_line(aes(x=d,y=h,colour=f,linetype=f))+
geom_point(aes(x=d,y=o,colour=f,shape=f))+
scale_colour_manual(values=c("black","red","blue","green"))+
scale_linetype_manual(values=c(0,1,1,1))+
scale_shape_manual(values=c(19,NA,NA,NA))

Ron.

>
> Looking forward to hear From you.
>
> Regards
>
> Ernest
>
>
> ________________________________________
> From: ggp...@googlegroups.com <ggp...@googlegroups.com> on behalf of Crump, Ron <R.E....@warwick.ac.uk>
> Sent: Tuesday, October 10, 2017 12:38 PM
> To: ERNEST MAUYA; ggplot2
> Subject: Re: How to add legend for multiple lines in GGPLOT2
Reply all
Reply to author
Forward
0 new messages