Question

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!

Was it helpful?

Solution

You can't use directly facet_wrap() with function ggparcoord() 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 of p2. There is no column named set.

 p2 <- ggparcoord(x, columns=1:3, groupColumn=4)
 head(p2$data)
  type .ID anyMissing variable       value
1    x   1      FALSE     var1  0.95473093
2    y   2      FALSE     var1 -0.05566205
3    x   3      FALSE     var1  2.57548872
4    y   4      FALSE     var1  0.14508261
5    x   5      FALSE     var1 -0.92022584
6    y   6      FALSE     var1 -0.05594902

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.

x$ID<-1:40
df.x<-melt(x,id.vars=c("set","ID","type"))

Then use function ggplot() and geom_line() to plot data.

ggplot(df.x,aes(x=variable,y=value,colour=type,group=ID))+
   geom_line()+facet_wrap(~set)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top