如果 ggplot 在 for 循环内不起作用,尽管它在循环之外工作 [英] ggplot does not work if it is inside a for loop although it works outside of it

查看:28
本文介绍了如果 ggplot 在 for 循环内不起作用,尽管它在循环之外工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个简单的 ggplot 函数,该函数在循环外运行良好,但在循环内部却无法正常工作,即使迭代值不干扰 ggplot 函数.为什么会这样?

I'm using a simple ggplot function which works fine outside a loop but not inside even if the iterative value does not interfere with the ggplot function. Why is it so ?

这是我的代码

x=1:7
y=1:7
df = data.frame(x=x,y=y)
ggplot(df,aes(x,y))+geom_point()

它有效!但是如果 ggplot 在 for 循环内......

It works ! But if the ggplot is inside a for loop ...

for (i in 1:5) {
   ggplot(df,aes(x,y))+geom_point()
}

它不再起作用了,我错过了什么?

it doesn't work anymore, what am I missing ?

推荐答案

当在 for 循环中时,你必须明确地print 你的结果 ggplot对象:

When in a for loop, you have to explicitly print your resulting ggplot object :

for (i in 1:5) { 
    print(ggplot(df,aes(x,y))+geom_point()) 
}

这篇关于如果 ggplot 在 for 循环内不起作用,尽管它在循环之外工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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