开发者

Plotting axes with different scales for one data set in R

I have a large data set I am plotting in R, and I'd like to have an axis on each side of the graph show the data in two different scales. So for example, on the left vertical axis I'd like to plot the data directly (e.g. plot(y ~ x) ) and on the right axis, I'd like to have a linear scaling of the left axis. (e.g. plot( y*20 ~ x).

So there would only be one data set displayed, but the axes would show different meanings for those data points.

I've tried the following:

plot(x = dataset$x, y = dataset$y)
axis(4, pretty(dataset$y,10) )

This will correctly print a new right axis with the same scale as the default left axis. (essentially useless, but it works) However, if I make this tiny change:

plot(x = dataset$x, y = dataset$y)
axis(4, pretty(10*dataset$y,10) )

Suddenly, it refuses to add my new right axis. I suspect this has something to do with R seeing if the axis matches the data set in some way and rejecting it if n开发者_JAVA技巧ot. How can I get R to ignore the data set and just print an arbitrary axis of my choosing?


What you ask for is not always proper praxis, but you can force it via par(new=TRUE):

x <- 1:20
plot(x, log(x), type='l') 
par(new=TRUE)              # key: ask for new plot without erasing old
plot(x, sqrt(x), type='l', col='red', xlab="", yaxt="n")
axis(4)

The x-axsis is plotted twice, but as you have the same x-coordinates that is not problem. The second y-axis is suppressed and plotted on the right. But the labels show you that you are now mixing to different levels.


R doesn't seem to be rejecting your axes. What error are you getting? Your command will put ticks way off the graph (since it uses the first axis to position them). What I think you want is the following:

> plot(x = dataset$x, y = dataset$y)
> axis(4, at = axTicks(2), label = axTicks(2) * 10)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜