Facet for continuous variables in ggplot2
facet, ggplot2, r
Solution
Making an example of how you can use `transform` to make an inline transformation:
qplot(WindSpeed, Force,
data = transform(DF,
fct = cut(WindDir, seq(0,180,3))),
facets=~fct)
You don't "pollute" `data` with the faceting variable, but it is in the data frame for ggplot to facet on (rather than being a function of columns in the facet specification).
This works just as well in the expanded syntax:
ggplot(transform(DF,
fct = cut(WindDir, seq(0,180,3))),
aes(WindSpeed, Force)) +
geom_point() +
facet_wrap(~fct)
Problem
Possible Duplicate: ggplot - facet by function output `ggplot2`'s `facets` option is great for showing multiple plots by factors, but I've had trouble learning to efficiently convert continuous variables to factors within it. With data like: ``` 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))) ``` I get the error : `At least one layer must contain all variables used for facetting` I would like to examine the relationship `Force~WindSpeed` by discrete 30 degree intervals, but it seems `facet` requires factors to be attached to the data frame being used (obviously I could do `DF$DiscreteWindDir <- cut(...)`, but that seems unecessary). Is there a way to use `facets` while converting continuous variables to factors?