Rilcon42
Rilcon42

Reputation: 9763

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_up<-data.frame(cutoff=c(1:10),percentage_accuracy=seq(.1,1,by=.1))
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)
plot(x=df_down$cutoff,y=df_down$percentage_accuracy,axes=F,xlab=NA,ylab=NA,col="red",type="p")
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

Upvotes: 1

Views: 1096

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73345

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

par("mar")
# [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

Upvotes: 2

Related Questions