GGPLOT GEOM_ERRORBAR不在酒吧上?

发布于 2025-02-12 17:28:05 字数 851 浏览 0 评论 0原文

我的数据集看起来像这样:

df<-data.frame(Mean=c(56.0,63.8,46.9,48.5,39.8, 44.4, 51.9, 57.2, 40.5, 40.6),
              sd=c(24.0, 21.8, 24.4, 24.0, 21.0, 19.8, 22.4, 21.5, 20.5, 15.6),
              Quality=as.factor(c("P","M","P","M",
                                  "P","M", "P","M",
                                  "P","M")), 
              Category=c("A","B","C","D","E"),
              Insert= c(0.0, 0.1, 0.3, 0.5, 1.0))

绘图后,我得到以下图: “在此处输入图像说明”

基本上,误差条不在条上,而是在条之间显示。请让我知道如何纠正错误条,以便它们出现在酒吧上。

我使用以下代码:

p <- ggplot(df, aes(x=Category, y=Mean, fill=Quality)) +
  geom_bar(position=position_dodge(), stat="identity",
           colour='black') + 
  geom_errorbar(aes(ymin=Mean-sd, ymax=Mean+sd), width=.2)
print(p)

My dataset looks like this:

df<-data.frame(Mean=c(56.0,63.8,46.9,48.5,39.8, 44.4, 51.9, 57.2, 40.5, 40.6),
              sd=c(24.0, 21.8, 24.4, 24.0, 21.0, 19.8, 22.4, 21.5, 20.5, 15.6),
              Quality=as.factor(c("P","M","P","M",
                                  "P","M", "P","M",
                                  "P","M")), 
              Category=c("A","B","C","D","E"),
              Insert= c(0.0, 0.1, 0.3, 0.5, 1.0))

Upon plotting, I get the following figure:
enter image description here

Basically, the error bars are not on the bars but are presented between the bars. Kindly let me know how can I correct the error bars so that they appear on the bars.

I use the following code:

p <- ggplot(df, aes(x=Category, y=Mean, fill=Quality)) +
  geom_bar(position=position_dodge(), stat="identity",
           colour='black') + 
  geom_errorbar(aes(ymin=Mean-sd, ymax=Mean+sd), width=.2)
print(p)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2025-02-19 17:28:05

您还必须使用position_dodge(.9)来躲避错误栏: 。

library(ggplot2)

ggplot(df, aes(x=Category, y=Mean, fill=Quality)) +
  geom_bar(position=position_dodge(), stat="identity",
           colour='black') + 
  geom_errorbar(aes(ymin=Mean-sd, ymax=Mean+sd), width=.2, position=position_dodge(.9))

为了达到所需的结果,您还必须使用position = position_dodge(.9),其中.9指的是拒绝条形的默认宽度: “”

To achieve your desired result you also have to dodge the error bars by using position=position_dodge(.9) where the .9 refers to the default width by which the bars get dodged:

library(ggplot2)

ggplot(df, aes(x=Category, y=Mean, fill=Quality)) +
  geom_bar(position=position_dodge(), stat="identity",
           colour='black') + 
  geom_errorbar(aes(ymin=Mean-sd, ymax=Mean+sd), width=.2, position=position_dodge(.9))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文