Text Wrap for Plot Titles

Text wrap for plot titles

try adding "\n" (new line) in the middle of your title. For example:

plot(rnorm(100), main="this is my title \non two lines")

Sample Image

ggplot: How to wrap title text according to margins that are relative to plot's width

Not a 100% satisfactory answer to the question but it might still be helpful. Instead of aligning to the entire plot's width, we can align to the panel's width with ggtext::element_textbox(). You can vary the margin argument depending on the size of the title, unit(15, "pt") seemed to work in this particular case.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.2
library(ggtext)
#> Warning: package 'ggtext' was built under R version 4.0.3

my_title <- c("reltively long sentences that normally isn't appropriate as a title but it is what it is")

ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_boxplot() +
labs(title = my_title) +
theme(
plot.title = element_textbox(hjust = 0.5,
width = unit(0.5, "npc"),
margin = margin(b = 15))
)

Sample Image

Created on 2020-12-25 by the reprex package (v0.3.0)

EDIT: Example with plot.title.position = "plot". If you set element_textbox(..., halign = 0.5), then the distances from the text to the borders is equal, but this will center-align the text.

ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
geom_boxplot() +
labs(title = my_title) +
theme(
plot.title = element_textbox(hjust = 0.5,
width = unit(0.5, "npc"),
margin = margin(b = 15)),
plot.title.position = "plot"
)

Sample Image

Created on 2020-12-28 by the reprex package (v0.3.0)

R: ggplot2, can I set the plot title to wrap around and shrink the text to fit the plot?

I do not think there is a text wrap option in ggplot2 (I have always just inserted \n manually). You can, however, shrink the size of the title's text by altering your code in the following way:

title.size<-10
r + geom_smooth() + opts(title = my_title,plot.title=theme_text(size=title.size))

In fact, you all aspects of text with the theme_text function.

Text wrapping works only for vertical labels in matplotlib

Text wrapping doesn't wrap text to not overlap, it wraps text when it tries to go outside the figure.

For vertical labels it seems as matplotlib wraps the text to have nice labels but it just prevents the text to go outside the figure. You can verify this when you set a tight layout - no wrapping occurs, just the axes is being shrinked to accommodate for the labels so that there's no need to wrap them (provided it's possible).

If you make the horizontal label so long that they would go outside the figure they will be wrapped, but only those that would go outside the figure.
Sample Image

See also this source code comment in Text#_get_wrapped_text()

Return a copy of the text with new lines added, so that the text is
wrapped relative to the parent figure.

Is there a way to wrap plot titles in ggarrange?

You can add plot.title.position = 'plot' to your call to theme, which will align titles and subtitles with the entire plot rather than the title panels.

antal_hemvist_program <- function(x {
totdata %>%
filter(program==x) %>%
ggplot(aes(x=fct_rev(fct_infreq(NYA_REGION)))) +
geom_bar() +
theme(axis.title.y = element_blank(),
axis.text.x=element_text(size=5),
plot.title.position = 'plot') +
ylab("Antal")+
coord_flip()+ggtitle(x)}

Sample Image

Wrapping partially bolded and italicized main title in ggpubr - ggerrorplot

Going off the ggtext package webiste:

library(ggpubr)
library(ggtext)
library(tidyverse)

iris %>%
ggerrorplot(
x = "Species",
y = "Sepal.Length",
add = "jitter",
error.plot = "linerange",
add.params = list(color = "darkolivegreen2", size = 1.5),
xlab = 'Treatment',
ylab = "Sepal Length",
ylim = c(0, 10),
width = 1,
ggtheme = theme_dark()
) +
labs(
title = "<b>Figure 5</b> <i>Species with long name</i> Long fake name virginica response all things <i>Shorter species</i> Sepal Length Only Purple Long Fake Name") +
theme(
plot.title.position = "plot",
plot.title = element_textbox_simple(
size = 13,
lineheight = 1,
padding = margin(5.5, 5.5, 5.5, 5.5),
margin = margin(0, 0, 5.5, 0)
)
) +
rotate_x_text(45)

You can manually include line breaks in the title using <br>.

Final graph



Related Topics



Leave a reply



Submit