I want to plot multiple parallel coordinate plots from one dataset. Currently I have a working solution with split and l_ply which produces 4 ggplot2 objects. I would like to solve this with facet_wrap or facet_grid to have a more compact layout and a single legend. Is this possible?
With a normal ggplot2 object (boxplot) facet_wrap works perfectly. With the GGally functionggparcoord() I get the error Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting
What am I doing wrong?
require(GGally)
require(ggplot2)
# Example Data
x <- data.frame(var1=rnorm(40,0,1),
var2=rnorm(40,0,1),
var3=rnorm(40,0,1),
type=factor(rep(c("x", "y"), length.out=40)),
set=factor(rep(c("A","B","C","D"), each=10))
)
# this works
p1 <- ggplot(x, aes(x=type, y=var1, group=type)) + geom_boxplot()
p1 <- p1 + facet_wrap(~ set)
p1
# this does not work
p2 <- ggparcoord(x, columns=1:3, groupColumn=4)
p2 <- p2 + facet_wrap(~ set)
p2
Any suggestions are appreciated! Thank you!
You can’t use directly
facet_wrap()with functionggparcoord()because this function use as data only those columns which are specified in call to this function. It can be seen by looking on data element ofp2. There is no column namedset.To get the same type of plot with faceting, first, you need to add new column (contains just numbers corresponding to number of cases) to existing data frame and then reshape this data frame.
Then use function
ggplot()andgeom_line()to plot data.