Building axes with different scales for one dataset in R

I have a large dataset that I draw in R, and I would like the axis on each side of the graph to display data at two different scales. So, for example, on the left vertical axis, I would like to directly plot the data (for example, a graph (y ~ x)) and on the right axis, I would like to have linear scaling of the left axis. (e.g. graph (y * 20 ~ x).

Thus, only one data set will be displayed, but the axes will show different values ​​for these data points.

I tried the following:

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

This correctly prints the new right axis at 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 he refuses to add my new right axis. I suspect that this is due to the fact that R sees that the axis corresponds to a specific data set and rejects it if not. How can I make R ignore a dataset and just print an arbitrary axis of my choice?

+4
source share
2 answers

R does not seem to deflect your axes. What error are you getting? Your team will place ticks on the chart (since the first axis is used to place them). I think you want the following:

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

What you ask for does not always match the rule, but you can force it through 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) 

x-axsis is displayed twice, but since you have the same x-coordinates, which are not a problem. The second Y axis is suppressed and plotted to the right. But these shortcuts show that you are now mixing different levels.

+7
source

Source: https://habr.com/ru/post/1301882/


All Articles