gpt4 book ai didi

r - 使用条形图时如何在网格线顶部绘制条形图?

转载 作者:行者123 更新时间:2023-12-04 11:34:36 24 4
gpt4 key购买 nike

我正在使用 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)

最佳答案

我认为这可能属于:
“panel.first:在设置绘图轴之后但在任何绘图发生之前评估的'表达式'。这对于绘制背景网格或散点图平滑很有用。请注意,这是通过延迟评估工作的:通过来自其他绘图方法的这个论点很可能不起作用,因为它可能被评估得太早了"

因此,可能最好的方法是绘制 barplot分 3 个步骤:

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

关于r - 使用条形图时如何在网格线顶部绘制条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28171030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com