通过循环将多个曲线/函数添加到一个ggplot [英] Add multiple curves / functions to one ggplot through looping
问题描述
我想将多个曲线添加到一个ggplot
.使用普通的plot
和curve(add=TRUE)
,我可以循环显示曲线功能,并可以添加多个曲线/功能.
I want to add multiple curves to one ggplot
. With the normal plot
and curve(add=TRUE)
i can loop the curve function and can add multiple curves/functions.
由于我想重写ggplot
的代码,因此我试图弄清楚如何使用ggplot实现此目的.
Since i want to rewrite my code for ggplot
, i'm trying to figure out how achieve this with ggplot.
我正在使用stat_function
和for
循环,但是ggplot
并没有添加curve
,只是保存了最后一个.
I'm using the stat_function
and a for
loop, but ggplot
is not adding the curve
, just saving the last one.
单行的最小可复制示例:
Minimum reproducible example with a single line:
library(ggplot2)
p1<-ggplot()
for (i in 1:10){
p1<- p1 + stat_function(aes(x=1:200),fun = function(x) {x+i*3}, col=i)
print(p1)
}
我认为代码应该做什么:
What i think the code should do:
我循环多个stat_function
并将其添加到我的情节p1
I loop multiple stat_function
and adding it to my plot p1
我想做什么:
我希望将Alle线添加到一个绘图中(p1
).照原样编写的代码仅显示循环的最后一行,好像总是在覆盖绘图,但是据我了解,ggplot
这应该只添加一行.还有一个解释为什么这不起作用会很好,也许我只是不明白ggplot
在这里
I want alle lines to be added in one plot(p1
). The code as it is, just shows the last line of the loop, as if it is overwriting the plot always, but as far as i understand ggplot
this should just add a line. Also an explanation why this does not work would be nice, maybe i just don't understand ggplot
here
推荐答案
问题在于,您引用的所有stat_function
都与 same i
变量有关.因此,您添加的每个功能都会与其他功能完美重叠.
The issue is that all the stat_function
s you are refer to the same i
variable. And as a consequence, each function you add will overlay perfectly with the others.
解决方案是在局部范围内重新分配变量,为每次迭代创建局部副本:
The solution is to reassign the variable in a local scope, to make a local copy for each iteration:
p1 = ggplot(data.frame(x = 1 : 200)) + aes(x)
for (i in 1:10){
p1 = local({
j = i
p1 + stat_function(fun = function(x) x + j * 3, col = j)
})
}
为了使事情更加混乱,您实际上不必给局部变量起一个新的名字;您可以很容易地编写i = i
,并继续使用i
.原因是此分配将生成一个新的局部变量i
,该变量将掩盖非局部变量i
.我希望我们可以同意编写这样的代码会造成混乱,这是一个坏主意.
To make matters more confusing, you don’t actually have to give the local variable a new name; you could just as easily have written i = i
, and continued to use i
. The reason is that this assignment will generate a new local variable i
that masks the non-local variable i
. I hope we can agree that writing such code is confusing, and a bad idea.
我还通过将数据x
从stat_function
移出并直接移到ggplot
对象中,略微简化了代码.
I’ve also taken the liberty of simplifying your code slightly by moving the data x
out of the stat_function
, and into the ggplot
object directly.
但是,在这里完全不使用循环和重新分配更为干净.相反,您可以使用lapply
或map
(来自purrr软件包):
However, it is altogether cleaner not to use a loop and reassignment here. Instead you can use lapply
or map
(from the purrr package):
p1 = ggplot(data.frame(x = 1 : 200)) +
aes(x) +
map(
1 : 10,
~ stat_function(fun = function (x) x + .x * 3, color = .x)
)
这更短,更易读(它着重于什么"而不是如何",即循环的机制),并且使用单个分配.
This is shorter, more readable (it focuses on the "what" rather than the "how", i.e. the mechanics of the loop), and it uses a single assignment.
这篇关于通过循环将多个曲线/函数添加到一个ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!