Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 different facet width for categorical x-axis [duplicate]

Tags:

r

ggplot2

facet

I have am plotting different facets of categorical data:

df <- as.data.frame(as.factor(c("A","B","C","D","E","F")))
names(df) <- "Xvar"
df$Yvar <- c(2,1,4,5,3,7)
df$facet <- c(rep("facet 1",2),rep("facet 2",4))

ggplot(df, aes(x=Xvar, y=Yvar, group=1)) +
  geom_line() +
  facet_wrap(~facet, scales="free_x")

enter image description here

How can I make it such that facet 1 consisting of only two categories is half the size of facet 2 containing four categories? I.e. that the width of each facet is proportional to the number of categorical x-axis data points? I tried scales="free_x" to no avail.

like image 628
user2568648 Avatar asked Oct 21 '25 05:10

user2568648


1 Answers

If you're willing to use facet_grid instead of facet_wrap, you can do this with the space parameter.

ggplot(df, aes(x=Xvar, y=Yvar, group=1)) +
  geom_line() +
  facet_grid(~facet, scales="free_x", space = "free_x")

enter image description here

like image 180
Eric Watt Avatar answered Oct 22 '25 19:10

Eric Watt