Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align a multiline title [duplicate]

Tags:

r

ggplot2

title

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

enter image description here

Desirable plot

enter image description here

like image 429
badalovi Avatar asked Jan 03 '21 17:01

badalovi


People also ask

How to center align multi-line text in GraphViz?

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 .

How to align titles in visualization using Plotly module?

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.

What is title_Y in AutoCAD?

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.

How do I center align text in word?

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:


Video Answer


3 Answers

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)

enter image description here

You can change 4.5 as necessary for the desired effect.

like image 174
Ian Campbell Avatar answered Oct 27 '22 11:10

Ian Campbell


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))
 )
like image 31
user20650 Avatar answered Oct 27 '22 11:10

user20650


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)

like image 2
tjebo Avatar answered Oct 27 '22 11:10

tjebo