Combine plots with axis normalization
I use par(new=T)
before each of my plots to add my开发者_C百科 plot to the same graph.
However, when I do that it superimposes the two plots and the axis values get overwritten over each other and look messed up.
How do I properly add plot to the same graph that also normalizes axis intervals based on the two plots?
Use of par(new=TRUE) should be saved as a very last resort, usually there is a better/easier way. When creating the original plot set the xlim and ylim to include enough space for all the variables you will be plotting, then us functions like lines, points, symbols, or others to add the additional information: e.g.:
plot(x1,y1, xlim=range(x1,x2,x3), ylim=range(y1,y2,y3))
points(x2,y2, col='blue')
points(x3,y3, col='red')
There is also the matplot function which can plot several lines or sets of points in a single command.
Even better is to combine the data sets together then use xyplot from the lattice package or the ggplot2 package to do the multiple plots in one step.
There are also some functions in the plotrix package for combining graphs (with different scales as an option).
If you really need to use par(new=TRUE), then just specify the xlim and ylim in every plotting function to force them to line up. You can also supress the plotting of the default axes by specifying axes=FALSE or xaxt='n', yaxt='n', then, if wanted, you can use the axis function to put in axes on the other sides and can specify exactly where you want tick marks and labels.
Try ?lines
, ?points
, ?abline
, or ?plot.xy
.
精彩评论