Lara
Lara

Reputation: 73

x-axis label text size is not reduced while y-axis is reduced

I've been trying to reduce the size of the labels in x-axis so that all the values appear, but don't know why it doesn't work. Also I try to use mtext to put only one x-axis label for all three graphs but it also didn't work. Could anyone please help? Here is the data named 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

This is my code

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

this is the result I get

enter image description here

x-axis labels are too big even though y-axis label size is reduced. As the result, not all values can be displayed in the axis. If possible, I would like all the labels of x-axis to be clearly seen in the graph. I can't change the size of width and height because it's required.

Upvotes: 0

Views: 234

Answers (2)

IRTFM
IRTFM

Reputation: 263301

You could also have set parameters just in the initial par call instead of in each barplot separately and also set las=2 to get perpendicular orientation of tick labels.

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

Upvotes: 1

jay.sf
jay.sf

Reputation: 72593

To use mtext to get global axis labels is a good idea. You could also do this with the legend. For the tick labels I'd recommend to turn them by 90°. Therefore, the overall readability should have improved. (Could be even better fine-tuned than my solution provided.)

First we put the matrices into a list and use barplot in a lapply to save typing.

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

Into the par options we include oma to enlarge outer margins, and xpd to place text and legends anywhere. In the barplot we switch off the x axis and add them manually. Use lossless compression by setting compression="lzw".

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

Result

enter image description here

However, for the solution to work, I had to at least double the size of the tiff size because the figure margins get too large. However, the aspect ratio remains the same, which should be more important. Could you check that with your journal? Maybe you could also use pdf("barplot.pdf", width=13, height=5) instead of the tiff(.) line.


Data

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

Upvotes: 0

Related Questions