2016-08-23 73 views
0

我疊置兩個欄地塊與基地圖形(因爲他們有不同的尺度,到目前爲止,它一直是一個惡夢做它ggplot)以下數據相同:XLIM與x軸不barplot

> str(pidf) 
'data.frame': 1300 obs. of 7 variables: 
$ spec1 : num 0.00192 0.00213 0.00151 0.00234 0.00133 ... 
$ spec2 : num 0.00241 0.00278 0.00189 0.00264 0.00155 ... 
$ spec3 : num 0.00231 0.00262 0.00187 0.00274 0.00165 ... 
$ spec4 : num 0.00209 0.0026 0.00166 0.00225 0.00165 ... 
$ spec5 : num 0.0025 0.00285 0.00188 0.00285 0.00181 ... 
$ numbers: int 1 2 3 4 5 6 7 8 9 10 ... 
$ ratios : num 0.59 0.642 0.544 0.625 0.567 ... 

這是我的陰謀代碼:

par(mar = c(5,5,2,5)) 
barplot(pidf$ratios, col = "skyblue", ylim = c(0, 1), 
ylab = "Sequenced Base Ratios", border = NA, main = "Chlorocebus tantalus") 
axis(1, at = round(seq(0, 1300, 100))) 
par(new = T) 
barplot(pidf$spec5, col = "chartreuse3", border = NA, ylim = c(0, 0.003), 
    axes = F, xlab = NA, ylab = NA) 
axis(side = 4) 
mtext(side = 4, line = 3, expression(pi)) 
legend("topright", legend=c(expression(pi), "Ratios"), cex = 0.75, 
    pch=c(15, 15), col=c("chartreuse3", "skyblue")) 

當我運行這段代碼x軸比數據短 如果我在第一個情節指定xlim = c(1,1300),然後我得到這個

有人知道爲什麼會發生這種情況,以及如何解決它?我想這可能與平價值有關,但我需要這些以便爲我在右邊繪製的軸留出空間。提前致謝!

回答

0

我認爲問題在於棒不會自動以單位間隔定位。默認情況下,barplot會在條之間添加0.2個單位的間隔(請參見?barplot,搜索「空間」)。例如,如果我們有20條,x軸的右邊緣爲x = 24:

z <- 1:20 
barplot(z) 
axis(side=1,at=c(0,24)) 

enter image description here

對於您的應用程序,它可能最容易通過指定space=0,以適應這一點:

barplot(z,space=0) 
axis(side=1,at=c(0,20)) 

enter image description here

+0

這樣一個簡單的修復哈哈,謝謝! –