ggplot add ticks to each plot in a facet_wrap

前端 未结 1 1525
独厮守ぢ
独厮守ぢ 2021-01-05 08:06

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,          


        
1条回答
  •  时光说笑
    2021-01-05 08:46

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

    0 讨论(0)
提交回复
热议问题