Rilcon42 Rilcon42 - 1 year ago 81
R Question

Axis label not appearing when adding a y-axis on the right side

can someone explain why the right hand axis label is not appearing?

df_down<-data.frame(cutoff=c(2:11),percentage_accuracy=runif(10, 0, 1))

plot(x=df_up$cutoff,y=df_up$percentage_accuracy, main="RSI predicting trend",ylab=NA,xlab="RSI cutoff value",col="blue",type="p")
mtext(side = 2, line = 3, "% successful upward predictions")
par(new = T)
axis(side = 4)
mtext(side = 4, line = 3, '% successful downward predictions') #THIS DOESNT APPEAR
legend("top",legend=c("% successful upward predictions", '% successful downward predictions'), pch=c(1,1),col=c("blue", "red"))

enter image description here

Answer Source

You need to set right margin of your graphical device. The default margins are:

# [1] 5.1 4.1 4.1 2.1

You see that right side is 2.1 while the left side is 4.1. If you want two y-axis on both sides, set both margins the same.

new_par <- old_par <- par("mar")
new_par[4] <- old_par[2]
par(mar = new_par)

## your code, unchanged

par(mar = old_par)

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download