可能重复:
ggplot - 按功能输出的facet
ggplot2
的facets
选择是伟大的显示的因素多条曲线,但我已经遇到了麻烦,学习连续变量高效地转换成因素在其中.使用以下数据:
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)))
Run Code Online (Sandbox Code Playgroud)
我收到错误: At least one layer must contain all variables used for facetting
我想Force~WindSpeed
通过离散的30度间隔检查这种关系,但似乎facet
需要将因子附加到正在使用的数据框上(显然我可以这样做DF$DiscreteWindDir <- cut(...)
,但这似乎是不必要的).facets
在将连续变量转换为因子时有没有办法使用?
举例说明如何使用transform
内联转换:
qplot(WindSpeed, Force,
data = transform(DF,
fct = cut(WindDir, seq(0,180,3))),
facets=~fct)
Run Code Online (Sandbox Code Playgroud)
您没有data
使用faceting变量"污染" ,但是它在ggplot的数据框中面向(而不是facet规范中的列的函数).
这在扩展语法中同样有效:
ggplot(transform(DF,
fct = cut(WindDir, seq(0,180,3))),
aes(WindSpeed, Force)) +
geom_point() +
facet_wrap(~fct)
Run Code Online (Sandbox Code Playgroud)