对齐尴尬的情节和轴标题

时间:2016-08-02 10:32:52

标签: r plot

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

enter image description here

我现在希望在名为“温度(°C)”的图的左侧的4个图上添加一个共同的y轴标签,并在图表的右侧添加一个名为“降水(mm)”的图表。我还希望每个情节都有一个中心对齐的标题,其中包含季节的名称。输出应如下所示:

enter image description here

我已经看过各种用隐形图绘制来创建轴标题等的例子,但不能适应我的情况。任何建议都非常感激。

1 个答案:

答案 0 :(得分:1)

正如我在5小时前的评论中所说,有一种方法可以通过mtext()来解决所有定位问题。只是我不知道这是否是最好的解决方案。然而,有5个小时没有答案到来,所以我决定发布我的。

放置“温度”,“降水”和“冬季”没有任何困难,但需要对“春天”,“夏季”和“秋季”进行一些调整。以下模板代码中的变量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)

enter image description here