1

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")

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
Joachim Schork
  • 2,025
  • 3
  • 25
  • 48
  • One way would be using `mtext()` – Miha Jul 22 '18 at 12:54
  • @Miha Thanks for your response. Could you specify that in some more detail? When I use `mtext(..., line = ...)` I can move the title. However, I do not know exactly at which line-position the other title is and therefore I am not able to specify exactly the same height. Furthermore, I'm looking for an automated way, since I'm going to create this graphic many times with different plots. – Joachim Schork Jul 22 '18 at 13:15
  • Try something like this: `mtext("Lower main title", side = 3, line = 17)`, where argumetn `line` means position of the text. – Miha Jul 22 '18 at 13:22
  • @Miha Thanks again. With your code I can APPROXIMATELY replicate the position, which is nice. However, I am looking for EXACTLY the same position. Is there a way to detect the exact height of the first title and adjust the second title accordingly? – Joachim Schork Jul 22 '18 at 14:52

2 Answers2

1

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.

user2554330
  • 37,248
  • 4
  • 43
  • 90
1

If you don't mind that both plots are the same size you could do this by using layout() function that divides the device according to rows or columns specified.

So first you specify the two rows where your plots will be

# 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

UPDATE

According to @user2554330 the above code can be replaced with only setting

par(pty = "s")

which means that we set a graphical parameter which will generate a square plotting region.

Then Use your code

# 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")

ant the output:

enter image description here

Miha
  • 2,559
  • 2
  • 19
  • 34