问题
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 Is there a way to make the start of the two lines of the title flush after adjusting them horizontally?
回答1:
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:
Please let me know whether this is what you want.
回答2:
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")
回答3:
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)`
回答4:
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))
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))
Hope that help !
来源:https://stackoverflow.com/questions/56662815/unequal-horizontal-adjustment-to-title-lines-in-ggplot