[R] plotting lines on old graph after par(new=TRUE)

Boris Steipe boris.steipe at utoronto.ca
Wed May 7 20:50:37 CEST 2014


Since points() and lines() plot on the scale of the last plot, you could plot your "first" graph after the "second" one, or if there is a particular reason why you plot them in the order you do, add a third, empty graph with the scale of the first one as the last plot.

B.




On 2014-05-07, at 2:37 PM, Dustin Fife wrote:

> Hi,
> 
> In my work, I often investigate relationships between highly skewed data.
> Example:
> 
> set.seed(111)
> require(MASS)
> d = data.frame(mvrnorm(1000, mu=c(0,0), Sigma=matrix(c(1,.6,.6,1), nrow=2)))
> names(d) = c("x","y")
> ## Skew Y
> d$y = d$y^4
> plot(d$x, d$y)
> lines(lowess(d$x, d$y), lwd=2, col="blue")
> 
> Unfortunately, with such skewed data, it's hard to see the line unless we
> "zoom in," by either ignoring the outliers, or "breaking" the scale of the
> y axis, such that the first 2/3 of the graph correspond to a "normal"
> scale, and the remaining 1/3 correspond to a "compressed" scale. (I know
> this is generally not recommended, see
> http://r.789695.n4.nabble.com/split-a-y-axis-to-show-data-on-different-scales-td805816.html).
> I created a function to do this:
> 
> scaleBreak = function(x,y,axis=2, breakpos=1,...){
> 
>    #### figure out which Y values are above the breakpos
>    y_above = y[y>breakpos]; x_above = x[y>breakpos]
>    y_below = y[y<=breakpos]; x_below = x[y<=breakpos]
> 
>    #### pick ranges
>    range_1 = range(y_below)
>    range_2 = range(y_above)
> 
>    #### find limits of y axis (so it spans 2/3rds)
>    mx = max(y_below); mn = min(y_below)
>    ylims1 = c(mn, mx + (mx-mn)/2)
> 
>    #### plot bottom graph
>    plot(range(x), ylims1, type="n", yaxt="n",...)
>    points(x_below, y_below, yaxt="n",...)
>    axis(2, pretty(y_below))
> 
>    min2 = (breakpos + .45*breakpos - max(y))/.45
> 
>    #### add second graph
>    par(new=TRUE)
>    plot(range(x), c(min2, max(y)), type="n", yaxt="n", xaxt="n", ylab="",
> axes=F)
>    points(x_above, y_above, yaxt="n",...)
>    axis(2, pretty(y_above)[-1])
>    require(plotrix)
>    axis.break(axis=axis, breakpos)
> 
> }
> 
> The problem I'm having is that the fitted line is not printed on the scale
> of the bottom 2/3 plot:
> 
> scaleBreak(d$x, d$y, breakpos=20)
> lines(lowess(d$x, d$y), lwd=2, col="blue")
> 
> Can anyone think of a solution where the line is on the scale of the bottom
> 2/3 of the plot, in the right location? (Again, let me preempt the
> objections others might raise about how this should not be done. I know it
> generally should not be done, but let's pretend I have an excellent reason
> :) )
> 
> Thanks in advance!
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list