可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I would like to left align the title in a plot like this
ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
ggtitle("Unemployment in USA between 1967 and 2007") +
xlab("") +
ylab("Unemployed [thousands]")
First attempt
ggplot(data = economics, aes(x = date, y = unemploy)) + geom_line() +
ggtitle("Unemployment in USA for some years") +
xlab("") +
ylab("Unemployed [thousands]") +
theme(plot.title = element_text(hjust = -0.45, vjust=2.12)))
Yay success! But wait... there's more... now I want to change the title to something else.
ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
ggtitle("Unemployment in USA between 1967 and 2007") +
xlab("") +
ylab("Unemployed [thousands]") +
theme(plot.title = element_text(hjust = -0.45, vjust=2.12))
So now I need to adjust hjust... :(
The question
How can I make the title left justified (a couple of pixels left of the y axis label or so) over and over again without messing with the hjust value? Or what is the relationship between hjust and the length of the string?
I have tried to annotate manually according to this question, but then I got only the title, and nothing else for some reason - and an error.
Thank you!
回答1:
Until someone comes up with a better solution, one way would be something like
library(ggplot2)
library(grid)
library(gridExtra)
p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
labs(x = NULL, y = "Unemployed [thousands]", title = NULL)
title.grob <- textGrob(
label = "Unemployment in USA for some years",
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)
回答2:
You can manually adjust the layout of the ggplot
output. First, we set up the basic plot:
library(ggplot2)
p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
labs(title = "Unemployment in USA between 1967 and 2007",
x = NULL, y = "Unemployed [thousands]")
We can now convert the ggplot
object into a gtable
object, and inspect the layout of the elements in the plot. Notice that the title
is in the fourth column of the grid, the same column as the main panel.
g <- ggplotGrob(p)
g$layout
# t l b r z clip name
# 17 1 1 10 7 0 on background
# 1 5 3 5 3 5 off spacer
# 2 6 3 6 3 7 off axis-l
# 3 7 3 7 3 3 off spacer
# 4 5 4 5 4 6 off axis-t
# 5 6 4 6 4 1 on panel
# 6 7 4 7 4 9 off axis-b
# 7 5 5 5 5 4 off spacer
# 8 6 5 6 5 8 off axis-r
# 9 7 5 7 5 2 off spacer
# 10 4 4 4 4 10 off xlab-t
# 11 8 4 8 4 11 off xlab-b
# 12 6 2 6 2 12 off ylab-l
# 13 6 6 6 6 13 off ylab-r
# 14 3 4 3 4 14 off subtitle
# 15 2 4 2 4 15 off title
# 16 9 4 9 4 16 off caption
To align the title
with the left edge of the plot, we can change the l
value to 1.
g$layout$l[g$layout$name == "title"] <- 1
Draw the modified grid:
grid::grid.draw(g)
Result:
回答3:
Another way to do this is to utilize theme()
. Use the labs
function to label all your titles x =
for x axis, y =
for y axis, title =
for plot title, fill =
or colour =
if you have a legend you want to put a title. Inside theme()
is hjust = 0
, this will left justify the plot title to the left. You can delete hjust = 0
and the plot title will be centered align.
labs(x = 'Sex', y = 'Age Mean', title = 'Suicide 2003-2013 Age Mean by Sex') +
theme(plot.title = element_text(family = 'Helvetica',
color = '#666666',
face = 'bold',
size = 18,
hjust = 0))
回答4:
I wrote the ggdraw()
layer in cowplot
specifically so that I could make annotations easily anywhere on a plot. It sets up a coordinate system that covers the entire plot area, not just the plot panel, and runs from 0 to 1 in both the x and y direction. Using this approach, it is easy to place your title wherever you want.
library(cowplot)
theme_set(theme_gray()) # revert to ggplot2 default theme
p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
ggtitle("") + # make space for title on the plot
xlab("") +
ylab("Unemployed [thousands]")
ggdraw(p) + draw_text("Unemployment in USA between 1967 and 2007",
x = 0.01, y = 0.98, hjust = 0, vjust = 1,
size = 12) # default font size is 14,
# which is too big for theme_gray()
回答5:
This question refers to this github tidyverse/ggplot2 solved issue: https://github.com/tidyverse/ggplot2/issues/3252
And it is implemented in ggplot2 (development version): https://github.com/tidyverse/ggplot2/blob/15263f7580d6b5100989f7c1da5d2f5255e480f9/NEWS.md
Themes have gained two new parameters, plot.title.position and plot.caption.position, that can be used to customize how plot title/subtitle and plot caption are positioned relative to the overall plot (@clauswilke, #3252).
To follow your example as a reprex:
# First install the development version from GitHub:
#install.packages("devtools") #If required
#devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.2.1.9000'
ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
labs(x=NULL,
y="Unemployed [thousands]",
title = "Unemployment in USA for some years",
subtitle = "A subtitle possibly",
caption = "NOTE: Maybe a caption too in italics.") +
theme(plot.caption = element_text(hjust = 0, face= "italic"), #Default is hjust=1
plot.title.position = "plot", #NEW parameter. Apply for subtitle too.
plot.caption.position = "plot") #NEW parameter
Created on 2019-09-04 by the reprex package (v0.3.0)