2014-03-29 42 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

相關問題