14

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

# 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)现在,当我按因子变量(找出原因。我尝试更改甚至删除 的widthandsize属性geom_errorbar,但没有任何反应。无论数据如何,有没有办法让每个图表的水平线宽度相同?我的意思是,为什么它会有所不同?或者这个宽度是否传达了一些信息?

在此处输入图像描述

4

2 回答 2

15

下面是一个使用随机数据的可重现示例。该问题的解决方法是将宽度乘以您拥有的类/因子的数量。在下图中,由于我使用了三个因子,因此使用 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)
于 2015-03-15T10:30:35.000 回答
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%。也许可以解决你的问题。

于 2018-12-20T15:22:42.097 回答