[R] Plot 3 lines in one graph

Peter Alspach Peter.Alspach at plantandfood.co.nz
Tue Nov 6 05:41:22 CET 2012


Tena koe Ignacio

I cannot follow you example (you might care to read the posting guide, link at end, to help you in this regard).  However, the usual way to plot three lines in one graph is to use lines.  For example,

yourData <- data.frame(x=1:2, y1=runif(2), y2=runif(2), y3=runif(2))
with(yourData, plot(x, y1, ylim=range(unlist(yourData[,-1])), type='l'))
with(yourData, lines(x, y2, col='red3'))
with(yourData, lines(x, y3, col='blue2', lty='dashed'))

I hope this is of some help ...

Peter Alspach

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ignacio Martinez
Sent: Tuesday, 6 November 2012 12:10 p.m.
To: r-help
Subject: [R] Plot 3 lines in one graph

I'm new with R. I want to plot 3 lines in one graph. This is my data:

print(x)
     V1       V2       V3      V41 -4800 25195.73 7415.219 7264.282
-2800 15195.73 5415.219 7264.28

I tried using matplot, but I cannot get exactly what I want. This is what I get, and this is my code:

matplot(x[,1],x[,-1],type='b', xlab = "epsilon_h",
        ylab = "Value2", xlim= range(-4500,-100),
        col = c("blue","green","red"), pch=1:3)
ex12 <- expression(V(h == 40),
                   V(h==20),
                   V(h==0))
legend("topright", ex12, col = c("blue","green","red"), pch=1:3)

I would like to make the lines extend so I can see the intersections.


The other, fancier and better looking, option that i found is ggplot2. But for what I understand from the example<http://wiki.stdout.org/rcookbook/Graphs/Scatterplots%20(ggplot2)/>I
would need to reshape my data to something like this

     id       x       y      1     1     -4800   25195.73
2     1     -2800   15195.733     2     -4800   7415.2194     2
-2800   5415.2195     3     -4800   7264.286     3     -2800   7264.28

Thanks a lot for the help!

-Ignacio

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org 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.

The contents of this e-mail are confidential and may be ...{{dropped:14}}




More information about the R-help mailing list