ggMarginal plot margins
It looks like ggMarginal adds the supporting plots but then it pushes the title to the top of the margin with no extra room. This doesn't make the plot titles very accessible. Is there any support or methods for building some buffer room between top of margin to the title? I tried adding more room before using ggMarginal under theme, but it still doesn't work.
Thanks!
Can you post what you tried in terms of theme modifications?
Yea well I was just playing around with it so I added the plot.margin function to see if I could create extra space before adding the ggMarginal function. And when I tried doing this for the ggMarginal plot it wouldn't let me do it. Here is all the code and I still get the title glued to the top margin with no room. Appreciate any help!
tr <- ggplot(mtcars) +
aes(x = mpg, y = disp, color = as.character(cyl), shape = as.character(cyl)) +
geom_point(size = 3) +
geom_smooth(method = "lm",
se = TRUE,
show.legend = FALSE,
alpha = 0.1) +
scale_color_manual(values = c("#325756FF", "#7D9FC2FF", "#C582B2FF", "#51806AFF", "#4D5F8EFF", "#A092B7FF")) +
labs(
# Main title label
title = "<i> Mtcars</i> Vizualization Project",
# Subtitle label
subtitle = "Model for <b style='color:#325756FF'>4</b>, <b style='color:#7D9FC2FF'>6</b>, and <b style='color:#C582B2FF'>8</b> Cylinders",
# X-axis label
x = "MPG",
# Y-axis label
y = "Dispersion",
# Caption label
caption = "Sources: mtcars.",
# Legend titles
shape = "Cylinder",
color = "Cylinder",
fill = "Cylinder"
) +
theme(
# Legend location, formatting, and background
legend.position = "bottom",
legend.title = element_text(
family = "",
face = "bold",
size = 16,
color = "grey30"
),
# Legend background fill and outline (color)
legend.background = element_blank(),
#
legend.box.background = element_blank(),
# Legend box for shapes
legend.key = element_blank(),
# Overall text formatting
text = element_text(
family = "",
face = "bold",
size = 16,
color = "grey30"
),
# Plot title formatting
plot.title = element_markdown(
lineheight = 1.1,
family = "",
size = 20,
face = "bold",
hjust = 0.5,
color = "grey30"
),
# Plot subtitle formatting
plot.subtitle = element_markdown(
family = "",
size = 14,
hjust = 0.5,
face = "plain",
color = "grey30"
),
# Plot caption formatting
plot.caption = element_text(
family = "",
size = 10,
face = "plain",
color = "grey30"
),
# Axes text angle and formatting
axis.text.x = element_text(
angle = 0,
face = "bold"
),
axis.text.y = element_text(
angle = 0,
face = "bold"
),
# Overall (x and y) axes ticks formatting
axis.ticks = element_blank(),
# Overall (x and y) axes lines formatting
axis.line = element_line(
color = "#b4aea9"
),
# Panel grid formatting
panel.grid = element_line(
color = "grey95"
),
# Minor overall (x and y) panel grid formatting
panel.grid.minor = element_blank(
),
# Major overall (x and y) panel grid formatting
panel.grid.major = element_line(
linetype = "solid"
),
# Panel background (what is inside axes)
panel.background = element_rect(
fill = "#ffffff",
color = "#ffffff"
),
# Plot background (what is outside axes)
plot.background = element_rect(
fill = "#ffffff",
color = "#ffffff"
),
plot.margin = margin(4, 1, 1, 1, "cm")
)
tr2 <- tr %>%
ggMarginal(
groupColour = TRUE,
groupFill = TRUE
)
tr3 <- tr2 %>%
theme(plot.margin = margin(4, 1, 1, 1, "cm"))
This is what the output looks like.
Can you try using margin, like so:
library(ggplot2)
library(ggExtra)
p <- ggplot(mtcars, aes(x = mpg, y = mpg)) +
geom_point() +
ggtitle("title") +
theme(plot.title = element_text(margin = margin(b = 1, unit = "inches")))
ggMarginal(p)

Created on 2024-03-27 with reprex v2.1.0
Ah so the issue isn't the distance between the plots and the title, rather the title and the top margin. As your plot shows, ggMarginal glues the title to the top of the plot margin irregardless of the margins you created in the plot itself. So essentially I'm trying to have a few cm between the top of the plot margin and the title.
Try using t = 1 instead of b
Ah thank you so much!! That worked perfect!! All the best!
Thanks for helping @crew102 !!! Can we close this issue @frumh002 ?
Yes we can thanks again!!