在R中绘图时如何避免有线ylab错误

时间:2014-03-29 17:50:27

标签: r plot axis-labels

我需要一个两个y轴的数字。 hrbrmstr建议使用简单的图表。但是当我将图形调整到我的设置时,我观察到我无法在右侧添加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")

2 个答案:

答案 0 :(得分: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") 

对于记录来说,以这种方式(使用两个轴)将图形堆叠在一起是永远一个好主意。为了引起人们对你正在做的事情的注意,我把线和轴做成了相同的颜色,但这仍然是一个非常糟糕的主意

答案 1 :(得分:0)

首先,不建议在同一个图中使用两个Y轴。

如果您在at电话中添加axis参数,则会得到名称&#34; Accuracy&#34;在情节的右侧。

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