0

I need a two y-axes figure. hrbrmstr suggested to use simple plots. But when adapting the graph to my setting I observed I cannot add the ylab on the right hand side, getting a wired error:

Error in axis(4, ylim = c(0, 1), col = "black", col.axis = "black", las = 1,  : 
'labels' is supplied and not 'at'

Is this avoidable? look at the code the bottom line fpr SOURCE OF ERROR

featPerf <- data.frame( expS=c("1", "2", "3", "4"),
                        exp1=c(1000, 0, 0, 0),
                        exp2=c(1000, 5000, 0, 0),
                        exp3=c(1000, 5000, 10000, 0),
                        exp4=c(1000, 5000, 10000,20000),
                        accuracy=c(0.4, 0.5, 0.65, 0.9) )

# make room for both axes ; adjust as necessary
par(mar=c(5, 5, 5, 7) + 0.2) 

# plot the bars first with no annotations and specify limits for y
#barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", ylim=c(0, max(colSums(featPerf[2:5]))))
barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", beside=TRUE)

# make the bounding box (or not...it might not make sense for your plot)
#box()

# now make the left axis
axis(2, ylim=c(0, max(colSums(featPerf[2:5]))), col="black", las=1)

# start a new plot
par(new=TRUE)

# plot the line; adjust lwd as necessary
plot(x=1:4, y=featPerf[,6], xlab="Experiments", ylab="Abs. # of Features", axes=FALSE, type="l", ylim=c(0,1), lwd=5)

# annotate the second axis -- SOURCE OF ERROR ->           VVVVVVVVVVVVVVVVVV
axis(4, ylim=c(0,1), col="black", col.axis="black", las=1, labels="Accuracy")
Community
  • 1
  • 1
alex
  • 1,103
  • 1
  • 14
  • 25
  • The `labels` argument of `axis` defines what to print next to the tick marks and not the axis "name". You didn't define the position of the ticks, which is why you get this error. – Roland Mar 29 '14 at 17:56
  • @Roland Thank you. How can I avoid this? Can you offer the code for the last line `axes(4, ylim=...` – alex Mar 29 '14 at 17:59
  • I'm not sure what you are trying to do there. Don't pass a value to `labels`? Study the documentation. – Roland Mar 29 '14 at 18:01
  • @Roland I tried to understand the problem here:http://www.statmethods.net/advgraphs/axes.html (go down to "Axes") However, they say that with `labels=`it is possible to put the `ylab` corresponding command, when creating the whole plot (which is here not directly possible since there are two Y-axes) – alex Mar 29 '14 at 18:04
  • 1
    I don't see where they say anything like that. In fact they show how to add a label to the right axis using `mtext`. – Roland Mar 29 '14 at 18:12
  • @Roland Thank you. I need to name the right hand Y-axe as "Accuracy". Do you know how this can be accomplished without getting the error? – alex Mar 29 '14 at 18:17
  • @Roland Thank you! I might follow your advise. But, then: If you want to help, you should do it, and not ring around the problem, by showing that some question is not formally perfect, while understanding it. It is what I interpreted, if I knew it better, be sure, I would do it. Have a nice day (this a honest wish). – alex Mar 29 '14 at 18:55

2 Answers2

1

Like this?

par(mar=c(4,4,1,4) + 0.2) 
barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", beside=TRUE)
axis(2, ylim=c(0, max(colSums(featPerf[2:5]))), col="black", las=1)
par(new=TRUE)
plot(x=1:4, y=featPerf[,6], xlab="Experiments", ylab="Abs. # of Features", axes=FALSE, type="l", ylim=c(0,1), lwd=5, col="blue")
axis(4, ylim=c(0,1), col="blue", col.axis="blue", las=1)
mtext("Accuracy",4,line=2, col="blue") 

For the record, it is never a good idea to stack plots on top of each other this way (with two axes). I've made the line and the axis the same color in an attempt to draw attention to what you are doing, but this is still a very bad idea.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
0

First of all it is not advisable to use two Y-axes in a same plot.

If you add at argument to the axis call, you get the name "Accuracy" on the right hand side of the plot.

axis(4, ylim=c(0,1), col="black", col.axis="black", las=1, labels="Accuracy",
     at = .5)
djhurio
  • 5,437
  • 4
  • 27
  • 48
  • Thank you. Ok, but now I have no scale. I need this plot with two y-axes with the name "Accuracy" and the values from `0` to `1`. Is this technically not possible? – alex Mar 29 '14 at 18:33