【发布时间】: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
我想在 facet_wraps 的上排图上显示 x 轴刻度。例如:
library(ggplot2)
ggplot(diamonds, aes(carat)) + facet_wrap(~ cut, scales = "fixed") + geom_density()
生成此图:
有没有简单的方法可以达到这个效果?
【问题讨论】:
标签: r ggplot2 facet facet-wrap
使用 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")
【讨论】: