【发布时间】:2019-10-28 18:31:36
【问题描述】:
我有几个相似的数据框和每个数据集的非线性回归。只要最初我不知道那里有多少数据框,我就想使用 for 循环覆盖这些图。我可以使用for() 循环覆盖geom_point() 层,但是当我使用stat_function() 尝试相同时,只绘制最后一个函数。
我怎样才能获得与积分相同的功能结果?
MWE:
library(ggplot2)
# Colors vector
hues = seq(15, 375, length = 10 + 1)
cols = hcl(h = hues, l = 65, c = 100)[1:10]
# Create plot and add first layer
p <- ggplot(data = data.frame(x = 1:10, y = 10 + 1:10),
aes(x = x, y = y, color = cols[i])) +
geom_point()
# Add points of other datasets
for (i in 1:9) {
p <- p + geom_point(data = data.frame(x = 1:10, y = i + 1:10), color = cols[i])
}
print(p)
# This for cycle only seems to work for the last layer
for (i in 1:10) {
p <- p + stat_function(fun = function(x) (i + x), color = cols[i])
}
print(p)
提前谢谢你。
【问题讨论】: