减少y轴时,不会减小x轴标签文本的大小

时间:2019-03-24 05:46:51

标签: r resize axis

我一直在尝试减小x轴上标签的大小,以使所有值都显示出来,但不知道为什么它不起作用。另外,我尝试使用mtext为所有三个图形仅放置一个x轴标签,但是它也不起作用。谁能帮忙吗? 这是名为Benefitlossr的数据

Region      ChangeNPV   ChangeNPVadjusted   ChangeHC    Scenario
Carabooda   13.47257941 7.430879051 0.1 S1-2
Carabooda   13.47151427 7.530120412 0.055   S2-3
Carabooda   14.83684617 8.940968276 0.056   S3-4
Carabooda   15.37691395 9.617533157 0.056   S4-5
Neerabup    3.499426472 2.232675752 0.01    S1-2
Neerabup    3.499596203 2.23966378  0.01    S2-3
Neerabup    3.836086106 2.566649186 0.01    S3-4
Neerabup    3.995114558 2.725839325 0.02    S4-5
Nowergup    3.513500149 1.700543633 0.02    S1-2
Nowergup    3.513585809 1.710386802 0.01    S2-3
Nowergup    3.850266108 2.034689127 0.02    S3-4
Nowergup    4.009112768 2.194350586 0.02    S4-5

这是我的代码

Caraboodaloss <- subset(Benefitlossr, Region=="Carabooda")
Neerabuploss <- subset(Benefitlossr, Region=="Neerabup")
Nowerguploss <- subset(Benefitlossr, Region=="Nowergup")

Caraboodaloss

tiff("barplot.tiff", width=130, height=50, units='mm', res=300)
par(mfrow=c(1,3))
par(mar=c(5, 4, 4, 0.2))

mxCarabooda <- t(as.matrix(Caraboodaloss[,2:3]))
Caraboodaloss$Label <- paste(Caraboodaloss$Scenario, Caraboodaloss$ChangeHC)
colnames(mxCarabooda) <- Caraboodaloss$Label
colours=c("gray63","gray87")

barplot(mxCarabooda, main='Carabooda', ylab='Profit loss ($m)',
        xlab='Change in water table at each level of GW cut', beside=TRUE, 
        col=colours, ylim=c(0,30),cex.lab=0.7, cex.sub=0.7, cex.axis=0.7)

legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), cex=0.6, pch=15)

mxNeerabup <- t(as.matrix(Neerabuploss[,2:3]))
Neerabuploss$Label <- paste(Neerabuploss$Scenario, Caraboodaloss$ChangeHC)
colnames(mxNeerabup) <- Neerabuploss$Label

colours=c("gray63","gray87")

barplot(mxNeerabup,main='Neerabup', ylab='', 
        xlab='Change in water table at each level of GW cut', beside=TRUE, 
        col=colours, ylim=c(0,30), cex.lab=0.7, cex.sub=0.7, cex.axis=0.7)
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), cex=0.6, pch=15)

mxNowergup <- t(as.matrix(Nowerguploss[,2:3]))

Nowerguploss$Label <- paste(Nowerguploss$Scenario,Nowerguploss$ChangeHC)
colnames(mxNowergup) <- Nowerguploss$Label
colours=c("gray63","gray87")

barplot(mxNowergup,main='Nowergup', ylab='', 
        xlab='Change in water table at each level of GW cut',beside=TRUE, 
        col=colours, ylim=c(0,30), cex.lab=0.7, cex.sub=0.7, cex.axis=0.7)
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), cex=0.6, pch=15)

dev.off()

这是我得到的结果

enter image description here

即使减小了y轴标签的尺寸,x轴标签也太大。结果,并非所有值都可以显示在轴上。如果可能的话,我希望在图中清楚地看到所有x轴的标签。我不能更改宽度和高度的大小,因为这是必需的。

2 个答案:

答案 0 :(得分:1)

您还可以仅在最初的par调用中设置参数,而不是分别在每个barplot中进行设置,还可以设置las=2以获得刻度标签的垂直方向。

tiff("barplot.tiff", width=130, height=50, units='mm', res=300)
par(mfrow=c(1,3))
par(mar=c(5, 4, 4, 0.2), cex.lab=0.5, cex.sub=0.7, cex.axis=0.5, las=2)

mxCarabooda <- t(as.matrix(Caraboodaloss[,2:3]))
Caraboodaloss$Label <- paste(Caraboodaloss$Scenario, Caraboodaloss$ChangeHC)
colnames(mxCarabooda) <- Caraboodaloss$Label
colours=c("gray63","gray87")

barplot(mxCarabooda, main='Carabooda', ylab='Profit loss ($m)',
        xlab='Change in water table at each level of GW cut', beside=TRUE, 
        col=colours, ylim=c(0,30))

legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), cex=0.6, pch=15)

mxNeerabup <- t(as.matrix(Neerabuploss[,2:3]))
Neerabuploss$Label <- paste(Neerabuploss$Scenario, Caraboodaloss$ChangeHC)
colnames(mxNeerabup) <- Neerabuploss$Label

colours=c("gray63","gray87")

barplot(mxNeerabup,main='Neerabup', ylab='', 
        xlab='Change in water table at each level of GW cut', beside=TRUE, 
        col=colours, ylim=c(0,30) )
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), cex=0.6, pch=15)

mxNowergup <- t(as.matrix(Nowerguploss[,2:3]))

Nowerguploss$Label <- paste(Nowerguploss$Scenario,Nowerguploss$ChangeHC)
colnames(mxNowergup) <- Nowerguploss$Label
colours=c("gray63","gray87")

barplot(mxNowergup,main='Nowergup', ylab='', 
        xlab='Change in water table at each level of GW cut',beside=TRUE, 
        col=colours, ylim=c(0,30) )
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), cex=0.6, pch=15)

dev.off() 

enter image description here

答案 1 :(得分:0)

使用mtext获取全局轴标签是一个好主意。您也可以通过图例执行此操作。对于刻度线标签,我建议将它们旋转90°。因此,整体可读性应该有所提高。 (甚至可以比我提供的解决方案更好地进行微调。)

首先,我们将矩阵放入列表中,并在barplot中使用lapply来保存输入。

L <- list(mxCarabooda=mxCarabooda, mxNeerabup=mxNeerabup, mxNowergup=mxNeerabup)

par选项中,我们包括oma以扩大外部边距,以及xpd以便将文本和图例放置在任何地方。在barplot中,我们关闭x轴并手动添加它们。通过设置compression="lzw"使用lossless compression

tiff("barplot.tiff", width=260, height=100, units='mm', res=300, compression="lzw")
par(mfrow=c(1,3), mar=c(5, 4, 4, 2) + 0.1, oma=c(6, 4, 0, 0), xpd=TRUE)

lapply(seq_along(L), function(x) {
  b1 <- barplot(L[[x]], main=gsub("^mx", "", names(L)[x]), beside=TRUE, xaxt="n",
                col=c("gray63","gray87"), ylim=c(0,30))
  axis(1, at=apply(b1, 2, mean), labels=colnames(L[[x]]), 
       tick=FALSE, line=FALSE, las=2)
})

mtext('Change in water table at each level of GW cut', side=1, outer=TRUE, line=1)  # x-lab
mtext('Profit loss ($m)', side=2, outer=TRUE, line=1)                               # y-lab
legend(-16.5, -15, bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), cex=1.2, pch=15, horiz=TRUE, xpd=NA)           

dev.off()

结果

enter image description here

但是,为了使解决方案起作用,我必须至少将tiff大小增加一倍,因为图形边距变得太大。但是,长宽比保持不变,这应该更重要。您可以用日记检查一下吗?也许您也可以使用pdf("barplot.pdf", width=13, height=5)代替tiff(.)行。


数据

mxCarabooda <- structure(c(13.47258, 7.430879, 13.47151, 7.53012, 14.83685, 
                           8.940968, 15.37691, 9.617533), .Dim = c(2L, 4L), .Dimnames = list(
                             c("ChangeNP", "ChangeNP.1"), c("Sl-2 0.1", "S2-3 0.055", 
                                                            "S3-4 0.056", "S4-5 0.056")))
mxNeerabup <- structure(c(3.499426, 2.232676, 3.499596, 2.239664, 3.836086, 
                          2.566649, 3.995115, 2.725839), .Dim = c(2L, 4L), .Dimnames = list(
                            c("ChangeNP", "ChangeNP.1"), c("Sl-2 0.1", "S2-3 0.055", 
                                                           "S3-4 0.056", "S4-5 0.056")))

mxNowergup <- structure(c(3.5135, 1.700544, 3.513586, 1.710387, 3.850266, 2.034689, 
                          4.009113, 2.194351), .Dim = c(2L, 4L), .Dimnames = list(
                            c("ChangeNP", "ChangeNP.1"), c("Sl-2 0.02", "S2-3 0.01", 
                                                           "S3-4 0.02", "S4-5 0.02")))