Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
3 views
in R Programming by (3.5k points)
edited by

How can I plot two graphs in same plot in R, I am using this command to perform my task but it isn’t working.

    x <- seq(-3, 4, 1.05)

    y1 <- pnorm(x)

    y2 <- pnorm(x, 2, 2)

    plot(x, y1, type = "2", col = "blue")

    plot(x, y2, type = "l", col = "yellow")

Please help me with it.

1 Answer

0 votes
by (46k points)
edited by

There are several methods to perform this task I am sharing a few with you:

Method-1:

Use par, it will plot on the same graph but axis will be different. E.x.

plot( x, y1, type="l", col="blue" )

par(new=TRUE)

plot( x, y2, type="l", col="yellow" )

Method-2:

You can add lines() or points() to the existing graph but it won’t create a new window.

plot(x,y1,type="l",col="blue")

lines(x,y2,col="yellow")

Method-3:

Use matplot function, e.x.

matplot(x, cbind(y1,y2),type="l",col=c("blue","yellow"),lty=c(1,1))

There are some other methods too, I hope one of this help you with your issue.

Related questions

0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer
asked May 29, 2019 in R Programming by Ritik (3.5k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...