【问题标题】:Bunched up x axis ticks on multi panelled plot in ggplot在ggplot中的多面板图上聚集了x轴刻度
【发布时间】:2021-12-16 15:05:28
【问题描述】:

我正在尝试从三个单独的图制作多面板图(参见图片)。但是,当图采用多面板格式时,我无法纠正成束的 x 轴刻度标签。以下是单个绘图和多面板的脚本:

个人情节:

NewDat [[60]]
EstRes <- NewDat [[60]]

EstResPlt = ggplot(EstRes,aes(Distance3, `newBa`))+geom_line() + scale_x_continuous(n.breaks = 10, limits = c(0, 3500))+ scale_y_continuous(n.breaks = 10, limits = c(0,25))+ xlab("Distance from Core (μm)") + ylab("Ba:Ca concentration(μmol:mol)") + geom_hline(yintercept=2.25, linetype="dashed", color = "red")+ geom_vline(xintercept = 1193.9, linetype="dashed", color = "grey")+ geom_vline(xintercept = 1965.5, linetype="dashed", color = "grey") + geom_vline(xintercept = 2616.9, linetype="dashed", color = "grey") + geom_vline(xintercept = 3202.8, linetype="dashed", color = "grey")+ geom_vline(xintercept = 3698.9, linetype="dashed", color = "grey") 
 
EstResPlt 

多面板图:

MultiP <- grid.arrange(MigrPlt,OcResPlt,EstResPlt, nrow =1)

我已尝试包括:

MultiP <- grid.arrange(MigrPlt,OcResPlt,EstResPlt, nrow =1)+ 
  theme(axis.text.x = element_text (angle = 45)) )
MultiP

但只收到错误。不必包含所有刻度线。初始值、中间值和最终值就足够了,因此它们不需要全部包含或倾斜。我只是不确定该怎么做。非常感谢您的帮助。

【问题讨论】:

  • 如果该代码正是您使用的,那么末尾的 ) 是不必要的拼写错误。此外,您可以在使用grid.arrange之前手动旋转每个点。
  • 感谢您的回复。额外的 ) 是一个错字,一旦删除就没有效果。您将使用什么脚本手动旋转每个点?我试过这个没有用:``` MultiP

标签: r ggplot2 axis-labels


【解决方案1】:

有几个选项可以解决拥挤的轴。让我们考虑以下与您的情况相似的示例。默认标记策略不会过度拥挤 x 轴。

library(ggplot2)
library(patchwork)
library(scales)

df <- data.frame(
  x = seq(0, 3200, by = 20),
  y = cumsum(rnorm(161))
)

p <- ggplot(df, aes(x, y)) +
  geom_line()

(p + p + p) / p &
  scale_x_continuous(
    name = "Distance (um)"
  )

但是,由于您已将 n.breaks = 10 分配给秤,它变得拥挤。所以一个简单的解决方案就是删除它。

(p + p + p) / p &
  scale_x_continuous(
    n.breaks = 10, 
    name = "Distance (um)"
  )

或者,您可以将微米转换为毫米,从而减少标签的宽度。

(p + p + p) / p &
  scale_x_continuous(
    n.breaks = 10,
    labels = label_number(scale = 1e-3, accuracy = 0.1),
    name = "Distance (mm)"
  )

另一种选择是仅在每个 n 单位放置一个中断,在下面的例子中,一个 1000。这恰好与偶然省略 n.breaks = 10 相吻合。

(p + p + p) / p &
  scale_x_continuous(
    breaks = breaks_width(1000), 
    name = "Distance (um)"
  )

reprex package (v2.0.1) 于 2021-11-02 创建

【讨论】:

【解决方案2】:

我认为举个例子会更好。

我的意思是,你制作了MigrPltOcResPltEstResPlt,每个都有ggplot() +.....。对于要旋转 x 轴的绘图,请添加 + theme(axis.text.x = element_text (angle = 45))

例如iris数据中,只为a点赞旋转x轴文字

a <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() + 
  theme(axis.text.x = element_text (angle = 45))
b <- ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point() 

gridExtra::grid.arrange(a,b, nrow = 1)

【讨论】:

  • 谢谢你,Park (stackoverflow.com/users/16729175/park),它回答了我的问题。出于兴趣,而不是倾斜刻度标签,我将如何删除每隔一个刻度标签?因此,例如,在右上方绘图的 x 轴上(Petal.Width),我将如何排除只有 0.0、1.0、2.0 而不是 0.0、0.5、1.5、2.0、2.5?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2019-06-12
  • 2018-02-09
  • 1970-01-01
  • 2018-03-27
相关资源
最近更新 更多