Pergunta

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")
Foi útil?

Solução

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.

Outras dicas

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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top