facet_wrap()对象上存在错误栏的问题

时间:2019-07-01 17:04:52

标签: r ggplot2 facet facet-wrap

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

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

1 个答案:

答案 0 :(得分:0)

我将通过重塑误差线的数据来实现这一点,以便它们可以访问每行不同列中所需的值。

sapply(sapply(A, function(x) strsplit(x, " ", fixed = TRUE), USE.NAMES = FALSE), 
       function(x) paste(x[1], as.numeric(as.roman(x[2])), collapse = " "))

[1] "Case 1" "Case 2" "Case 3" "Case 4"

enter image description here