Plot two graphs in same plot in R
I would like to plot y1 and y2 in the same plot.
x <- seq(-2, 2, 0.05) y1 <- pnorm(x) y2 <- pnorm(x, 1, 1) plot(x, y1, type = "l", col = "red") plot(x, y2, type = "l", col = "green")
But when I do it like this, they are not plotted in the same plot together.
In Matlab one can do
hold on, but does anyone know how to do this in R?
points() will add to the existing graph, but will not create a new window. So you'd need to do
Read more... Read less...
You can also use
par and plot on the same graph but different axis. Something as follows:
plot( x, y1, type="l", col="red" ) par(new=TRUE) plot( x, y2, type="l", col="green" )
If you read in detail about
R, you will be able to generate really interesting graphs. Another book to look at is Paul Murrel's R Graphics.
When constructing multilayer plots one should consider
ggplot package. The idea is to create a graphical object with basic aesthetics and enhance it incrementally.
ggplot style requires data to be packed in
# Data generation x <- seq(-2, 2, 0.05) y1 <- pnorm(x) y2 <- pnorm(x,1,1) df <- data.frame(x,y1,y2)
require(ggplot2) ggplot(df, aes(x)) + # basic graphical object geom_line(aes(y=y1), colour="red") + # first layer geom_line(aes(y=y2), colour="green") # second layer
+ operator is used to add extra layers to basic object.
ggplot you have access to graphical object on every stage of plotting. Say, usual step-by-step setup can look like this:
g <- ggplot(df, aes(x)) g <- g + geom_line(aes(y=y1), colour="red") g <- g + geom_line(aes(y=y2), colour="green") g
g produces the plot, and you can see it at every stage (well, after creation of at least one layer). Further enchantments of the plot are also made with created object. For example, we can add labels for axises:
g <- g + ylab("Y") + xlab("X") g
g looks like:
As pointed out in comments,
ggplot's philosophy suggests using data in long format.
You can refer to this answer in order to see the corresponding code.
I think that the answer you are looking for is:
plot(first thing to plot) plot(second thing to plot,add=TRUE)
use this if
y2 are evaluated at the same
x points. It scales the Y-axis to fit whichever is bigger (
y2), unlike some of the other answers here that will clip
y2 if it gets bigger than
y1 (ggplot solutions mostly are okay with this).
Alternatively, and if the two lines don't have the same x-coordinates, set the axis limits on the first plot and add:
x1 <- seq(-2, 2, 0.05) x2 <- seq(-3, 3, 0.05) y1 <- pnorm(x1) y2 <- pnorm(x2,1,1) plot(x1,y1,ylim=range(c(y1,y2)),xlim=range(c(x1,x2)), type="l",col="red") lines(x2,y2,col="green")
Am astonished this Q is 4 years old and nobody has mentioned
tl;dr: You want to use
I disagree with
par(new=TRUE) because that will double-print tick-marks and axis labels. Eg
The output of
plot(sin); par(new=T); plot( function(x) x**2 ).
Look how messed up the vertical axis labels are! Since the ranges are different you would need to set
ylim=c(lowest point between the two functions, highest point between the two functions), which is less easy than what I'm about to show you---and way less easy if you want to add not just two curves, but many.
What always confused me about plotting is the difference between
lines. (If you can't remember that these are the names of the two important plotting commands, just sing it.)
Here's the big difference between
curve will plot a function, like
lines plots points with x and y values, like:
lines( x=0:10, y=sin(0:10) ).
And here's a minor difference:
curve needs to be called with
add=TRUE for what you're trying to do, while
lines already assumes you're adding to an existing plot.
Here's the result of calling
Behind the scenes, check out
methods(plot). And check
body( plot.function )[]. When you call
plot(sin) R figures out that
sin is a function (not y values) and uses the
plot.function method, which ends up calling
curve is the tool meant to handle functions.