从新data.frame添加行到ggplot2

时间:2018-05-25 11:33:34

标签: r ggplot2

我对ggplot2有点新鲜,我仍觉得很难。我有一个问题,如果我在新的data.frame中没有完全相同的维度和组变量,我似乎无法向现有的ggplot添加新行?我的任务非常基础,我只想用自定义的新行覆盖我现有的情节。我已经明白我需要创建一个新的data.frame(请更正我的,如果我可以避免这一点,只需填写点数)。我已经包含了一个可重复的例子:

library(ggplot2)

myData <- read.table(text = "method snr  ETE seETE 
A 0.05000000 6271.81376 20.6425930 
B 0.05000000 6381.98572 32.1865506 
C 0.05000000 6475.02054 56.0132440 
A 0.08511187 3715.73544 25.1363884 
B 0.08511187 3765.98131 31.7262622 
C 0.08511187 3789.54854 30.6453110 
A 0.14488062 2236.33326 19.5141480 
B 0.14488062 2311.03474 19.5621079 
C 0.14488062 2294.81436 24.8266107 
A 0.24662121 1364.90421 23.9130341 
B 0.24662121 1370.77070 24.5344443 
C 0.24662121 1382.02831 20.7921383 
A 0.41980785  791.70601 12.7460112 
B 0.41980785 782.89390  9.8216899  
C 0.41980785  805.59632 16.4216281 
A 0.71461264 461.10549  3.5709983  
B 0.71461264 456.74167  4.2534227  
C 0.71461264 457.76056  3.6075648  
A 1.21644040 275.80665  3.1710210  
B 1.21644040 270.93396  2.6314973  
C 1.21644040 271.44721  4.1367136  
A 2.07067039 159.10473  2.4732979  
B 2.07067039 156.99296  2.1519381  
C 2.07067039 157.99218  1.5685260  
A 3.52477267  93.10721  0.9666481  
B 3.52477267  94.67429  1.9537842  
C 3.52477267  95.28148  1.7862093  
A 6.00000000  55.43254  0.4655978  
B 6.00000000  54.05841  0.5925237  
C 6.00000000  55.20151  0.8690876", sep = "", header = TRUE)

myAdditionalData <- data.frame("x" =  c(0.05000000, 0.08511187, 0.14488062, 0.24662121, 0.41980785, 0.71461264, 1.21644040, 2.07067039, 3.52477267, 6.00000000),
                               "y" = c(6020.83600, 3537.01303, 2077.86114, 1220.66469,  717.09426, 421.26571,  247.47764,  145.38374,   85.40744,   50.17363))

myPlotETE <- function(data, main = "", xlabel = "", ylabel = "", ticksize = 5, textSize = 13){
  ggplot(data, aes(x = snr, y = ETE, group = method, colour = method)) + 
  geom_errorbar(aes(ymin = ETE-seETE, ymax = ETE+seETE))+
  geom_point()+
  geom_line()+ 
  coord_trans(x = "log")+
  ggtitle(main) +
  xlab(xlabel) +
  ylab(ylabel) +
  theme(plot.title = element_text(hjust = 0.5, size = textSize)) +
  theme(axis.title.y = element_text(angle = 0, vjust = 0.5, size = textSize))+
  theme(axis.text.x = element_text(size = ticksize)) +  
  theme(axis.text.y = element_text(size = ticksize)) +
  theme(legend.position = "bottom")
}

p <- myPlotETE(data=myData, main ="" , ylabel = "") 

所以我可以绘制p,但我无法添加我的行

p

plot without wanted line

p + geom_line(data = myAdditionalData, aes( x = x, y = y))
Error in FUN(X[[i]], ...) : object 'method' not found

非常感谢任何帮助!

0 个答案:

没有答案