3

I have a barplot with the following code:

bp <- barplot(COL0.matrix,
    beside=T,
    col=col,
    ylim=c(0,100), yaxt="n",
    xlab="Time",ylab="Relative Electrolyte Leakage (%)",
    las=1,xaxt = "n",
    cex.axis=1.5, cex.names= 1.5, font=2, font.lab=2, cex.lab=1.5, family="A", space=c(0,0,1,0), xaxs = 'i')
axis(side=2, family="A", cex.axis=0.8, las=1, font=2, pos=0, tck=c(0), at=c(0,10,20,30,40,50,60,70,80,90,100), labels=c("0", "10","20","30","40","50","60","70","80","90","100"))
axis(side=2, at=c(0,10,20,30,40,50,60,70,80,90,100), labels = c(NA),tcl=c(-0.25),pos=0)
axis(side=2, at=c(0,10,20,30,40,50,60,70,80,90,100), labels = c(NA),tcl=c(0.25),pos=0)
axis(side=1, at=c(1.2, 4.2), labels = c("Dawn", "Dusk"),tck=c(0), family="A", cex.axis=1.5, font=2, pos=0)

This results in the following barplot: Barplot resulting from the above code

I am trying to shift the bars which are right next to the y-axis away. I have tried changing space=(...) but this shifts the whole x-axis so that the x and y axis no longer join.

Is there a way of shifting the left two bars over?

GW97
  • 91
  • 1
  • 11
  • 1
    Can you `dput(COL0.matrix)`? Or give some similar structured data? – trosendal Dec 22 '18 at 11:23
  • 1
    I guess its something like this: `COL0.matrix <- matrix(c(70,80,60,70), ncol = 2)` – trosendal Dec 22 '18 at 11:25
  • @trosendal this is the result: structure(c(71.44109964, 78.43178612, 64.31581642, 70.3339388 ), .Dim = c(2L, 2L), .Dimnames = list(c("Control", "bold(\"Col-0 840g ha\"^\"-1\")" ), c("Dawn", "Dusk"))) – GW97 Dec 22 '18 at 11:30
  • 2
    By the way, it is nice to see someone using base R to make beautiful plots :) – trosendal Dec 22 '18 at 11:49
  • @trosendal that's very helpful thank you! I'm trying to keep the x- and y- axis connected while shifting the bars along the axis. The line=... separates the axis. Do you know of a way to shift the bars without separating the axis? – GW97 Dec 22 '18 at 12:32
  • 1
    Just edited to fix this – trosendal Dec 22 '18 at 12:39
  • @trosendal Thank you so much, that's really helpful!! :) – GW97 Dec 22 '18 at 12:54

1 Answers1

1

You can use the line parameter to move the axis over instead of moving the bars. You want to remove the pos = 0 and define the y title outside the barplot function so you can also control its position. Also you will want to play with the par(mar = ... part so it looks right for your device. For if you save in a pdf device your margin and even the cex parameters probably will need adjusting to make it nice. Also I set the graphics parameter xpd = TRUE to allow the lines function in the last line to plot into the margin space. If you don't do that you'll have a x axis that doesn't meet the y axis. If you don't want that then remove the last line.

COL0.matrix <-  structure(c(71.44109964, 78.43178612, 64.31581642, 70.3339388 ), .Dim = c(2L, 2L), .Dimnames = list(c("Control", "bold(\"Col-0 840g ha\"^\"-1\")" ), c("Dawn", "Dusk")))
col = c("white", "grey70", "white", "grey70")
par(mar = c(5,7,5,5), xpd = TRUE)
bp <- barplot(COL0.matrix,
              beside=T,
              col=col,
              ylim=c(0,100), yaxt="n",
              xlab="Time", ylab = "",
              las=1,xaxt = "n",
              cex.axis=1.5,
              cex.names= 1.5,
              font=2,
              font.lab=2,
              cex.lab=1.5,
              family="A",
              space=c(0,0,1,0),
              xaxs = 'i')

mtext("Relative Electrolyte Leakage (%)", side = 2, font = 2, cex = 1.5, line = 4)

axis(side=2, family="A", cex.axis=0.8,
     las=1, font=2, tck=c(0),
     at=c(0,10,20,30,40,50,60,70,80,90,100),
     labels=c("0", "10","20","30","40","50","60","70","80","90","100"),
     line = 1)
axis(side=2, at=c(0,10,20,30,40,50,60,70,80,90,100), labels = c(NA),tcl=c(-0.25), line = 1)
axis(side=2, at=c(0,10,20,30,40,50,60,70,80,90,100), labels = c(NA),tcl=c(0.25), line = 1)
axis(side=1, at=c(1.2, 4.2), labels = c("Dawn", "Dusk"),tck=c(0), family="A", cex.axis=1.5, font=2, line = 0)
lines(x = c(-0.3, 5.3), y = c(0, 0))
trosendal
  • 1,225
  • 9
  • 14