【问题标题】:Width of error bars in line plot using ggplot2使用ggplot2的线图中误差线的宽度
【发布时间】:2020-09-05 02:11:29
【问题描述】:

我有一些与标准错误相关的数据,并希望用错误栏显示这些数据。这就是我所拥有的:

# generate some data
hod <- data.frame(h = c(1:24,1:24,1:24), mean = 1:(24*3) + runif(24*3, 0, 5),ci = runif(24*3, 0, 2), t = c(rep("a",24),rep("b",24),rep("c",24)))

pd <- position_dodge(0.3)
  dayplot <- ggplot(hod, aes(x=h, y=mean, colour=as.factor(t),group=as.factor(t))) + 
    geom_line(position=pd, size=1) +
    geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci),
                  width=1,
                  size=0.5,
                  position=pd) +
    geom_point(position=pd, shape=21, size=1, fill="white") +
    scale_x_continuous(limits=c(-0.5,23.5),
                       breaks=c(0:8*3),
                       labels=ifelse(
                              c(0:8*3) < 10,
                              paste('0',c(0:8*3),':00',sep=''),
                              paste(c(0:8*3),':00',sep='')
                              )
                       ) +
    xlab("Hour of day") + ylab(ylabel) + labs(title = varlabels[var]) +
    theme_minimal() + 
    theme(plot.margin = unit(c(1,0,1,1), "cm"), 
          axis.title.x = element_text(vjust=-1),
          axis.title.y = element_text(angle=90, vjust=0),
          legend.margin = unit(c(0), "cm"),
          legend.key.height = unit(c(0.9), "cm"),
          panel.grid.major = element_line(colour=rgb(0.87,0.87,0.87)),
          panel.grid.minor = element_blank(),
          plot.background = element_rect(fill = rgb(0.97,0.97,0.97), linetype=0)
    )

唯一感兴趣的可能是:

geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci),
                      width=1,
                      size=0.5,
                      position=pd)

它给出:

现在,当我按因子变量 (as.factor(t)) 对数据进行分组时,我得到了几行而不是一行,这正是我想要的,但是,正如你所看到的,误差条上的水平线更窄,我不知道为什么。我尝试更改甚至删除geom_errorbarwidthsize 属性,但没有任何反应。无论数据如何,有没有办法让每个图表的水平线宽度相同?我的意思是,为什么它会有所不同?或者这个宽度是否传达了一些信息?

【问题讨论】:

  • 我认为如果有人可以向我解释widthsize 的用途和功能对于geom_errorbar 来说已经有所帮助,因为它应该与数据无关,因此与问题无关。
  • 我试图用一个简化的例子重现这个问题,但不能。这并不明显,出了什么问题。如果您提供数据,则更容易找到问题。
  • 我也有同样的问题。你解决了吗?
  • 没有。但我没有向@Roland 提供任何数据。

标签: r charts ggplot2


【解决方案1】:

以下是使用随机数据的可重现示例。该问题的解决方法是将宽度乘以您拥有的类/因子的数量。在下图中,由于我使用了三个因子,因此使用 3 的宽度可以解决问题。 ggplot2 似乎是通过数据集中数据点的数量来计算相对宽度,而不是 x 轴上的数值。这是(IMO)一个错误。

library(ggplot2)
library(grid)

#plot with factors
hod <- data.frame(h = c(1:24,1:24,1:24), mean = 1:(24*3) + runif(24*3, 0, 5),ci = runif(24*3, 0, 2), t = c(rep("a",24),rep("b",24),rep("c",24)))
pd <- position_dodge(0.3)
  dayplot <- ggplot(hod, aes(x=h, y=mean, colour=as.factor(t),group=as.factor(t))) + 

    geom_line(position=pd, size=1) +
    geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci),
                  width=1,
                  size=0.5,
                  position=pd) +
    geom_point(position=pd, shape=21, size=1, fill="white") +
    scale_x_continuous(limits=c(-0.5,23.5),
                       breaks=c(0:8*3),
                       labels=ifelse(
                              c(0:8*3) < 10,
                              paste('0',c(0:8*3),':00',sep=''),
                              paste(c(0:8*3),':00',sep='')
                              )
                       ) +
    xlab("Hour of day") +
    theme_minimal() + 
    theme(plot.margin = unit(c(1,0,1,1), "cm"), 
          axis.title.x = element_text(vjust=-1),
          axis.title.y = element_text(angle=90, vjust=0),
          legend.margin = unit(c(0), "cm"),
          legend.key.height = unit(c(0.9), "cm"),
          panel.grid.major = element_line(colour=rgb(0.87,0.87,0.87)),
          panel.grid.minor = element_blank(),
          plot.background = element_rect(fill = rgb(0.97,0.97,0.97), linetype=0)
    )
print(dayplot)


#plot without factors
hod <- data.frame(h = c(1:24,1:24,1:24), mean = 1:(24) + runif(24, 0, 5),ci = runif(24, 0, 2))
pd <- position_dodge(0.3)
  dayplot <- ggplot(hod, aes(x=h, y=mean)) + 

    geom_line(position=pd, size=1) +
    geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci),
                  width=1,
                  size=0.5,
                  position=pd) +
    geom_point(position=pd, shape=21, size=1, fill="white") +
    scale_x_continuous(limits=c(-0.5,23.5),
                       breaks=c(0:8*3),
                       labels=ifelse(
                              c(0:8*3) < 10,
                              paste('0',c(0:8*3),':00',sep=''),
                              paste(c(0:8*3),':00',sep='')
                              )
                       ) +
    xlab("Hour of day") +
    theme_minimal() + 
    theme(plot.margin = unit(c(1,0,1,1), "cm"), 
          axis.title.x = element_text(vjust=-1),
          axis.title.y = element_text(angle=90, vjust=0),
          legend.margin = unit(c(0), "cm"),
          legend.key.height = unit(c(0.9), "cm"),
          panel.grid.major = element_line(colour=rgb(0.87,0.87,0.87)),
          panel.grid.minor = element_blank(),
          plot.background = element_rect(fill = rgb(0.97,0.97,0.97), linetype=0)
    )

print(dayplot)

【讨论】:

  • 现在,与我的示例有何不同?据我所知,您仍在使用width = 1。你的意思是你应该使用width = 3
  • 是的。试试你自己的例子,宽度 = 9(或者你有多少层)。
  • 谢谢。我在 GitHub github.com/hadley/ggplot2/issues/1068 上发布了一个问题
  • 对于我们中的许多人来说,这个问题似乎仍未解决,因为该领域的标准不是按元素数量来缩放误差条宽度。我知道 ggplot 开发人员认为这是一个功能而不是错误,但我们的同事/期刊不同意。 @hadley,帮忙?谢谢!
  • @curiouslabrat 这绝对是一个错误。查看 grssnbchr 的 github 问题,也许可以用一个简单的代表重新打开。例如。
【解决方案2】:

我已经设法解决了一个类似的问题。在我的例子中,我想将水平和垂直误差条头设置为相同的大小 - 无论绘图的纵横比如何。

根据原贴代码:

f <- ggplot_build(dayplot)

f$plot$layers[[5]]$geom_params$width  <- 0.02 * diff(f$layout$panel_params[[1]]$x.range)
f$plot$layers[[6]]$geom_params$height <- 0.02 * diff(f$layout$panel_params[[1]]$y.range)

dayplot <- f$plot

这会将误差条头设置为轴范围的 2%。也许可以解决您的问题。

【讨论】:

    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多