垂直位置与正常图相同的饼图主标题

Pie chart main title with same vertical position as normal plot

我想通过 par(mfrow = c(1, 2) 绘制普通图和饼图(plotrix 包)side-by-side。两个图形的主标题应具有相同的垂直位置。但是,默认情况下,两个主标题的位置不同。

问题:如何保证饼图的主标题与普通图的标题垂直位置相同?

考虑以下 R 中的可重现示例。"Lower main title" 应与 "Main title with usual height" 处于相同高度。

# Set panel layout
par(mfrow = c(1, 2))

# Normal plot with normal height of main
plot(1:10, 1:10, main = "Main title with usual height")

# Load plotrix package for piecharts
library("plotrix")

# Pie chart with lower main title position
pie3D(1:5, labels = letters[1:5], main = "Lower main title")

问题是两个面板获得不同的绘图区域。如果您使用 pie3D(..., pty = "m"),它们将获得相同的绘图区域,但饼图看起来会变形,除非您选择 window 使饼图的绘图区域近似为正方形。

另一种解决方案是更改饼图的绘图区域以匹配另一个饼图,然后再绘制标题。例如,

# Set panel layout
par(mfrow = c(1, 2))

# Normal plot with normal height of main
plot(1:10, 1:10, main = "Main title with usual height")

# Save the plot region
plt <- par("plt")

# Load plotrix package for piecharts
library("plotrix")

# Pie chart with lower main title position
pie3D(1:5, labels = letters[1:5], main = "")

# Restore the original plot region and add the title
par(plt = plt)
title(main = "Pie title with matching height")

在您更改图的形状之前,此方法一直有效window;饼图区域试图保持正方形,它会向上或向下移动标题。

如果您不介意两个图的大小相同,您可以使用 layout() 函数根据指定的行或列划分设备。

所以首先你指定你的地块所在的两行

# So create matrix with 1 row, and specify size and width of your plots
two.rows.plot <- layout(matrix(c(1, 2), nrow = 1), widths = c(5, 5), heights = c(5, 5), TRUE) 
layout.show(two.rows.plot) # how the device is being split up into different figure regions

更新

根据@user2554330 上面的代码可以被替换只需要设置

par(pty = "s")

这意味着我们设置了一个图形参数,它会生成一个正方形的绘图区域。

然后使用您的代码

# Normal plot with normal height of main
plot(1:10, 1:10, main = "Main title with usual height")

# Load plotrix package for piecharts
library("plotrix")

# Pie chart with lower main title position
pie3D(1:5, labels = letters[1:5], main = "Lower main title")

蚂蚁输出: