facet_grid hierarchical splitting

588 views Asked by At

I can generate 2 big facets with facet_grid based on the factor (a). I can generate one facet for each id + factor combination with facet_wrap or facet_grid (b). I would like to have 3 facets: a(D+E) b(D+E) c(D+E) (c).

testd <- data.frame(id=c("a","b","c"),value=1:12,fac=c("D","E"))
#(a) 
ggplot(testd, aes(x=id,y=value))+ geom_point() + facet_grid(. ~ fac)
#(b) 
ggplot(testd, aes(x=id,y=value))+ geom_point() + facet_wrap(id ~ fac, nrow=1, scales="free_x")
ggplot(testd, aes(x=id,y=value))+ geom_point() + facet_grid(. ~ id + fac, scales="free_x")

### schema:
(a)
--D-- --E--
a b c a b c 
(b)
aD aE bD bE cD cE 
(c)
DE  DE DE
 a   b  c

What I would like to have is for each id one facet with points split into D and E in the same facet.

1

There are 1 answers

0
Sven Hohenstein On BEST ANSWER

I suppose you're looking for this:

ggplot(testd, aes(x = fac, y = value)) + geom_point() + facet_grid( ~ id)

I replaced x = id inside aes with x = fac.

enter image description here