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

查看:68
本文介绍了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天全站免登陆