问题
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.
回答1:
If I understand you correctly, space = "free_x"
does what you want.
library(ggplot2)
ggplot(mydf, aes(X, Y)) + geom_point()+
facet_grid (.~ groups, scales = "free_x", space = "free_x")

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")

来源:https://stackoverflow.com/questions/10454805/different-size-facets-proportional-of-x-axis-on-ggplot-2-r