如何使用其他数据添加行到ggplot?

时间:2015-03-26 14:44:51

标签: r ggplot2

我想在我的数据中添加一行:

我的部分资料:

dput(d[1:20,])
structure(list(MW = c(10.8, 10.9, 11, 11.7, 12.8, 16.6, 16.9, 
17.1, 17.4, 17.6, 18.5, 19.1, 19.2, 19.7, 19.9, 20.1, 22.4, 22.7, 
23.4, 24), Fold = c(21.6, 21.8, 22, 23.4, 25.6, 33.2, 33.8, 34.2, 
34.8, 35.2, 37, 38.2, 38.4, 39.4, 39.8, 40.2, 44.8, 45.4, 46.8, 
48), Column = c(33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 
33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 33.95, 
33.95, 33.95, 33.95, 33.95), Bool = c(1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("MW", "Fold", 
"Column", "Bool"), row.names = c(NA, 20L), class = "data.frame")

行数据:

> dput(bb[1:20,])
structure(c(1.95, 3.2, 3.7, 4.05, 4.5, 4.7, 4.75, 5.05, 5.2, 
5.2, 5.2, 5.25, 5.3, 5.35, 5.35, 5.4, 5.4, 5.45, 5.5, 5.5, 10, 
33.95, 58.66, 84.42, 110.21, 134.16, 164.69, 199.1, 234.35, 257.19, 
361.84, 432.74, 506.34, 581.46, 651.71, 732.59, 817.56, 896.24, 
971.77, 1038.91), .Dim = c(20L, 2L), .Dimnames = list(NULL, c("b", 
"a")))

作为我用来创建这个情节的最后一个代码:

first_dot <- ggplot(d, aes(x=MW, y=Column)) +
  geom_point() +
  scale_x_continuous(limits=c(0, 650), breaks=c(0, 200, 400, 650)) +
  scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))


first_dot + geom_line(bb, aes(x=b, y=a))

我尝试运行时始终遇到此错误:

Error: ggplot2 doesn't know how to deal with data of class uneval

你知道我做错了什么吗?

这就是没有线条的数据:

No line

添加like(大约)后它应该是什么样子:

with line

2 个答案:

答案 0 :(得分:3)

以防万一有人来这里寻找[基础]问题的答案&#34;如何在现有情节的基础上绘制来自不同数据框的一条线&#34;:

此处的关键点是在data=的调用中使用geom_line。假设我们中的某些人发生了data=并不是必需的,而且往往是它不起作用的原因。正如@Roland对问题的评论所做的那样,正确的代码是:

ggplot(d, aes(x=MW, y=Column)) +
     geom_point() +
     scale_x_continuous(limits=c(0, 650), breaks=c(0, 200, 400, 650)) +
     scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))+
     geom_line(data=as.data.frame(bb),aes(x=b,y=a))

答案 1 :(得分:2)

不幸的是,您的bb变量是一个数字表,因此ggplot无法对其进行绘制。你可以尝试以下方法:

first_dot + geom_line(data=data.frame(bb), aes(x=b, y=a))

请注意,您将bb变量转换为data.frame