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))
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:
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')))
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.
Related Topics
How to Pass Individual 'Curvature' Arguments in 'Ggplot2' 'Geom_Curve' Function
How to Pad a Vector with Na from the Front
Create Columns from Column of List in Data.Table
Stacked Bar Chart, Reorder by Total (Sum Up of Values) Instead of Value Ggplot2 + Dplyr
Adding Labels on Curves in Glmnet Plot in R
Addsma Not Drawn on Graph When Called from Function
How to Create a Bar and Line Plot with R Dygraphs
Unexpected Symbol Error in Parse(Text = Str) with Hyphen After a Digit
Q-Q Plot with Ggplot2::Stat_Qq, Colours, Single Group
The Representation of an Empty Argument in a "Call"
Splitting String Based on Letters Case
Read CSV with Two Headers into a Data.Frame
How to Add Se Error Bars to My Barplot in Ggplot2