ggplot为facet_wrap中的每个绘图添加刻度

con*_*nor 7 r facet ggplot2 facet-wrap

我想在facet_wraps的上面一行中的图上显示x轴刻度.例如:

library(ggplot2)
ggplot(diamonds, aes(carat)) +  facet_wrap(~ cut, scales = "fixed") +  geom_density()
Run Code Online (Sandbox Code Playgroud)

生成这个情节:

实际情节

我想在这个情节中画出蜱虫: 期望的情节

有没有一种简单的方法来实现这个结果?

ali*_*ire 11

使用scales = "free_x"为每个图添加x轴:

ggplot(diamonds, aes(carat)) + 
    geom_density() + 
    facet_wrap(~cut, scales = "free_x")
Run Code Online (Sandbox Code Playgroud)

用x轴绘图

然而,正如你可以看到和语法所暗示的,它也释放每个地块的限制自动调整,因此,如果你希望它们都保持一致,你需要设置它们xlim,limsscale_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")
Run Code Online (Sandbox Code Playgroud)

用均匀的x轴绘图