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

本秂侑毒 提交于 2019-12-23 21:36:53

问题


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

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.


回答1:


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



来源:https://stackoverflow.com/questions/45019839/ggplot2-different-facet-width-for-categorical-x-axis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!