2014-03-29 45 views
0

我需要两个y轴数字。建议用hrbrmstr使用简单的地块。但调整图形到我的设置时,我发现我不能添加在右侧的ylab,得到一个有线错误:如何避免在绘制R时出现有线ylab误差

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

这是可以避免的? 看代码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") 
+0

“axis”的'labels'参数定义了要在刻度标记旁边打印的内容,而不是轴“名称”。你没有定义滴答的位置,这就是你得到这个错误的原因。 – Roland

+0

@Roland谢谢。我怎样才能避免这种情况?你可以提供最后一行'轴的代码(4,ylim = ...' – alex

+0

我不确定你在那里做什么,不要把一个值传递给'labels'?研究文档。 – Roland

回答

1

是这样的?

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") 

为了记录在案,这是从来没有堆叠在彼此顶部地块这种方式(两轴)是一个好主意。我已经将线条和轴线设置为相同的颜色,以试图提请注意您正在做的事情,但是这仍然是一个非常糟糕的主意

+0

@johoward非常感谢!我确实只需要这个堆积的情节。 :) 祝你今天愉快。 – alex

0

首先的底线,不建议在同一地块使用两个Y轴。

如果您将at参数添加到axis调用中,您将在图的右侧获得名称“精度”。

axis(4, ylim=c(0,1), col="black", col.axis="black", las=1, labels="Accuracy", 
    at = .5) 
+0

谢谢。好吧,但现在我没有规模。我需要用两个名为“精度”的y轴和从“0”到“1”的值来绘制这个图。这在技术上不可行吗? – alex

相关问题