【问题标题】:error bars are aligned incorrectly with columns误差线与列不正确对齐
【发布时间】:2022-01-05 20:46:43
【问题描述】:

有人可以帮我解决这个问题吗?误差线未与列正确对齐。

 ggplot (data=nitrate_meso00, aes(x = Date, 
                               y = mean_rr, 
                               fill = treatment,
                               ymin = lci_rr+mean_rr,
                               ymax =uci_rr+ mean_rr)) +
  theme_bw() + 
  theme(panel.grid = element_blank ()) +
  # manually setting the width means we will have to tell geom_errorbar() about the new width
  geom_bar(stat = "identity", position = "dodge", width = 8) + 
  theme(legend.title = element_blank()) +
  theme(legend.position = "right")+
  geom_errorbar(width = 8,position = position_dodge(0.5)) 

【问题讨论】:

  • 您好,您可能需要发布您的数据以供人们帮助。尝试使用dput(nitrate_meso00) 或其他方法来提供您的结构。
  • 这能回答你的问题吗? ggplot2 - Bars non-aligned to error bars
  • 我认为 OP 在最初的 ggplot(aes()) 调用中使用 fill = treatment 实现了上述问题中的解决方案。我认为剩下的问题是条形图和误差线与中心的相对位移(如下所示)。非常方便注意,尽管将它们分组确实可以正确排列躲避的元素。

标签: r ggplot2


【解决方案1】:

我认为position_dodge 中的宽度需要等于您的条形宽度才能匹配位移?

这是一个使用完全模拟数据的示例 - 如上所述,如果您可以发布自己的数据样本,则可以测试这是否是导致您的问题的原因。

library(tidyverse)

tibble(x = rep(1:10, each = 2),
       a = rep(c("a", "b"), 10),
       y = rnorm(20, 20, 5)) %>%
  group_by(a) %>%
  mutate(sd = sd(y),
         ymin = y - sd,
         ymax = y + sd) %>%
  ggplot(aes(
    x,
    y,
    fill = a,
    ymin = ymin,
    ymax = ymax
  )) +
  geom_bar(stat = "identity",
           position = "dodge",
           width = 0.75) +
  theme(legend.title = element_blank()) +
  theme(legend.position = "right") +
  geom_errorbar(width = 0.5, position = position_dodge(width = 0.75))

在此示例中,将 geom_barposition_dodgewidth 参数设置为 0.75 将它们对齐。误差线宽度与这些无关。

reprex package 创建于 2022-01-05 (v2.0.1)

【讨论】:

    猜你喜欢
    • 2017-08-03
    • 2018-12-23
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    相关资源
    最近更新 更多