Plot Title at Bottom of Plot Using Ggplot2

Plot title at bottom of plot using ggplot2

Here is a solution using grid.text:

library("grid")
qplot(rnorm(100)) + theme(plot.margin=unit(c(0.5, 1, 2, 0.5), "lines"))
grid.text("My Title", x = unit(0.5, "npc"), y = unit(0, "npc"),
vjust = -0.5, gp = gpar(cex=1.2))

plot

ggplot2 How to put at bottom the x axis title?

What you could do is simulate an x-axis title at the bottom using a caption, then just remove the one at the top. These additions do the trick:

ggplot() + 
... +
labs(caption = "DAT") +
theme(axis.title.x = element_blank(),
plot.caption = element_text(size = 14, hjust = 0.5, face = "bold"),
...)

This is what it should look like:

Sample Image

How to position plot.tag and plot.title side-by-side, on the same line

One option to achieve your desired result would be the ggtext package which allows you to glue the tag and the title together but still have the option to add different styles via some markdown or HTML/CSS. To make this a bit more convenient I make use of a helper function:

library(ggplot2)
library(ggtext)

labs2 <- function(title = NULL, tag = NULL, ...) {
if (!is.null(tag)) {
tag <- paste0("**", tag, "**")
title <- paste(tag, title)
}
labs(title = title, ...)
}

ggplot(economics, aes(x = date, y = uempmed)) +
geom_line() +
labs2(title = "Median duration of unemployment",
tag = "Figure 1.0:",
y = "Weeks") +
theme(plot.title.position = 'plot',
plot.title = ggtext::element_markdown(hjust = 0, vjust = 1, margin = margin(t = 0, r = 0, b = 0, l = 0, unit = 'mm')))

Sample Image

Center align ggplot title when title is placed within the plot area

theme(plot.title = element_text(hjust = 0.5, margin = margin(t=10,b=-20)))

How to plot the legend title above a horizontal legend

Use guide_legend:

g4 +
guides(linetype = guide_legend(title.position = "top"))

For the title to be centered, you can add title.hjust = 0.5 in the guide_legend function.

Sample Image



Related Topics



Leave a reply



Submit