ggplot add ticks to each plot in a facet_wrap

╄→尐↘猪︶ㄣ 提交于 2019-11-30 04:02:13

问题


I'd like to display x-axis ticks on plots in the upper rows in facet_wraps. For example:

library(ggplot2)
ggplot(diamonds, aes(carat)) +  facet_wrap(~ cut, scales = "fixed") +  geom_density()

generates this plot:

I'd like to have ticks as I've drawn in on this plot:

Is there a simple way to achieve this result?


回答1:


Using scales = "free_x" adds x axes to each plot:

ggplot(diamonds, aes(carat)) + 
    geom_density() + 
    facet_wrap(~cut, scales = "free_x")

However, as you can see and the syntax suggests, it also frees the limits of each plot to adjust automatically, so if you want them all to be consistent, you'll need to set them with xlim, lims or scale_x_continuous:

ggplot(diamonds, aes(carat)) + 
    geom_density() + 
    xlim(range(diamonds$carat)) + 
    # or lims(x = range(diamonds$carat))    
    # or scale_x_continuous(limits = range(diamonds$carat))
    facet_wrap(~cut, scales = "free_x")



来源:https://stackoverflow.com/questions/41798392/ggplot-add-ticks-to-each-plot-in-a-facet-wrap

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