2

I've got a plot with a fairly wide y-axis label, so I want to adjust the title to the left so that it's flush with the label rather than the axis (like this question) but the twist is that I have a multiline title. I've been using hjust, but it adjusts the two lines differently. For example

ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point()+
  ggtitle("Figure: My long and winding title\nthat goes on and on and on") +
  ylab("My long label") +
  theme_bw() +
  theme(plot.title = element_text(size=16, hjust=-.33, color="black", face="bold")) +
  theme(axis.title.y = element_text(angle = 0, hjust = 1))

Gives plot output Is there a way to make the start of the two lines of the title flush after adjusting them horizontally?

pgcudahy
  • 1,542
  • 13
  • 36

4 Answers4

2

You could use the following code. First create the plot and assign it to g, then turn g in to a grob with ggplotGrob. Consequently, manipulate the title left alignment in the layout part of the grob (from 5 to 2). And Finally plot the adapted grob.

g <- ggplot(mtcars,aes(x=wt,y=mpg)) + 
  geom_point() + 
  ggtitle("Figure: My long and winding title\nthat goes on and on and on") + 
  ylab("My long label") + 
  theme_bw() + 
  theme(plot.title = element_text(size=16,  color="black", face="bold"))  + 
  theme(axis.title.y = element_text(angle = 0, hjust = 1))


grob <- ggplotGrob(g)

# what is the current content
grob$layout$l[grob$layout$name == "title"]
[1] 5

grob$layout$l[grob$layout$name == "title"] <- 2

# plot the new grob
grid::grid.draw(grob)

yielding this plot:

enter image description here

Please let me know whether this is what you want.

KoenV
  • 4,113
  • 2
  • 23
  • 38
0

Sorry I misunderstood your question.

I think you're just missing a space in the title.

ggtitle("Figure: My long and winding title\n that goes on and on and on")

figure

heds1
  • 3,203
  • 2
  • 17
  • 32
  • I already have a plot.title in theme with hjust. This new one just overrides it. If I set it to the original -.33 I get the same issue – pgcudahy Jun 19 '19 at 08:34
  • Sorry why do you want it to be -0.33? Is the output above incorrect? Do you want the title to be to the left of the y-axis? – heds1 Jun 19 '19 at 08:36
  • Yes, "I want to adjust the title to the left so that it's flush with the label rather than the axis" – pgcudahy Jun 19 '19 at 08:37
0

hjust is the responsible of the white space. Removing it remove the 2 white spaces on the second line.

library(ggplot2)

ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point()+
  ggtitle("Figure: My long and winding title\nthat goes on and on and on") +
  ylab("My long label") +
  theme_bw() +
  theme(plot.title = element_text(size=16, 
                                  color="black", face="bold",
                                  ),
        axis.title.y = element_text(angle = 0, hjust = 1))

enter image description here

EDIT 1:

If you want to automatically split the title in multiple line, you can use gsub. Here is one example where I split the title after 30 characters. (source)

long_title <- "Figure: My long and winding title that goes on and on and on"

ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point()+
  ggtitle(gsub('(.{1,30})(\\s|$)', '\\1\n', long_title)) +
  ylab("My long label") +
  theme_bw() +
  theme(plot.title = element_text(size=16, 
                                  color="black", face="bold",
                                  ),
        axis.title.y = element_text(angle = 0, hjust = 1))

enter image description here

Hope that help !

Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
0

Here You have it:

library(ggplot2)
library(grid)
library("gridExtra")
p<-ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point()+
  ggtitle("") +
  ylab("My long label") +
  theme_bw() +theme(axis.title.y = element_text(angle = 0, hjust = 1))


title.grob <- textGrob(
  label = "Figure: My long and winding title\nthat goes on and on and on",
  x = unit(0, "lines"), 
  y = unit(0, "lines"),
  hjust = 0, vjust = 0,
  gp = gpar(fontsize = 16))
p1 <- arrangeGrob(p, top = title.grob)
grid.draw(p1)`

enter image description here

Carles
  • 2,731
  • 14
  • 25