繁体   English   中英

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

[英]ggplot add ticks to each plot in a facet_wrap

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

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

生成这个情节:

实际情节

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

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

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

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

用x轴绘图

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

用均匀的x轴绘图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM