Whenever I run this code , the first plot would simply overwrite the previous one. Isnt there a way in R to separate to get two plots ?
plot(pc) title(main='abc',xlab='xx',ylab='yy') plot(pcs) title(main='sdf',xlab='sdf',ylab='xcv')
The par() function allows to set parameters to the plot. The mfrow() parameter allows to split the screen in several panels. Subsequent charts will be drawn in panels. You have to provide a vector of length 2 to mfrow() : number of rows and number of columns.
The par() function is used to set or query graphical parameters. We can divide the frame into the desired grid, add a margin to the plot or change the background color of the frame by using the par() function. We can use the par() function in R to create multiple plots at once.
To overlay a line plot in the R language, we use the lines() function. The lines() function is a generic function that overlays a line plot by taking coordinates from a data frame and joining the corresponding points with line segments.
To draw multiple curves in one plot, different functions are created separately and the curve() function is called repeatedly for each curve function. The call for every other curve() function except for the first one should have added an attribute set to TRUE so that multiple curves can be added to the same plot.
If you just want to see two different plotting windows open at the same time, use dev.new
, e.g.
plot(1:10) dev.new() plot(10:1)
If you want to draw two plots in the same window then, as Shane mentioned, set the mfrow
parameter.
par(mfrow = c(2,1)) plot(1:10) plot(10:1)
If you want to try something a little more advanced, then you can take a look at lattice graphics or ggplot, both of which are excellent for creating conditioned plots (plots where different subsets of data appear in different frames).
A lattice example:
library(lattice) dfr <- data.frame( x = rep(1:10, 2), y = c(1:10, 10:1), grp = rep(letters[1:2], each = 10) ) xyplot(y ~ x | grp, data = dfr)
A ggplot example. (You'll need to download ggplot from CRAN first.)
library(ggplot2) qplot(x, y, data = dfr, facets = grp ~ .) #or equivalently ggplot(dfr, aes(x, y)) + geom_point() + facet_grid(grp ~ .)
Try using par
before you plot.
par(mfrow = c(2, 1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With