ggplot在facet_wrap中的每个图上添加刻度线 [英] ggplot add ticks to each plot in a facet_wrap

查看:451
本文介绍了ggplot在facet_wrap中的每个图上添加刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

生成此图:

我想在此情节中画上勾号:

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

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

Is there a simple way to achieve this result?

推荐答案

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

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

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

但是,正如您所看到的和语法所建议的那样,它还释放了每个图的限制以进行自动调整,因此,如果要使它们保持一致,则需要使用xlimscale_x_continuous:

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

这篇关于ggplot在facet_wrap中的每个图上添加刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆