gpt4 book ai didi

r - ggplot2中连续变量的方面

转载 作者:行者123 更新时间:2023-12-04 04:12:34 29 4
gpt4 key购买 nike

这个问题已经在这里有了答案:




已关闭8年。




Possible Duplicate:
ggplot - facet by function output


ggplot2facets选项非常适合按因子显示多个图,但是我在学习如何有效地将连续变量转换为其中的因子时遇到了麻烦。带有如下数据:
DF <- data.frame(WindDir=sample(0:180, 20, replace=T), 
WindSpeed=sample(1:40, 20, replace=T),
Force=sample(1:40, 20, replace=T))
qplot(WindSpeed, Force, data=DF, facets=~cut(WindDir, seq(0,180,30)))

我收到错误: At least one layer must contain all variables used for facetting
我想按离散的30度间隔检查 Force~WindSpeed的关系,但是 facet似乎需要将因素附加到所使用的数据帧上(显然我可以做 DF$DiscreteWindDir <- cut(...),但这似乎没有必要)。将连续变量转换为因数时,有没有一种使用 facets的方法?

最佳答案

举例说明如何使用transform进行内联转换:

qplot(WindSpeed, Force,
data = transform(DF,
fct = cut(WindDir, seq(0,180,3))),
facets=~fct)

您无需使用faceting变量来“污染” data,但它在ggplot的数据框中是面向对象的(而不是作为facet规范中列的函数)。

这在扩展语法中同样有效:
ggplot(transform(DF,
fct = cut(WindDir, seq(0,180,3))),
aes(WindSpeed, Force)) +
geom_point() +
facet_wrap(~fct)

关于r - ggplot2中连续变量的方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14569502/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com