【问题标题】:Aligning awkward plot and axis titles对齐尴尬的情节和轴标题
【发布时间】:2016-12-07 15:58:55
【问题描述】:

我创建了以下图表,显示了从 1979 年到 2015 年 4 个季节(每一行)的温度变化(左列)和降水变化(右列):

我现在希望在绘图左侧的 4 个绘图中添加一个通用的 y 轴标签,称为“温度 (°C)”,并在绘图右侧添加一个相同的 y 轴标签,称为“降水 (mm)” .我还希望每行带有季节名称的地块都有一个居中对齐的标题。输出应如下所示:

我查看了各种使用不可见图进行绘图以创建轴标题等的示例,但无法根据我的情况进行调整。非常感谢任何建议。

【问题讨论】:

  • 你能发布你的代码吗?
  • 对不起@ZheyuanLi,我正在度假,直到现在才再次检查。这很好用,是一个很棒的帮助 - 谢谢!

标签: r plot


【解决方案1】:

正如我在 5 小时前的评论中所说,有一种方法可以解决所有定位问题,使用 mtext();只是我不知道这是否是最好的解决方案。但是,已经 5 个小时了,还没有答案,所以我决定发布我的。

放置“Temperatures”、“Precipitations”和“Winter”没有困难,但放置“Spring”、“Summer”和“Autumn”需要一些调整。以下模板代码 中的变量offset 控制这种调整。 offset = -14.5 接近最佳,当我在以下位置生成图形时:

jpeg(file = "template.jpeg", width = 600, height = 600, quality = 100)
## template code
dev.off()

根据您的情节大小,您需要根据需要调整/重置offset。


模板代码

## set plot layout, inner margin and outer margin
par(mfrow=c(4,2), mar = c(1.5,2.5,1.5,2.5), oma = c(3,4,3,4))

## plot 1
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 2
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 3
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 4
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 5
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 6
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 7
plot(1:5, ann = FALSE)
axis(4, at = axTicks(4))

## plot 8
plot(1:5, ann = FALSE)
axis(4, at = axTicks(4))

## write text on outer margins
mtext("Temperatures (°C)", 2, outer = TRUE, line = 2, font = 2)
mtext("Precipitation (mm)", 4, outer = TRUE, line = 2, font = 2)
mtext("Winter", 3, outer = TRUE, line = 0, font = 2)

## needs tuning on `offset`
offset <- -14.5
mtext("Spring", 3, outer = TRUE, line = offset, font = 2)
mtext("Summer", 3, outer = TRUE, line = 2 * offset, font = 2)
mtext("Autumn", 3, outer = TRUE, line = 3 * offset, font = 2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多