[R] Second y-axis --- alternative to par(new=

cls59 chuck at sharpsteen.net
Wed Oct 7 16:24:17 CEST 2009

Rainer M Krug-6 wrote:
> Thanks - but if I want to have the histogram with counts (freq=TRUE), then
> I have exactly the situation I was talking about - sorry for not being
> clear:
> x <- rnorm(200)
> hist(x, col = "blue", freq = TRUE)
> lines(density(x), col = "red", lwd = 2)
> i.e.
> left y-axis (histogram): count
> right y-axis (density estimate): probability density

When adding a second y-axis, I always rescale the plot with a call to
plot.window() rather than par(new=T). This rescales the plotting area while
still allowing low-level plotting functions like lines and points to be

x <- rnorm(200)
hist(x, col = "blue", freq = TRUE)

# recovering par('xaxp') uses the original x limits.
plot.window( xlim = range( par('xaxp')[1:2] ), ylim = range( density(x)$y ))

lines(density(x), col = "red", lwd = 2)

Hope this helps!


Charlie Sharpsteen
Environmental Resources Engineering
Humboldt State University
View this message in context: http://www.nabble.com/Second-y-axis-----alternative-to-par%28new%3D-tp25782532p25787630.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list