[英]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: 这导致以下条形图:
I am trying to shift the bars which are right next to the y-axis away. 我正试图将靠近y轴的条移开。 I have tried changing space=(...) but this shifts the whole x-axis so that the x and y axis no longer join.
我试过改变空间=(...),但这会移动整个x轴,使x和y轴不再连接。
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. 您可以使用
line
参数将轴移动而不是移动条。 You want to remove the pos = 0
and define the y title outside the barplot
function so you can also control its position. 你想删除
pos = 0
并在barplot
函数之外定义y标题,这样你也可以控制它的位置。 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. 你也想玩
par(mar = ...
部分,因此它看起来适合你的设备。如果你保存在pdf
设备中,你的余量甚至cex
参数可能需要调整以使它变得漂亮。而且我设置图形参数xpd = TRUE
以允许最后一行中的lines
功能绘制到边距空间。如果不这样做,你将使ax轴不符合y轴。如果你不这样做想要那么删除最后一行。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.