ggplot 如果它在 for 循环内则不起作用,尽管它在它之外工作 [英] ggplot does not work if it is inside a for loop although it works outside of it
本文介绍了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屋!
查看全文