【问题标题】:Difficulty with error bars on facet_wrap() objectfacet_wrap() 对象上的误差线困难
【发布时间】:2019-11-12 08:17:08
【问题描述】:

我正在尝试将误差线添加到 facet_wrap 图,但似乎无法让 R 生成我想要的图而没有错误消息。我编写了以下代码,它产生了我想要的图,但是我似乎无法让代码的错误栏部分正常工作。

ggplot(data = filter(Total, Cell_Line != "stDev"), aes(Time, Killing)) + 
  geom_line(data = transform(filter(Total, Cell_Line == "Wild_Type"), Cell_Line = NULL), group = 1) +
  #geom_errorbar(aes(x = filter(Total, Cell_Line == "Wild_Type")[,2], ymax = filter(Total, Cell_Line == "Wild_Type")[,3] + filter(Total, Cell_Line == "stDev")[,3], ymin = filter(Total, Cell_Line == "Wild_Type")[,3] - filter(Total, Cell_Line == "stDev")[,3]), data = filter(Total, Cell_Line == "Wild_Type" | Cell_Line == "stDev")) +
  geom_point(colour = "cadetblue") +
  facet_grid_paginate(Cell_Line ~ Run, ncol = 4, nrow = 4)

这段代码产生了Sample Plot的情节,这几乎是我想要的,它只是缺少黑线上的误差线。

这是我用来生成绘图的数据框的顶部:

Total <-
structure(list(Cell_Line = c("3", "7", "8", "17", "19", "20", 
"29", "33", "38", "47", "49", "53", "55", "Wild_Type", "stDev", 
"3", "7", "8", "17", "19", "20", "29", "33", "38", "47", "49", 
"53", "55", "Wild_Type", "stDev"), Time = c("00", "00", "00", 
"00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
"00", "02", "02", "02", "02", "02", "02", "02", "02", "02", "02", 
"02", "02", "02", "02", "02"), Killing = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0704388, 0.2881066, -0.0132908, 
0.04700991, 0.03049371, -0.02243472, 0.1513817, 0.129636, 0.09328508, 
0.05876777, 0.1063291, 0.0357473, 0.1974026, 0.07732854, 0.07383331
), Run = c("run1", "run1", "run1", "run1", "run1", "run1", "run1", 
"run1", "run1", "run1", "run1", "run1", "run1", "run1", "run1", 
"run1", "run1", "run1", "run1", "run1", "run1", "run1", "run1", 
"run1", "run1", "run1", "run1", "run1", "run1", "run1")), row.names = c(NA, 
30L), class = "data.frame")

这是我收到的当前错误消息:_

错误:美学长度必须为 1 或与数据相同 (90): x, ymax, ymin

【问题讨论】:

    标签: r ggplot2 facet facet-wrap


    【解决方案1】:

    我会通过重塑误差线的数据来解决此问题,以便它们可以访问每行不同列中所需的值。

    library(tidyverse)
    library(ggforce)
    ggplot(data = filter(Total, Cell_Line != "stDev"), aes(Time, Killing)) + 
      geom_line(data = transform(filter(Total, Cell_Line == "Wild_Type"), Cell_Line = NULL), group = 1) +
      geom_errorbar(data = Total %>% filter(Cell_Line %in% c("Wild_Type", "stDev")) %>% 
                      spread(Cell_Line, Killing),
                    aes(x = Time, y = Wild_Type, ymax = Wild_Type + stDev, ymin = Wild_Type - stDev)) +      
      geom_point(colour = "cadetblue") +
      facet_grid_paginate(Cell_Line ~ Run, ncol = 4, nrow = 4)
    

    【讨论】:

    • 正是我想要的,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2016-09-14
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多