[R] Plot 3 lines in one graph

David L Carlson dcarlson at tamu.edu
Tue Nov 6 06:16:50 CET 2012


matplot works just fine, but you only have two data points, at -4800 and at
-2800. You chop the x axis at -4500 so that the first point is outside the
graph window and extend it to -100 which is far beyond your point at -2800.
If you want to project the lines, you will have to add points. 

> x <- read.table(text="V1       V2       V3      V41
+  -4800 25195.73 7415.219 7264.282
+ -2800 15195.73 5415.219 7264.28",
+  header=TRUE)
> x
     V1       V2       V3      V41
1 -4800 25195.73 7415.219 7264.282
2 -2800 15195.73 5415.219 7264.280

Did you want to use the two points to compute a slopes and intercepts for
lines and then plot those lines?

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352



> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Peter Alspach
> Sent: Monday, November 05, 2012 10:41 PM
> To: Ignacio Martinez; r-help
> Subject: Re: [R] Plot 3 lines in one graph
> 
> 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}}
> 
> ______________________________________________
> 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.




More information about the R-help mailing list