2

我正在使用 layout() 在 R 中创建一个多图图形,并且只希望 y 轴显示在底部图中。因此,我将它们从循环中取出并将它们与轴一起添加到下方,但是当我这样做时,它们会被切断。这是我正在使用的代码:

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

任何关于如何恢复该轴的想法都值得赞赏!

4

1 回答 1

2

我无法让ylim你的代码部分工作,因为你没有提供scorecard_means_tabor的信息endorsement_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)
    }
于 2013-06-18T21:29:45.263 回答