【问题标题】:x-axis label text size is not reduced while y-axis is reducedx 轴标签文本大小不会减小,而 y 轴会减小
【发布时间】:2019-03-24 05:46:51
【问题描述】:

我一直在尝试减小 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()

这是我得到的结果

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

【问题讨论】:

  • 请不要发布数据图像:它不能被复制或搜索 (SEO),它会破坏屏幕阅读器,并且它可能不适合某些移动设备。参考:meta.stackoverflow.com/a/285557/3358272(和xkcd.com/2116)。另一个建议:显着减少你的代码。如果您解决了三个条形图之一的问题,那么您将能够将其应用于所有其他人,所以请不要给我们不必要的代码。 (通常,如果它需要滚动,它可能太多了。并非总是如此,当然。但看到这么多代码确实会阻止许多人进一步阅读。)
  • 您好,感谢您的评论。因为我还想展示给定导出需求约束的问题。如果可能的话,所有三个图表的一个通用 x-lab。对于数据,我会修复。谢谢

标签: r resize axis


【解决方案1】:

您也可以在初始 par 调用中设置参数,而不是分别在每个条形图中设置参数,还可以设置 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() 

【讨论】:

    【解决方案2】:

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

    首先,我们将矩阵放入一个列表中,并在 lapply 中使用 barplot 以节省输入。

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

    结果

    但是,为了使解决方案起作用,我必须至少将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")))
    

    【讨论】:

    • 谢谢@jay.sf,但期刊要求图片的大小,我无法更改:(
    • 我明白了。我已经编辑了我的答案。纵横比不应该是决定性的而不是确切的尺寸吗?问你的日记。如果您使用pdf(.),您将获得可调整大小而不会损失任何质量的矢量图形。
    • 实际上看起来很不错。我能想到的另一个选择是将 x 轴的第二个值(场景)放在第一个值(HCchange)的下方。但我也没有做到这一点。你怎么看?
    • 可以肯定的是,这是否意味着在情节中灰色和深灰色条将改变侧面?在这种情况下,将矩阵放入列表后,执行L &lt;- lapply(L, function(x) x[2:1, ])。这有帮助吗?
    • 顺便说一句,如果我想将每个变量作为 x 轴的 1 行,我应该问另一个问题还是可以在这里解释?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2021-06-26
    相关资源
    最近更新 更多