Hi, Is there a smart way in the R graphs to create a line that is broken in intervals based on the indicator given below. following is a small test graph Location,indicator,otherinfo 1.2,1,2.2 2.5,1,2.5 3.7,1,2.3 20.1,2,4.3 22.5,2,5.2 25.0,2,3.4 27.3,2,2.2 35.1,3,3.4 37.0,3,7.2 38.0,3,6.1 40.1,3,5.4 52.9,3,3.3 Right now in the plot the line is continuous, but I would like to have it broken based on the indicator. If the line of the plot reaches the last observation of indicator=1 then the line needs to stop; the next line will start at location 22.5 and continue up top 27.3; the next line goes from 35.1 up to 52.9. > x<-read.table(file='c:\\aldi\\testgraph.csv',sep=',',header=T) > x Location indicator otherinfo 1 1.2 1 2.2 2 2.5 1 2.5 3 3.7 1 2.3 4 20.1 2 4.3 5 22.5 2 5.2 6 25.0 2 3.4 7 27.3 2 2.2 8 35.1 3 3.4 9 37.0 3 7.2 10 38.0 3 6.1 11 40.1 3 5.4 12 52.9 3 3.3 > plot(x$Location,x$indicator,type='l',xlim=c(0,max(x$Location)),ylim=c(0,max(x$indicator,x$otherinfo))) > points(x$Location,x$otherinfo) TIA, Aldi --
If you insert an NA (or row of NA's) into the data at each place you want a break (after indicator increases), then the regular plot with type='l' will break the line for you. Is this what you want? -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Aldi Kraja > Sent: Wednesday, March 07, 2007 9:21 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Plotting a broken line? > > Hi, > > Is there a smart way in the R graphs to create a line that is > broken in intervals based on the indicator given below. > following is a small test graph > > Location,indicator,otherinfo > 1.2,1,2.2 > 2.5,1,2.5 > 3.7,1,2.3 > 20.1,2,4.3 > > 22.5,2,5.2 > 25.0,2,3.4 > 27.3,2,2.2 > > 35.1,3,3.4 > 37.0,3,7.2 > 38.0,3,6.1 > 40.1,3,5.4 > 52.9,3,3.3 > > Right now in the plot the line is continuous, but I would > like to have it broken based on the indicator. If the line of > the plot reaches the last observation of indicator=1 then the > line needs to stop; the next line will start at location 22.5 > and continue up top 27.3; the next line goes from 35.1 up to 52.9. > > > x<-read.table(file='c:\\aldi\\testgraph.csv',sep=',',header=T) > > x > Location indicator otherinfo > 1 1.2 1 2.2 > 2 2.5 1 2.5 > 3 3.7 1 2.3 > 4 20.1 2 4.3 > 5 22.5 2 5.2 > 6 25.0 2 3.4 > 7 27.3 2 2.2 > 8 35.1 3 3.4 > 9 37.0 3 7.2 > 10 38.0 3 6.1 > 11 40.1 3 5.4 > 12 52.9 3 3.3 > > > > plot(x$Location,x$indicator,type='l',xlim=c(0,max(x$Location)) > ,ylim=c(0,max(x$indicator,x$otherinfo))) > > points(x$Location,x$otherinfo) > > TIA, > Aldi > > -- > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Hi you shall probably cooperate with segments, so you need to extract start and end points for your lines e.g.> x<-c(1:6, 10:15,20:25) > y<-rep(c(1,2,3), each=6) > plot(x,y, type="l") > plot(x,y) >segments(sapply(split(x,y), min),1:3, sapply(split(x,y),max),1:3) Regards Petr On 7 Mar 2007 at 10:21, Aldi Kraja wrote: Date sent: Wed, 07 Mar 2007 10:21:06 -0600 From: Aldi Kraja <aldi at wustl.edu> To: r-help at stat.math.ethz.ch Subject: [R] Plotting a broken line? Send reply to: aldi at wustl.edu <mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe> <mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>> Hi, > > Is there a smart way in the R graphs to create a line that is broken > in intervals based on the indicator given below. following is a small > test graph > > Location,indicator,otherinfo > 1.2,1,2.2 > 2.5,1,2.5 > 3.7,1,2.3 > 20.1,2,4.3 > > 22.5,2,5.2 > 25.0,2,3.4 > 27.3,2,2.2 > > 35.1,3,3.4 > 37.0,3,7.2 > 38.0,3,6.1 > 40.1,3,5.4 > 52.9,3,3.3 > > Right now in the plot the line is continuous, but I would like to have > it broken based on the indicator. If the line of the plot reaches the > last observation of indicator=1 then the line needs to stop; the next > line will start at location 22.5 and continue up top 27.3; the next > line goes from 35.1 up to 52.9. > > > x<-read.table(file='c:\\aldi\\testgraph.csv',sep=',',header=T) > x > Location indicator otherinfo > 1 1.2 1 2.2 > 2 2.5 1 2.5 > 3 3.7 1 2.3 > 4 20.1 2 4.3 > 5 22.5 2 5.2 > 6 25.0 2 3.4 > 7 27.3 2 2.2 > 8 35.1 3 3.4 > 9 37.0 3 7.2 > 10 38.0 3 6.1 > 11 40.1 3 5.4 > 12 52.9 3 3.3 > > > > plot(x$Location,x$indicator,type='l',xlim=c(0,max(x$Location)),ylim=c( > 0,max(x$indicator,x$otherinfo))) > > points(x$Location,x$otherinfo) > > TIA, > Aldi > > -- > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Petr Pikal petr.pikal at precheza.cz
Hi Aldi, Yet another way to do what you want. 'd' is your data frame. You cannot easily modify the attributes of the lines however... # just to set up coordinates plot( d$Location, d$otherinfo, type="n") # plot the lines invisible(by(d, d$indicator, function(dd) lines(dd$Location, dd$otherinfo))) ********************************************************************************* *** Please note that my e-mail address has changed to m.j.bojanowski@uu.nl *** Please update you address books accordingly. Thank you! ********************************************************************************* ____________________________________ Michal Bojanowski ICS / Department of Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 m.j.bojanowski@uu.nl http://www.fss.uu.nl/soc/bojanowski/ -----Wiadomo¶æ oryginalna----- Od: r-help-bounces@stat.math.ethz.ch w imieniu Aldi Kraja Wys³ano: ¦r 2007-03-07 17:21 Do: r-help@stat.math.ethz.ch Temat: [R] Plotting a broken line? Hi, Is there a smart way in the R graphs to create a line that is broken in intervals based on the indicator given below. following is a small test graph Location,indicator,otherinfo 1.2,1,2.2 2.5,1,2.5 3.7,1,2.3 20.1,2,4.3 22.5,2,5.2 25.0,2,3.4 27.3,2,2.2 35.1,3,3.4 37.0,3,7.2 38.0,3,6.1 40.1,3,5.4 52.9,3,3.3 Right now in the plot the line is continuous, but I would like to have it broken based on the indicator. If the line of the plot reaches the last observation of indicator=1 then the line needs to stop; the next line will start at location 22.5 and continue up top 27.3; the next line goes from 35.1 up to 52.9. > x<-read.table(file='c:\\aldi\\testgraph.csv',sep=',',header=T) > x Location indicator otherinfo 1 1.2 1 2.2 2 2.5 1 2.5 3 3.7 1 2.3 4 20.1 2 4.3 5 22.5 2 5.2 6 25.0 2 3.4 7 27.3 2 2.2 8 35.1 3 3.4 9 37.0 3 7.2 10 38.0 3 6.1 11 40.1 3 5.4 12 52.9 3 3.3 > plot(x$Location,x$indicator,type='l',xlim=c(0,max(x$Location)),ylim=c(0,max(x$indicator,x$otherinfo))) > points(x$Location,x$otherinfo) TIA, Aldi -- ______________________________________________ R-help@stat.math.ethz.ch 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. [[alternative HTML version deleted]]