【问题标题】:Avoid overlapping x-axis labels in ggplot facet grid避免在 ggplot facet 网格中重叠 x 轴标签
【发布时间】:2016-12-19 14:55:50
【问题描述】:

我正在尝试绘制生长图的分面网格,并且每个图末端的标签彼此重叠。以下是使用 mpg 数据的示例代码:

print(ggplot(data = aggregate(hwy~class+year, data=mpg, mean), aes(x = year, y=hwy))+
    geom_line(aes(group = 1))+
    geom_point()+
    facet_wrap(~class,  nrow = 2)+
    xlab("Year")+
    scale_x_discrete(limits=unique(mpg$year)))

如何防止这种重叠,可能是通过将刻度线和标签从绘图边缘移入。我尝试在主题中使用边距,但我也没有成功。感谢您的帮助。

【问题讨论】:

  • 最简单的解决方案。太增加图形窗口或输出设备的宽度。但是,您可以旋转标签p + theme(axis.text.x = element_text(angle=-90, vjust=0.5))。或者您可以使用 expand..scale_x_discrete(expand=c(0.5, 0.5), limits=unique(mpg$year)) 添加一点空间

标签: r ggplot2


【解决方案1】:

我想你在用theme 中的panel.spacing.x 调整构面面板之间的水平间距(用ggplot2_3.0.0 测试)。

ggplot(data = aggregate(hwy~class+year, data=mpg, mean), aes(x = year, y=hwy))+
  geom_line(aes(group = 1))+
  geom_point()+
  facet_wrap(~class,  nrow = 2)+
  xlab("Year")+
  scale_x_discrete(limits=unique(mpg$year)) +
  theme(panel.spacing.x = unit(4, "mm"))

之前

之后 - using panel.spacing.x()

【讨论】:

    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2020-07-06
    • 2018-06-07
    • 2014-04-30
    • 1970-01-01
    • 2012-06-01
    相关资源
    最近更新 更多