Multiple plots with one legend

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple plots with one legend

mavkoup
Hi,

I've created 3 plots one under the other, and want to include a legend on the right that spans the height of all 3 plots.

How can I do this?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Multiple plots with one legend

David Winsemius

On Mar 26, 2011, at 7:06 PM, mavkoup wrote:

> Hi,
>
> I've created 3 plots one under the other, and want to include a  
> legend on
> the right that spans the height of all 3 plots.

?mtext
# with the `las` parameter for rotation

Or you can use:

text(x,y,  "some text", srt=-90,xpd=NA )
>


--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple plots with one legend

mavkoup
Ok I think I can figure that out. Which leads me to a further question. My 3 plots contain 10 time series, each with a different name and color. Can I create the legend such that it has a line of the correct color follow by the name of the series? I.e.

line(with color1) NAME 1
line(with color2) NAME 2
...

I have a vector of colors I'm using and a vector of names.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Multiple plots with one legend

David Winsemius

On Mar 26, 2011, at 8:27 PM, mavkoup wrote:

> Ok I think I can figure that out. Which leads me to a further  
> question. My 3
> plots contain 10 time series, each with a different name and color.  
> Can I
> create the legend such that it has a line of the correct color  
> follow by the
> name of the series? I.e.

"plots" and therefore "legends" in R can take many forms. Have your  
read the Posting Guide's recommendations on how to submit a  
reproducible example yet? That would be particularly important if you  
have three different plots on the same page.

>
> line(with color1) NAME 1
> line(with color2) NAME 2
> ...
>
> I have a vector of colors I'm using and a vector of names.

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple plots with one legend

jholtman
In reply to this post by mavkoup
You can use 'layout' to create 4 plot areas: the 3 plot you currently
have, and one for the legend.  Try this to see what happens:

> x <- cbind(rbind(1,2,3), 4)
> layout(x, width = c(5,1))
> layout.show(4)


On Sat, Mar 26, 2011 at 7:06 PM, mavkoup <[hidden email]> wrote:

> Hi,
>
> I've created 3 plots one under the other, and want to include a legend on
> the right that spans the height of all 3 plots.
>
> How can I do this?
>
> Thanks!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Multiple-plots-with-one-legend-tp3408537p3408537.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple plots with one legend

mavkoup
Yes that's what I had managed to generate too. I can produce my 3 plots. Each plot has 10 colored lines say. I want to place the legend in the 4th spot listing the name of the 10 colored lines, and their color.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple plots with one legend

jholtman
Here is how to do the legend:

x <- cbind(rbind(1,2,3), 4)
layout(x, width = c(5,1))
layout.show(4)
plot(1:10, type = 'l')
plot(1:10, type = 'l')
plot(1:10, type = 'l')

# reset margins for creating the legend
oldMar <- par(mar = c(0,0,0,0))
plot.new()
legend('center'
    , legend = 1:10
    , lwd = 3
    , col = 1:10
    )
par(oldMar)


On Sun, Mar 27, 2011 at 12:43 AM, mavkoup <[hidden email]> wrote:

> Yes that's what I had managed to generate too. I can produce my 3 plots. Each
> plot has 10 colored lines say. I want to place the legend in the 4th spot
> listing the name of the 10 colored lines, and their color.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Multiple-plots-with-one-legend-tp3408537p3408850.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.