0

I have have 4 linear equations. I used ?curve with par(plot=new) but it overwrites the lines instead of plotting parallel lines.

y<-2*x+1
y<-2*x+3
y<-(-2)*x+1
y<-(-2)*x+3

How can i plot these four lines on single plot to see the parallelogram on plot.?

logicstar
  • 197
  • 4
  • 16
  • Have you already checked [this](http://stackoverflow.com/questions/14860078/plot-multiple-lines-data-series-each-with-unique-color-in-r) and all of [these](http://stackoverflow.com/search?q=%5Br%5D+plot+multiple+lines)? – Pierre L Oct 29 '15 at 12:10
  • There is a `add` argument in `curve`. –  Oct 29 '15 at 12:10

3 Answers3

1

Is this maybe what you're looking for? I just randomly generated X-values here.

set.seed(42);x<-rnorm(100)

plot(x,2*x+1,type="l",xlab="X",ylab="Y")
lines(x,2*x+3)
lines(x,(-2)*x+1)
lines(x,(-2)*x+3)

enter image description here

horseoftheyear
  • 917
  • 11
  • 23
  • It works but when i use my data there only one line appears. a1<-61569627.5 b1<-1601 a2<-61569713.25 b2<-2139.5 a3<-61569843.25 b3<-2032.5 a4<-61569872.75 b4<-1820.5 m2<-(b3-b1)/(a3-a1) m4<-m2 m1<-(b4-b2)/(a4-a2) m3<-m1 set.seed(42); x<-1:10; plot(x,m1*(x-a1)+b1,type="l",xlab="X",ylab="Y") lines(x,m2*(x-a2)+b2) lines(x,m3*(x-a3)+b3) lines(x,m4*(x-a4)+b4) – logicstar Oct 29 '15 at 12:21
  • In order to include the other lines you need to set the `ylim` to include all the range of generated values by your models. As is shown in the answer below for instance. Note that in the data you gave in your comment some lines seem to be parallel and don't cross. – horseoftheyear Oct 29 '15 at 12:39
1

Or you can add xlim and ylim

limy=c(-4,4)
limx=c(-4,4)
curve(y<-2*x+1,xlim=limx,ylim=limy,ylab="")
par(new=T)
curve(y<-2*x+3,xlim=limx,ylim=limy,ylab="")
par(new=T)
curve(y<-(-2)*x+1,xlim=limx,ylim=limy,ylab="")
par(new=T)
curve(y<-(-2)*x+3,xlim=limx,ylim=limy,ylab="")
Batanichek
  • 7,761
  • 31
  • 49
1

With ggplot you could do something similar

x <- rnorm(100*1,mean=0,sd=1)
ggplot() + geom_line(aes(x,2*x+1)) + geom_line(aes(x,2*x+3)) etc
admccurdy
  • 694
  • 3
  • 11