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

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

问题描述

我使用了一个简单的ggplot函数,它在循环外工作,但不在里面,即使迭代值不会干扰ggplot函数。为什么是这样?

这是我的代码

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

有用!但是如果ggplot在for循环中...

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

它不工作了!我缺少什么?



谢谢

解决方案

for 循环,您必须显式 print 您的结果 ggplot 对象:

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


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 ?

Here is my code

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

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 ?

Thank you

解决方案

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()) 
}

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

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