使用条形图时如何在网格线顶部绘制条形图?
How plot bars on top of grid lines when using barplot?
我在 barplot()
中使用 grid()
。因为我希望将条形图绘制在网格顶部,所以我使用了 panel.first
参数。但是,网格线与 y 轴的刻度不匹配。谁能告诉我如何解决这个问题?我的代码如下,谢谢。
WRE <- c(1423, 41721)
bp <- barplot(WRE, xaxt = 'n', xlab = '', yaxt = 'n', las = 3, width = 0.5,
space = 1.5, main = paste("How You Compare", sep = ""), ylab = "",
names.arg = NA, col = c("red","blue"), cex.lab = 1,
panel.first = grid(nx = NA, ny = NULL))
axis(1, at=bp, labels=c("Average Claims", "Your Claims"),
tick=FALSE, las=1, line=-1, cex.axis=1)
axis(2, at=axTicks(2), sprintf("$%s", formatC(axTicks(2), digits=9, big.mark=",", width=-1)),
cex.axis=0.9, las=2)
您可以尝试在设置轴后将网格添加到绘图中,即删除 panel.first
参数并在添加轴后添加 grid(nx=NA, ny=NULL)
我认为这可能落在里面:
"panel.first: an ‘expression’ to be evaluated after the plot axes are set up but before any plotting takes place. This can be useful for drawing background grids or scatterplot smooths. Note that this works by lazy evaluation: passing this argument from other plot methods may well not work since it may be evaluated too early"
所以,最好的方法可能是分 3 个步骤绘制 barplot
:
# Draw the barplot
bp <- barplot(WRE, xaxt = 'n',xlab = '',yaxt = 'n',las = 3, width =0.5,space = 1.5, main=paste("How You Compare", sep=""), ylab="", names.arg=NA, col=c("red","blue"), cex.lab=1)
# add the grid
grid(nx=NA, ny=NULL)
# redraw the bars...
barplot(WRE, xaxt = 'n',xlab = '',yaxt = 'n',las = 3, width =0.5,space = 1.5, ylab="", names.arg=NA, col=c("red","blue"), cex.lab=1, add=T)
# Then you can add the axes
axis(1, at=bp, labels=c("Average Claims", "Your Claims"), tick=FALSE, las=1, line=-1, cex.axis=1)
axis(2, at=axTicks(2), sprintf("$%s", formatC(axTicks(2), digits=9, big.mark=",", width=-1)), cex.axis=0.9, las=2)
我在 barplot()
中使用 grid()
。因为我希望将条形图绘制在网格顶部,所以我使用了 panel.first
参数。但是,网格线与 y 轴的刻度不匹配。谁能告诉我如何解决这个问题?我的代码如下,谢谢。
WRE <- c(1423, 41721)
bp <- barplot(WRE, xaxt = 'n', xlab = '', yaxt = 'n', las = 3, width = 0.5,
space = 1.5, main = paste("How You Compare", sep = ""), ylab = "",
names.arg = NA, col = c("red","blue"), cex.lab = 1,
panel.first = grid(nx = NA, ny = NULL))
axis(1, at=bp, labels=c("Average Claims", "Your Claims"),
tick=FALSE, las=1, line=-1, cex.axis=1)
axis(2, at=axTicks(2), sprintf("$%s", formatC(axTicks(2), digits=9, big.mark=",", width=-1)),
cex.axis=0.9, las=2)
您可以尝试在设置轴后将网格添加到绘图中,即删除 panel.first
参数并在添加轴后添加 grid(nx=NA, ny=NULL)
我认为这可能落在里面:
"panel.first: an ‘expression’ to be evaluated after the plot axes are set up but before any plotting takes place. This can be useful for drawing background grids or scatterplot smooths. Note that this works by lazy evaluation: passing this argument from other plot methods may well not work since it may be evaluated too early"
所以,最好的方法可能是分 3 个步骤绘制 barplot
:
# Draw the barplot
bp <- barplot(WRE, xaxt = 'n',xlab = '',yaxt = 'n',las = 3, width =0.5,space = 1.5, main=paste("How You Compare", sep=""), ylab="", names.arg=NA, col=c("red","blue"), cex.lab=1)
# add the grid
grid(nx=NA, ny=NULL)
# redraw the bars...
barplot(WRE, xaxt = 'n',xlab = '',yaxt = 'n',las = 3, width =0.5,space = 1.5, ylab="", names.arg=NA, col=c("red","blue"), cex.lab=1, add=T)
# Then you can add the axes
axis(1, at=bp, labels=c("Average Claims", "Your Claims"), tick=FALSE, las=1, line=-1, cex.axis=1)
axis(2, at=axTicks(2), sprintf("$%s", formatC(axTicks(2), digits=9, big.mark=",", width=-1)), cex.axis=0.9, las=2)