轴在R中的多个绘图中切断

时间:2013-06-18 21:09:21

标签: r plot axis

我使用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])

对于如何让轴回来的任何想法都表示赞赏!

1 个答案:

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