通过循环将多个曲线/函数添加到一个ggplot [英] Add multiple curves / functions to one ggplot through looping

查看:118
本文介绍了通过循环将多个曲线/函数添加到一个ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个曲线添加到一个ggplot.使用普通的plotcurve(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_functionfor循环,但是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_functions 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.

我还通过将数据xstat_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.

但是,在这里完全不使用循环和重新分配更为干净.相反,您可以使用lapplymap(来自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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆