Force X axis on both graphs in a facet grid when X values are the same

ⅰ亾dé卋堺 提交于 2019-12-04 00:18:40

问题


I have data with about 30 categories for the X axis in two groups for faceting. I will show this with some random data:

dataf <- data.frame(x=c(1:30), A=rnorm(30,20,5), B=rnorm(30,15,0.5))
datam <- melt(dataf, id="x")
ggplot(datam, aes(factor(x), value)) + 
  geom_bar(stat="identity") + 
  facet_grid(variable ~ .)

This is just lovely, except that it would be easier to quickly read off categories on the top grouping if the x axis was reproduced on that graph too. However

ggplot(datam, aes(factor(x), value)) + 
  geom_bar(stat="identity") + 
  facet_grid(variable ~ ., scales="free")

makes no difference to the x axis because, I guess, the values are the same for both groupings.

How can I force the X axis to be reproduced for the top group as well of bars?


回答1:


Try using facet_wrap instead:

ggplot(datam, aes(factor(x), value)) + 
    geom_bar(stat="identity") + 
    facet_wrap(~variable,nrow = 2,scales = "free")



来源:https://stackoverflow.com/questions/10913840/force-x-axis-on-both-graphs-in-a-facet-grid-when-x-values-are-the-same

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