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?
Making an example of how you can use
transformto make an inline transformation:You don’t “pollute”
datawith 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: