44

The following is a situation:

group1 <- seq(1, 10, 2)
group2 <-  seq(1, 20, 3)
x = c(group1, group2)
mydf <- data.frame (X =x , Y = rnorm (length (x),5,1), 
 groups = c(rep(1, length (group1)), rep(2, length(group2))))

ggplot(mydf, aes(X, Y, group= groups)) + geom_point()+ facet_grid (.~ group)

Different facets are scaled by x limits in the following plot:

 ggplot(mydf, aes(X, Y, group= groups)) + geom_point()+ 
   facet_grid (.~ group, scales = "free_x")

As total width of x has meaning, I want to produce facets of different width not only different scale. Thus the expected facet 1's wideth should be half the size of 2.

enter image description here

jon
  • 11,186
  • 19
  • 80
  • 132
  • 1
    BTW, I couldn't get your example to run. I suggested editorial changes but they were rejected. In the calls to `facet.grid()`, I used `. ~ groups` in place of `. ~ group`. Also, `group = groups` in the aesthetics statements is not needed. – Sandy Muspratt May 05 '12 at 22:34

1 Answers1

68

If I understand you correctly, space = "free_x" does what you want in facet_grid. As far as I know, facet_wrap has never supported a space argument, but many facet_wrap commands can be cast as facet_grid commands.

library(ggplot2)

ggplot(mydf, aes(X, Y)) + geom_point()+ 
facet_grid (.~ groups, scales = "free_x", space = "free_x")

enter image description here

And if you want the same style of labelling on the x axes:

ggplot(mydf, aes(X, Y)) + geom_point()+ 
 scale_x_continuous(breaks = seq(0,20,2)) +
 facet_grid (.~ groups, scales = "free_x", space = "free_x")

enter image description here

divibisan
  • 11,659
  • 11
  • 40
  • 58
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
  • 7
    `space` is only supported for `facet_grid`. What to do for `facet_wrap`? Any options? – Paul 'Joey' McMurdie Nov 13 '17 at 01:55
  • Sorry, deleted my comment about `space`, because I discovered the unrecognized-argument error was specific to `facet_wrap`. – Paul 'Joey' McMurdie Nov 13 '17 at 01:56
  • 1
    @PaulMcMurdie As far as I know, `facet-wrap` has never supported a `space` argument, but many `facet_wrap` commands can be cast as `facet-grid` commands. If you have a plot where that is not feasible, it might be better to ask a new question. – Sandy Muspratt Nov 14 '17 at 07:18