在ggplot中循环变量 [英] Looping over variables in ggplot
本文介绍了在ggplot中循环变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用ggplot循环多个列来创建多个图,但在for循环中使用占位符会改变ggplot的行为。
I want to use ggplot to loop over several columns to create multiple plots, but using the placeholder in the for loop changes the behavior of ggplot.
如果我有这个:
If I have this:
t <- data.frame(w = c(1, 2, 3, 4), x = c(23,45,23, 34),
y = c(23,34,54, 23), z = c(23,12,54, 32))
这工作得很好:
This works fine:
ggplot(data=t, aes(w, x)) + geom_line()
但这不是:
i <- 'x'
ggplot(data=t, aes(w, i)) + geom_line()
如果我想最终遍历x,y和z,这是一个问题。
有帮助吗?
Which is a problem if I want to eventually loop over x, y and z. Any help?
推荐答案
您只需要使用 aes_string
而不是 aes
,如下所示:
You just need to use aes_string
instead of aes
, like this:
ggplot(data=t, aes_string(x = "w", y = i)) + geom_line()
code> w 然后也需要被指定为一个字符串。
Note that w
then needs to be specified as a string, too.
这篇关于在ggplot中循环变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文