I have a problem with ggplot
title positioning. When I use hjust
argument to slide title a bit right, both lines undesirably slide left and right. I want them one under the other. drawlabel()
,annototations
are not useful for my case, because they depend on x and y axis and their units(date,currency,kg) which mean every time I have to adjust them. I want unique coordinate system, whatever I am plotting I can easily use same place for different plots.
An undesired example with code is shown below.
set.seed(10)
df <- data.frame(x=1:10,y=round(rnorm(10)*1:10,2))
ggplot(df,aes(x=x,y=y))+
geom_line(size=0.75)+
labs(title = 'Here comes my title like that some words\nand my second line title')+
theme(
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.075)
)
Undesirable plot
Desirable plot
For example we can align multi-line text of labels to be either center (default), left or right aligned using a Graphviz feature. When we want to text to be center aligned we simply use the new-line character . If we want to have our text left aligned we use \l instead of . And to right align the text we use .
In order to align titles in visualization using plotly module, we are going to use the update_layout () method. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
title_y: This parameter is used to align the title in a vertical motion and accepts a value from 0 to 1. With title_y=0.5, the title should be in center.
When we want to text to be center aligned we simply use the new-line character . If we want to have our text left aligned we use \l instead of . And to right align the text we use . In the following example we have three labels that are center, left and right aligned:
I agree with @stefan in the comments that there appears to be no way to do this with theme
.
One hackish approach is to remove your hjust
change and manually move the position of the title by editing the grid layout.
library(ggplot)
library(grid)
p <- ggplot(df,aes(x=x,y=y)) +
geom_line(size=0.75) +
labs(title = 'Here comes my title like that some words\nand my second line title') +
theme(plot.title.position = 'plot')
grob <- ggplotGrob(p)
grob$layout[grob$layout$name == "title","l"] <- 4.5
grid.newpage()
grid.draw(grob)
You can change 4.5
as necessary for the desired effect.
ggtext
provides tools that make it a bit easier to muck about with text. In the example below, using the <br>
to add a newline we can then render this with the theme parameter element_textbox_simple
(with a little padding around the text).
library(ggplot2)
library(ggtext)
set.seed(10)
df <- data.frame(x=1:10,y=round(rnorm(10)*1:10,2))
ggplot(df,aes(x=x,y=y))+
geom_line(size=0.75)+
labs(title = 'Here comes my title like that some words <br> and my second line title') +
theme(
plot.title.position = "plot",
plot.title = element_textbox_simple(padding = margin(5.5, 5.5, 5.5, 5.5))
)
I might miss the obvious, but a simple option is to use a subtitle :)
Now, the indentation with hjust with title and subtitle is also NOT the same, however, it seems to be a constant! Weirdly this is totally dependent on your device. For example, in my RStudio Viewer, I need a constant of ~2.1, for ggsave ~1.49, and with reprex, I need 1.525 (see the example). However, once found the constant, it is actually fairly consistent. At least - for me here.
Works also for negative hjust!
library(ggplot2)
set.seed(10)
df <- data.frame(x = 1:10, y = round(rnorm(10) * 1:10, 2))
p <- ggplot(df, aes(x, y)) +
labs(
title = "Here comes my title like that some words",
subtitle = "and my second line title"
)
hjusttitle <- 0.175
p +
theme(
plot.title = element_text(hjust = hjusttitle, size = 14),
plot.subtitle = element_text(hjust = hjusttitle / 1.525, size = 14)
)
hjusttitle <- -0.15
p +
theme(
plot.title = element_text(hjust = hjusttitle, size = 14),
plot.subtitle = element_text(hjust = hjusttitle / 1.525, size = 14)
)
hjusttitle <- 1
p +
theme(
plot.title = element_text(hjust = hjusttitle, size = 14),
plot.subtitle = element_text(hjust = hjusttitle / 1.525, size = 14)
)
Created on 2021-01-04 by the reprex package (v0.3.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With