2013-06-18 43 views
2

我正在使用layout()在R中創建多個陰謀圖並且只想要y軸顯示在底部陰謀圖中。因此,我把它們從循環中移出,並將它們添加到軸的下面,但是當我這樣做時,它們會被切斷。這裏是我正在使用的代碼:軸在R中的多個陰謀圖中切斷

onlytext <- function(string){ 
    plot(0:1, 0:1, bty='n', type='n', xaxt='n', yaxt='n', xlab='', ylab='') 
    text(0.5, 0.5, string, cex=1.2, font=2) 
} 
nf <- layout(matrix(c(0,1:14), 5, 3, byrow=TRUE), heights = c(2,5,5,5,5), widths = c(1.5,4,4)) 

par (mar=c(.3,.3,.3,.3)) 
onlytext ('Approval'); onlytext ('Vote Choice') 
name_vec <- c("Strongly Approve", "Approve", "Disapprove", "Strongly Disapprove")  
control_means_tab <- matrix(sample(rnorm(100), 48), ncol = 6, nrow = 4) 
x <- seq(1,6) 

for(i in 1:3){ 
onlytext(name_vec[i])  
    for(j in 1:2){   

     plot(x, control_means_tab[j,], xaxt = 'n', xlab = '', ylab = '', xaxt='n', bty = "n", ylim = c((min(c(control_means_tab, scorecard_means_tab, endorsement_means_tab))- .02),(max(c(control_means_tab, scorecard_means_tab, endorsement_means_tab)) + .02)), col = "blue", pch = 19, cex =.6) 


} 
} 

onlytext(name_vec[4])  

     plot(x, control_means_tab[j,], xlab = '', ylab = '', xaxt = "n", bty = "n", ylim = c((min(c(control_means_tab, scorecard_means_tab, endorsement_means_tab))- .02),(max(c(control_means_tab, scorecard_means_tab, endorsement_means_tab)) + .02)), col = "blue", pch = 19, cex =.6) 
     axis(1, 1:6, LETTERS[1:6]) 


     plot(x, control_means_tab[j,], xaxt = 'n', xlab = '', ylab = '', xaxt='n', bty = "n", ylim = c((min(c(control_means_tab, scorecard_means_tab, endorsement_means_tab))- .02),(max(c(control_means_tab, scorecard_means_tab, endorsement_means_tab)) + .02)), col = "blue", pch = 19, cex =.6) 
        axis(1, 1:6, LETTERS[1:6]) 

有關如何獲得該軸的任何想法是讚賞!

+0

您的代碼不會爲我運行。此外,我不明白爲什麼佈局()矩陣有14個地塊的空間(0表示沒有繪製在該位置),但有8個地塊(循環中6個,外部2個)。 – rbatt

回答

2

我不能讓你的代碼ylim一部分工作,因爲你沒有提供scorecard_means_tabendorsement_means_tab的信息,但我想我看到你以後,一般。試試這段代碼,看看它是否有幫助。

x <- seq(1, 6) 
y <- matrix(sample(rnorm(100), 48), ncol=6, nrow=8) 
ylabs <- rep(c("Strongly Approve", "Approve", "Disapprove", "Strongly Disapprove"), rep(2, 4)) 
xlabs <- rep(c("Approval", "Vote Choice"), 4) 
par(mfrow=c(4, 2), mar=c(1, 1, 1, 1), oma=c(2, 4, 2, 1)) 
for(i in seq(dim(y)[1])) { 
    if(i < 6.5) { 
     plot(x, y[i, ], xaxt='n', xlab='', ylab='', bty="n", col="blue", pch=19, cex=0.6) 
     } else { 
     plot(x, y[i, ], xlab='', ylab='', bty="n", col="blue", pch=19, cex=0.6) 
     } 
    if(i < 2.5) mtext(xlabs[i], side=3, line=1) 
    if(i %in% c(1, 3, 5, 7)) mtext(ylabs[i], side=2, line=3) 
    } 
+0

謝謝@JVA;改變代碼以包含oma參數適用於我(mar = c(.3,.3,.3,.3),oma = c(3,.3,.3,.3)) –