我想左对齐plot.title,plot.subtitle并plot.caption在水平ggplot2条形图中.
例:
library("ggplot2") # ggplot2 2.2
df <- data.frame(type=factor(c("Brooklyn",
"Manhatten and\n Queens")),
value=c(15,30))
# manual hjust for title, subtitle & caption
myhjust <- -0.2
ggplot(df,
aes(x=type, y=value)) +
geom_bar(stat='identity') +
coord_flip() +
labs(
title = "This is a nice title",
subtitle = "A subtitle",
caption = "We even have a caption. A very long one indeed.") +
theme(axis.title=element_blank(),
plot.title=element_text(hjust = myhjust),
plot.subtitle=element_text(hjust = myhjust ),
plot.caption=element_text(hjust = myhjust))
Run Code Online (Sandbox Code Playgroud)
我该如何调整所有3个labs元素(plot.title,plot.subtitle和plot.caption),可在axis.text开始(红色垂直线,曼哈顿的"M")?
另外:为什么固定的 myhjust结果是3个不同的水平位置plot.title,plot.subtitle和plot.caption?
小智 18
这个问题是指这个github tidyverse/ggplot2解决的问题:https : //github.com/tidyverse/ggplot2/issues/3252
它在ggplot2(开发版)中实现:https : //github.com/tidyverse/ggplot2/blob/15263f7580d6b5100989f7c1da5d2f5255e480f9/NEWS.md
主题获得了两个新参数,plot.title.position 和 plot.caption.position,可用于自定义情节标题/副标题和情节标题相对于整体情节的定位方式(@clauswilke,#3252)。
以您的示例为代表:
# 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'
df <- data.frame(type=factor(c("Brooklyn","Manhatten and\n Queens")),
value=c(15,30))
ggplot(df, aes(x=type, y=value)) +
geom_bar(stat='identity') +
coord_flip() +
labs(title = "This is a nice title",
subtitle = "A subtitle",
caption = "We even have a caption. A very long one indeed.") +
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
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2019 年 9 月 4 日创建
hrb*_*str 10
虽然您可以编辑这三个grob,但您也可以:
library(gridExtra)
library(grid)
grid.arrange(
textGrob("This is a nice title",
gp=gpar(fontsize=16, col="#2b2b2b"),
x=unit(0.005, "npc"), just=c("left", "bottom")),
textGrob("A subtitle",
gp=gpar(fontsize=12, col="#2b2b2b"),
x=unit(0.005, "npc"), just=c("left", "bottom")),
ggplot(df, aes(x=type, y=value)) +
geom_bar(stat='identity') +
coord_flip() +
theme(axis.title=element_blank()),
textGrob("We even have a caption. A very long one indeed.",
gp=gpar(fontsize=9, col="#2b2b2b"),
x=unit(0.005, "npc"), just=c("left", "bottom")),
ncol=1,
heights=c(0.075, 0.025, 0.85, 0.05)
)
Run Code Online (Sandbox Code Playgroud)
为它做一个包装,把它放在个人的pkg.繁荣.完成.
library(ggplot2)
library(gridExtra)
library(grid)
df <- data.frame(type=factor(c("Brooklyn","Manhatten and\n Queens")), value=c(15,30))
ggplot(df, aes(x=type, y=value)) +
geom_bar(stat='identity') +
coord_flip() +
theme(axis.title=element_blank()) +
theme(plot.margin=margin(l=0, t=5, b=5))-> gg
flush_plot <- function(x, title, subtitle, caption) {
tg <- function(label, ...) {
textGrob(label, x=unit(0, "npc"), just=c("left", "bottom"),
gp=do.call(gpar, as.list(substitute(list(...)))[-1L])) }
grid.arrange(
tg(title, fontsize=16, col="#2b2b2b"),
tg(subtitle, fontsize=12, col="#2b2b2b"), x,
tg(caption, fontsize=9, col="#2b2b2b"),
ncol=1, heights=c(0.075, 0.025, 0.85, 0.05)
)
}
flush_plot(gg, "This is a nice title", "A subtitle",
"We even have a caption. A very long one indeed.")
Run Code Online (Sandbox Code Playgroud)