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

How to add line using other data to ggplot?

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

我的部分数据:

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

你知道我做错了什么吗?

没有线的数据是这样的:

以及添加后的样子(大约):

不幸的是你的 bb 变量是一个数字 table 所以这不能被 ggplot 绘制。您可以尝试以下操作吗:

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

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

以防万一有人来到这里寻找 "how to plot a line from a different data frame on top of an existing plot"[潜在] 问题的答案:

这里的关键点是在对geom_line的调用中使用data=。假设 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))