【问题标题】:ggplot add ticks to each plot in a facet_wrapggplot 为 facet_wrap 中的每个图添加刻度
【发布时间】:2017-06-07 11:50:07
【问题描述】:

我想在 facet_wraps 的上排图上显示 x 轴刻度。例如:

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

生成此图:

我想在这个情节上画出蜱虫:

有没有简单的方法可以达到这个效果?

【问题讨论】:

    标签: r ggplot2 facet facet-wrap


    【解决方案1】:

    使用 scales = "free_x" 将 x 轴添加到每个绘图:

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

    但是,正如您所看到的和语法所暗示的,它还释放了每个绘图的限制以自动调整,因此如果您希望它们都保持一致,您需要使用xlim、@987654328 设置它们@或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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 2021-06-21
      • 1970-01-01
      相关资源
      最近更新 更多