R 水平柱状图的标题、副标题和标题对齐
我想将水平ggplot2条形图中的R 水平柱状图的标题、副标题和标题对齐,r,plot,ggplot2,R,Plot,Ggplot2,我想将水平ggplot2条形图中的plot.title、plot.subtitle和plot.caption左对齐 例如: 库(“ggplot2”)#ggplot2.2 df虽然您可以编辑这三个grob,但您也可以: library(gridExtra) library(grid) grid.arrange( textGrob("This is a nice title", gp=gpar(fontsize=16, col="#2b2b2b"),
plot.title
、plot.subtitle
和plot.caption
左对齐
例如:
库(“ggplot2”)#ggplot2.2
df虽然您可以编辑这三个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)
)
为它做一个包装,把它放在一个个人包裹里。繁荣完成了
库(ggplot2)
图书馆(gridExtra)
图书馆(网格)
df-gg
flush_plot此问题指的是github tidyverse/ggplot2解决的问题:
并在ggplot2(开发版本)中实现:
Themes获得了两个新参数plot.title.position和plot.caption.position,可用于自定义相对于整个情节的情节标题/副标题和情节标题的定位方式(@clauswilke,#3252)
要以reprex为例:
#首先从GitHub安装开发版本:
#如果需要,安装.packages(“devtools”)#
#devtools::install_github(“tidyverse/ggplot2”)
图书馆(GG2)
包装版本(“ggplot2”)
#> [1] '3.2.1.9000'
df这将对齐标题、副标题和标题,但似乎不会对齐y轴标签。plot.margin=unit(c(0,0,0,0),“line”)
将修复此问题that@hrbrmstr回答得好!但老实说:我更喜欢纯ggplot2解决方案,尽管代码更少……如果有一个/-:这是一个很好的问题,说明得很好,解释得很清楚。这是我在使用ggplot时经常遇到的一个问题。虽然下面的grid.arrange()选项是可行的,但对我来说,它并不是这个问题的完整答案。例如,为什么相同的hjust值对标题、副标题和标题的影响不一样?我认为答案是,在每种情况下,参考点都是文本字符串的中间。虽然这对于居中对齐的文本是合理的,但对于打印区域以外的左对齐或右对齐则不起作用。看起来这应该是一个简单的设置。
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)
)
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.")