1

在很多情况下,我们需要证明标准误差。在ggplot2中,我们可以使用geom_errorbar函数来完成。我发现当 x 变量是 Date 类型时,ggplot2 无法完全绘制误差线。有关详细信息,请参阅下面的 R 脚本。

library(gcookbook) # For the data set
# Take a subset of the cabbage_exp data for this example
ce <- subset(cabbage_exp, Cultivar == "c39")
# With a line graph

p1 = ggplot(ce, aes(x=Date, y=Weight)) +
  geom_line(aes(group=1)) +
  geom_point(size=4) +
  geom_errorbar(aes(ymin=Weight-se, ymax=Weight+se), width=.2)

ce$Date = as.Date(c('01/01/2001', '01/01/2002', '01/01/2003'), "%m/%d/%Y") 

p2 = ggplot(ce, aes(x=Date, y=Weight)) +
  geom_line(aes(group=1)) +
  geom_point(size=4) +
  geom_errorbar(aes(ymin=Weight-se, ymax=Weight+se), width=.2)

p1
p2

在此处输入图像描述

4

1 回答 1

1

只需按照RHA指示(下面的代码)。@RHA,请随时将我的答案复制到一个新答案中,因为它是你的,而不是我的。

geom_errorbar 的宽度

# install.packages("gcookbook", dependencies = TRUE)
library(gcookbook) # For the data set
# Take a subset of the cabbage_exp data for this example
ce <- subset(cabbage_exp, Cultivar == "c39")
# With a line graph

# install.packages("ggplot2", dependencies = TRUE)
require(ggplot2)

ce$Date = as.Date(c('01/01/2001', '01/01/2002', '01/01/2003'), "%m/%d/%Y") 

(p2 = ggplot(ce, aes(x=Date, y=Weight)) +
  geom_line(aes(group=1)) +
  geom_point(size=4) +
  geom_errorbar(aes(ymin = Weight- se, ymax= Weight + se), width=45)))
于 2015-11-29T15:26:44.130 回答