ggplot频数 - r条形图



在使用barplot时如何在网格线上绘制条形图? (2)

在设置轴之后,可以尝试将网格添加到图中,即删除panel.first参数并在添加轴之后添加grid(nx=NA, ny=NULL)

https://src-bin.com

我在barplot()使用grid() barplot() 。 因为我想要在网格顶部绘制条,我使用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)

Answer #1

我认为这可能会落在里面:
“panel.first:在绘制坐标轴之前,绘图之前进行评估的”表达式“,这对于绘制背景网格或散点图平滑是很有用的。 其他情节方法可能不起作用,因为它可能被评估得太早“

所以,最好的办法可能是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)