简体   繁体   中英

How to shift bars from y-axis using barplot() R

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM