Wayne Aldo Gavioli
2007-Sep-21 01:57 UTC
[R] Line Graph - Greater than 2 variables on plot
Hello all, I was wondering if anyone knew how to construct a multiple line graph on R, where there are 2 (or more) sets of data points plotted against some x axis of data, and you can draw a line on the graph connecting each set of data points. For example: A B C D 0.6566 2.1185 1.2320 5 0.647 2.0865 1.2325 10 0.6532 2.1060 1.2287 15 0.6487 2.1290 1.2313 20 0.6594 2.1285 1.2341 25 0.6577 2.1070 1.2343 30 0.6579 2.1345 1.2340 35 0.6734 2.1705 1.2362 40 0.675 2.1845 1.2372 45 0.6592 2.1550 1.2340 50 0.6647 2.1710 1.2305 55 Would there be a way: a) To graph all the points of data in sets A, B and C as Y coordinates on one graph, using the points in set D as the X-axis/coordinates for all 3 sets (A, B and C)? b) To be able to draw 3 lines on the graph that connect each set of data (1 line connects all the A points, one line connects all the B points, one line connects all the C points) I couldn't find anything in the examples or the help section about multiple lines on the same graph, only one line. Thanks, Wayne
On 21/09/2007, at 1:57 PM, Wayne Aldo Gavioli wrote:> > > Hello all, > > I was wondering if anyone knew how to construct a multiple line > graph on R, > where there are 2 (or more) sets of data points plotted against > some x axis of > data, and you can draw a line on the graph connecting each set of > data points. > > For example: > > A B C D > 0.6566 2.1185 1.2320 5 > 0.647 2.0865 1.2325 10 > 0.6532 2.1060 1.2287 15 > 0.6487 2.1290 1.2313 20 > 0.6594 2.1285 1.2341 25 > 0.6577 2.1070 1.2343 30 > 0.6579 2.1345 1.2340 35 > 0.6734 2.1705 1.2362 40 > 0.675 2.1845 1.2372 45 > 0.6592 2.1550 1.2340 50 > 0.6647 2.1710 1.2305 55 > > > > Would there be a way: > a) To graph all the points of data in sets A, B and C as Y > coordinates on one > graph, using the points in set D as the X-axis/coordinates for all > 3 sets (A, B > and C)? > b) To be able to draw 3 lines on the graph that connect each set of > data (1 line > connects all the A points, one line connects all the B points, one > line connects > all the C points) > > > I couldn't find anything in the examples or the help section about > multiple > lines on the same graph, only one line.?points ?lines ?matplot ###################################################################### Attention:\ This e-mail message is privileged and confidenti...{{dropped}}
This should do it for you:> x <- read.table(textConnection("A B C D+ 0.6566 2.1185 1.2320 5 + 0.647 2.0865 1.2325 10 + 0.6532 2.1060 1.2287 15 + 0.6487 2.1290 1.2313 20 + 0.6594 2.1285 1.2341 25 + 0.6577 2.1070 1.2343 30 + 0.6579 2.1345 1.2340 35 + 0.6734 2.1705 1.2362 40 + 0.675 2.1845 1.2372 45 + 0.6592 2.1550 1.2340 50 + 0.6647 2.1710 1.2305 55"), header=TRUE)> > matplot(x[, 4], x[, -4], type='o') >On 9/20/07, Wayne Aldo Gavioli <wgavioli at fas.harvard.edu> wrote:> > > Hello all, > > I was wondering if anyone knew how to construct a multiple line graph on R, > where there are 2 (or more) sets of data points plotted against some x axis of > data, and you can draw a line on the graph connecting each set of data points. > > For example: > > A B C D > 0.6566 2.1185 1.2320 5 > 0.647 2.0865 1.2325 10 > 0.6532 2.1060 1.2287 15 > 0.6487 2.1290 1.2313 20 > 0.6594 2.1285 1.2341 25 > 0.6577 2.1070 1.2343 30 > 0.6579 2.1345 1.2340 35 > 0.6734 2.1705 1.2362 40 > 0.675 2.1845 1.2372 45 > 0.6592 2.1550 1.2340 50 > 0.6647 2.1710 1.2305 55 > > > > Would there be a way: > a) To graph all the points of data in sets A, B and C as Y coordinates on one > graph, using the points in set D as the X-axis/coordinates for all 3 sets (A, B > and C)? > b) To be able to draw 3 lines on the graph that connect each set of data (1 line > connects all the A points, one line connects all the B points, one line connects > all the C points) > > > I couldn't find anything in the examples or the help section about multiple > lines on the same graph, only one line. > > > Thanks, > > > > Wayne > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Wayne Aldo Gavioli wrote:> > Hello all, > > I was wondering if anyone knew how to construct a multiple line graph on R, > where there are 2 (or more) sets of data points plotted against some x axis of > data, and you can draw a line on the graph connecting each set of data points. > > For example: > > A B C D > 0.6566 2.1185 1.2320 5 > 0.647 2.0865 1.2325 10 > 0.6532 2.1060 1.2287 15 > 0.6487 2.1290 1.2313 20 > 0.6594 2.1285 1.2341 25 > 0.6577 2.1070 1.2343 30 > 0.6579 2.1345 1.2340 35 > 0.6734 2.1705 1.2362 40 > 0.675 2.1845 1.2372 45 > 0.6592 2.1550 1.2340 50 > 0.6647 2.1710 1.2305 55 > > > > Would there be a way: > a) To graph all the points of data in sets A, B and C as Y coordinates on one > graph, using the points in set D as the X-axis/coordinates for all 3 sets (A, B > and C)? > b) To be able to draw 3 lines on the graph that connect each set of data (1 line > connects all the A points, one line connects all the B points, one line connects > all the C points) > > > I couldn't find anything in the examples or the help section about multiple > lines on the same graph, only one line. >Hi Wayne, Assume your data is in a data frame named "wag": plot(wag$D,wag$A,main="Three variable plot",xlab="D",ylab="Value", ylim=range(wag[c("A","B","C")]),type="l",col=2) lines(wag$D,wag$B,type="l",pch=2,col=3) lines(wag$D,wag$C,type="l",pch=3,col=4) legend(25,1.9,c("A","B","C"),lty=1,col=2:4) Jim