【发布时间】:2019-02-15 16:05:17
【问题描述】:
我想叠加两个图:一个是简单的点图,其中一个变量用于控制点的大小;另一个是简单的曲线。
这是第一个情节的虚拟示例;
library(ggplot2)
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla")
现在让我们用来自另一个数据框的数据覆盖一条曲线到该图:
df2 = data.frame(x=x, y=x^2-x+2)
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla") + geom_line(data=df2, aes(x, y), color = "blue") + scale_color_discrete(name = "other", labels = c("nanana"))
它会产生错误:
FUN(X[[i]], ...) 中的错误:找不到对象“v”
v 中的值不是用来绘制预期的诅咒的,但无论如何,我在 df2 中添加了一个虚拟 v。
df2 = data.frame(x=x, y=x^2-x+2, v=replicate(length(x),0)) # add a dummy v
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla") + geom_line(data=df2, aes(x, y), color = "blue") + scale_color_discrete(name = "other", labels = c("nanana"))
一个结果有一个乱七八糟的传说:
实现理想情节的正确方法是什么?
【问题讨论】:
标签: r dataframe ggplot2 legend