I want to plot a normal plot and a pie chart (plotrix package) side-by-side via par(mfrow = c(1, 2)
. The main titles of both graphics should have the same vertical position. However, by default both main titles have a different positioning.
Question: How could I ensure that the main title of the pie chart has the same vertical position as the title in a normal plot?
Consider the following reproducible example in R. "Lower main title" should be at the same height as "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")
The problem is that the two panels get different plot regions. If you use pie3D(..., pty = "m")
they will get the same plot regions, but the pie will look distorted, unless you've chosen a window that makes the plot region for the pie approximately square.
Another solution is to change the plot region for the pie to match the other one, and plot the title after that. For example,
# 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")
This works until you change the shape of the plot window; the pie region tries to stay square, and it will move the title up or down.