ggplot2: independent axes for grid facet

Viewed 65

I have xy data on a grid with some categorical variables. Here is some dummy data.

fn <- function(x,group,type) {
  y <- expand.grid(x=seq(1,x),y=seq(1,x))
  y$group <- group
  y$type <- type
  return(y)
}

dfr <- rbind(
  fn(5,"a","1"),
  fn(5,"b","1"),
  fn(20,"c","1"),
  fn(10,"a","2"),
  fn(50,"c","2")
)

head(dfr)

  x y group type
1 1 1     a    1
2 2 1     a    1
3 3 1     a    1
4 4 1     a    1
5 5 1     a    1
6 1 2     a    1

I need them on a grid layout due to the grouping variables.

Default grid

Good: Keeps grid layout, Gaps between points fixed, aspect ratio fixed

Bad: Data does not fill facet

ggplot(dfr,aes(x,y))+
  geom_point()+
  facet_grid(group~type)+
  theme(aspect.ratio=1)

enter image description here

Grid free scales

Good: Keeps grid layout, aspect ratio fixed.

Bad: Data does not fill facet, Gaps between points differ

ggplot(dfr,aes(x,y))+
  geom_point()+
  facet_grid(group~type,scales="free")+
  theme(aspect.ratio=1)

enter image description here

Grid free scales + free space

Good: Keeps grid layout, Gaps between points fixed

Bad: Data does not fill facet, Aspect ratio cannot be set

ggplot(dfr,aes(x,y))+
  geom_point()+
  facet_grid(group~type,scales="free",space="free")

enter image description here

Wrap free scales

Good: Data fills facet, aspect ratio fixed

Bad: Loses grid layout, gap between points not equal

ggplot(dfr,aes(x,y))+
  geom_point()+
  facet_wrap(group~type,scales="free")+
  theme(aspect.ratio=1)

enter image description here

I would like all of these combined.

  • Keeps grid layout
  • Gaps between points fixed
  • Aspect ratio fixed
  • Data fills the facet (ie; variable size facets)

I think this is only possible using grid layout with independent axes. Is there some option I am missing or do I have to build this manually?

Update: Expected end result.

enter image description here

0 Answers
Related