【发布时间】:2019-05-03 22:54:03
【问题描述】:
请多多包涵,因为我对 R 很陌生,尤其是对 ggplot() 很陌生。这是我第二次询问有关使用此功能的问题。我想创建一个类似于下面的图:
为此目的,我一直在使用类似这样的脚本:
df <- tibble::tribble(~Proportion, ~Error, ~Time,
0.351 , 0.154, "Day",
0.223 , 0.157 , "Night")
dfnew <- df %>%
mutate(ymin = Proportion - Error,
ymax = Proportion + Error)
p <- ggplot(data = dfnew, aes(x = Time, y = Proportion)) +
geom_point() + geom_line(aes(group = 1), color="lightblue",size=2) +
geom_errorbar(aes(x = Time, ymin = ymin, ymax = ymax),color="purple",width=0.1,size=1)
p<-p+theme(axis.text=element_text(size=12),
axis.title=element_text(size=14))
但是,我现在面临的问题是,置信区间的数据包括上限和下限,而不是上面脚本中的错误值。我的数据如下所示:
> df
# A tibble: 2 x 4
Average Lower Upper Time
<dbl> <dbl> <dbl> <chr>
1 0.351 0.284 0.421 Day
2 0.223 0.178 0.275 Night
知道如何将这两个 Lower 和 Upper 值实现到错误栏中吗?
感谢任何输入!
【问题讨论】:
-
只需通过
aes()传递相应的列,例如geom_errorbar(aes(y= Average, ymin = Lower, ymax = Upper))? -
@Nate 是的。我只是担心警告:我怎样才能删除它?
> dfnew <- df %>% + mutate(ymin = Proportion - Lower, + ymax = Proportion + Upper) > p <- ggplot(data = dfnew, aes(x = Time, y = Proportion)) + + geom_point() + geom_line(aes(group = 1), color="lightblue",size=2) + + geom_errorbar(aes(y= Proportion, ymin = Lower, ymax = Upper),color="purple",width=0.1,size=1) Warning: Ignoring unknown aesthetics: y > p<-p+theme(axis.text=element_text(size=12), + axis.title=element_text(size=14)) -
geom_errorbar()抱怨y = Average我说你应该包括(我的 b)。误差条只想知道高低,所以它忽略了 y。删除y =应该会删除警告 -
@如果我这样做,那么我会留下一个错误栏不在它们应该在的位置的情节。您应该能够使用我发布的示例复制错误