【问题标题】:Plotting a ggplot() when having the confidence interval upper and lower values在具有置信区间上限和下限时绘制 ggplot()
【发布时间】: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

知道如何将这两个 LowerUpper 值实现到错误栏中吗?

感谢任何输入!

【问题讨论】:

  • 只需通过aes() 传递相应的列,例如geom_errorbar(aes(y= Average, ymin = Lower, ymax = Upper))?
  • @Nate 是的。我只是担心警告:我怎样才能删除它? &gt; dfnew &lt;- df %&gt;% + mutate(ymin = Proportion - Lower, + ymax = Proportion + Upper) &gt; p &lt;- 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 &gt; p&lt;-p+theme(axis.text=element_text(size=12), + axis.title=element_text(size=14))
  • geom_errorbar() 抱怨 y = Average 我说你应该包括(我的 b)。误差条只想知道高低,所以它忽略了 y。删除 y = 应该会删除警告
  • @如果我这样做,那么我会留下一个错误栏不在它们应该在的位置的情节。您应该能够使用我发布的示例复制错误

标签: r ggplot2


【解决方案1】:

这有帮助吗?

使用新数据:

df2 <- read.table(
  header = T,
  stringsAsFactors = F,
  text = "  Average Lower Upper Time
1   0.351 0.284 0.421 Day  
2   0.223 0.178 0.275 Night"
)

并对绘图数据源稍作调整:

ggplot(data = df2, aes(x = Time, y = Average)) +
  geom_point() +
  geom_line(aes(group = 1), color="lightblue",size=2) + 
  geom_errorbar(aes(x = Time, ymin = Lower, ymax = Upper),
                color="purple",width=0.1,size=1)

我得到了这张图:

这在我看来是正确的,但我可能遗漏了一些你注意到的东西。让我知道..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多