0

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Lara
  • 73
  • 1
  • 8
  • Please do not post an image of data: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557/3358272 (and https://xkcd.com/2116/). Another suggestion: reduce your code significantly. If you solve your issue for one of the three bar plots, then you will be able to apply it to all others, so please do not give us unnecessary code. (Often, if it needs to scroll, it may be too much. Not always, for sure. But seeing that much code does deter many from even reading further.) – r2evans Mar 24 '19 at 06:46
  • Hi thank you for your comment. Cos I also want to show the problem given constraint on the exported requirement. And if possible the one common x-lab for all three graphs. For data, I'll fix. Thanks – Lara Mar 24 '19 at 07:13

2 Answers2

1

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

IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

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")))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thanks @jay.sf, but the size of the figure is required by the journal and I cannot change that :( – Lara Mar 24 '19 at 09:43
  • I see. I've edited my answer. Shouldn't the aspect ratio be decisive instead of the exact dimensions? Ask your journal. If you use `pdf(.)` you get a vector graphic that is resizable without any loss of quality. – jay.sf Mar 24 '19 at 12:37
  • That actually looks very good. Another option that I can think of is to put the second value (scenario) of the x-axis below the first value (HCchange). But I also fail to do that. What do you think? – Lara Mar 24 '19 at 12:57
  • To be sure, would this imply that in the plot the grey and darkgrey bars will change sides? In this case, after putting matrices into the list, do `L <- lapply(L, function(x) x[2:1, ])`. Does this help? – jay.sf Mar 24 '19 at 13:15
  • By the way if I want to put each variable as 1 row in x-axis should I ask another question or can be explained here? Thanks – Lara Mar 24 '19 at 14:48