Combine plots with axis normalization - r

I use par(new=T) before each of my plots to add my 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.

Related

Multiple Axis with Plots.jl

Is there a way to have the second dataset plot on a separate axis, overlaid on the first plot?
using Plots; gadfly(size=(800,400))
plot(Vector[randn(100)], line = ([:green], :step))
plot!(Vector[randn(100)], line = ([:red], :step))
It is now done by adding a twinx() argument:
plot(rand(10))
plot!(twinx(),100rand(10))
There is, however, some unintentional axis and label behavior:
Labels are plotted on top of each other by default
xticks are plotted on top of the already existing plot
Default colors are not correlated to the total number of series in the subplot
Therefore, I suggest adding some additional arguments:
plot(rand(10),label="left",legend=:topleft)
plot!(twinx(),100rand(10),color=:red,xticks=:none,label="right")
There still seems to be an issue correlating all series associated with the subplot at the moment.
It's easy, but doesn't work with Gadfly. It should work fine with PyPlot and GR. Here's an example:
I can confirm (for GR) using Plots; gr()

How to draw abline() that doesn't intersect the Y-axis in R?

I have a location quotient plot drawn in R and want to draw a horizontal line along the plot where Y = 1. I have the code abline(h=1, col="black") but when the line is drawn, it intersects the Y-axis and crosses out my Y-axis labels.
Does anyone know how to terminate the line at the Y-axis rather than having it intersect?
Many thanks.
As mentioned in the comments, it looks like the parameter xpd has been changed, so one option is to change it back to FALSE, see ?par. you can control the clipping region using the clip function to further limit the range that abline and other functions plot within. This may also be affected by you plotting device (different devices can deal with clipping differently).

Add stripplot points to bwplot (Lattice graphics in R)

##Example data to illustrate problem:
type=c("grp1","grp2","grp1","grp3","grp3","grp3","grp4")
num=c(1,1,2,4,3,5,1)
cols=c(rep("red",5),"green","red")
library(lattice)
bwplot(num~type)
par(new=T)
stripplot(num~type,col=cols)
I like the additional information displayed by the box plot but I need the information conveyed by the coloured points in the strip chart. Obviously par(new=T) doesn't work, so how can I overlay the points onto the box plot?
You can define a panel function like this :
library(lattice)
bwplot(num~type,panel=function(x,y,...){
panel.bwplot(x,y,...)
panel.stripplot(x,y,col=cols,...)
})
the answer by agstudy is quite good, though as I pointed out in a comment, any outliers plotted by bwplot will also be plotted by stripplot, which could get confusing.
Note this is only an issue if you were using jitter.data=TRUE as an option to stripplot, otherwise the duplicate points would plot directly over top of one another and you'd never know they were there. If the data had outliers, and you used jitter.data=TRUE, you'd see twice as many outliers as you should.
the following approach suppresses the outliers from bwplot to avoid this issue:
bwstrip <- function(x,y,...){
panel.stripplot(x,y,col=cols,do.out=FALSE,jitter.data=TRUE,...)
panel.bwplot(x,y,...)
}
bwplot(lobe.depths,panel=bwstrip)

R: Suppress axes in addtable2plot{plotrix}

I am trying to add a table to a plot with addtable2plot(). But I am running into a peculiar problem, viz. it's overriding the options in plot(). If I do:
plot(seq(1:10), axes=F)
the axes labels, ticks etc. are not printed. But if I then add:
addtable2plot(x=.8*max(axis(1)), y=.7*max(axis(2)), data.frame(matrix(1:9, nrow=3)))
the axes etc. get printed automatically.
that's because you call axis(). Use axTicks(1) instead.

Horizontal grid not matching y axis ticks

I have the following data:
x=c(2.880262,3.405859,3.613575,3.744480,3.682059,3.694075,3.758320,4.034290,4.202741,4.309383,4.996279,5.981309,5.103148,4.926363,4.696024,5.522913,5.330382,4.434304,5.154567,6.247156,8.612752,9.996526,9.606994,10.303496,5.954970,5.688171,6.340349,6.252854,6.355642,5.988570,7.317148,11.664384,14.231579,16.489029,23.100640,20.280043,21.562793,24.311327,23.735198,23.796386,23.118181,23.269722,19.886981,20.000975,19.967642,24.278910,17.447721,14.536114,20.646378,19.096832,20.258060,19.803196)
y=1:52
w=c(-2784,-2897,-2897,-2066,-2466,-2466,-2466,-2466,-2102,-2102,-2102,-2202,-2094,-2094,-2094,-2094,-1691,-1691,-1691,-1691,-1691,-1674,-1774,-1774,-2019,-2019,-2019,-2019,-2019,-1988,-1988,-1988,-1988,-1988,-1888,-1888,-1888,-1888,-1888,-1888,-1888,-1488,-2051,-2051,-2051,-2051,-2315,-2315,-2315)
v=1:49
When I try to plot these, my grid does not match the tick marks. Is there a way to fix this in base?
plot(y,x,type='l',col='blue',log='y')
grid(NA,NULL)
Resulting plot:
And the other plot:
plot(v,w,type='l',yaxt='n')
grid(NA,NULL)
axis(2,pretty(w),format(pretty(w)/1000,big.mark=','))
Result:
I put both up because I am using different techniques to label the y axis, and one is a log chart while the other is not. By the way, I have hundreds of other data sets that are placing the grid lines by the tick marks. It is just these two that are not matching grids to ticks.
For the first plot, just use equilogs=F.
For the second plot, since you are using non-default axis ticks, I think you'll have to resort to abline like it says in ?grid. Good luck!

Resources