ggplot2:创建主题标题,带有 cowplot 的副标题
ggplot2: Creating themed title, subtitle with cowplot
我有一个数据框列表,我用它来制作 ggplot
的列表,然后 assemble 进入带有 cowplot
的绘图网格。然后我需要附加一个共享标题、副标题和标题。我想以这些标签将具有相同主题元素(大小、字体等)的方式来执行此操作,就好像它们是由 labs
而不是 cowplot::draw_label
.
创建的一样
在我的真实情况下,我有几个分区表,每个分区表都有自己的单位和比例,这就是为什么我不能只分面,而是需要构建彼此独立的地块。
这是数据的简化版本,以及我加载的包:
library(tidyverse)
library(cowplot)
dfs <- list(
adults_no_diploma = structure(list(
tract = c("09003405100", "09003405200", "09003405300", "09003405401", "09003405402", "09003405500", "09003405600", "09003405700", "09003405800", "09003405900"),
value = c(0.08, 0.108, 0.095, 0.099, 0.105, 0.103, 0.161, 0.279, 0.056, 0.055)),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")),
severe_cost_burden = structure(list(
tract = c("09003405100", "09003405200", "09003405300", "09003405401", "09003405402", "09003405500", "09003405600", "09003405700", "09003405800", "09003405900"),
value = c(0.128, 0.147, 0.165, 0.1, 0.151, 0.11, 0.179, 0.184, 0.14, 0.038)),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
)
我正在使用自定义主题(同样是简化版):
theme_georgia <- function(...) {
theme_gray(base_family = "Georgia", ...) +
theme(plot.title = element_text(face = "bold"))
}
plots <- dfs %>%
imap(~{
ggplot(.x, aes(x = value)) +
geom_density() +
ggtitle(.y) +
theme_georgia()
})
gridded <- plot_grid(plotlist = plots, nrow = 1)
在 cowplot
annotations vignette 之后,我可以使用 ggdraw() + draw_label("Socio-economic measures")
制作标题并手动设置字体大小等内容,但我更喜欢以某种方式将标签定义为 一个标题;也就是说,主题会应用 plot.title
中的所有内容,创建副标题和标题也是如此。
我目前的解决方法是为标题和副标题创建一个空的 ggplot
和 labs
,对标题做同样的事情,然后用 cowplot::plot_grid
垂直堆叠它们。
title_gg <- ggplot() +
labs(title = "Socio-economic measures", subtitle = "By census tract, 2016") +
theme_georgia()
plot_grid(title_gg, gridded, ncol = 1, rel_heights = c(0.15, 1))
解决方法还可以,但我喜欢 draw_label
带来的整洁和对齐。我可以将这些主题元素一一添加以模仿标题:
title_theme <- ggdraw() +
draw_label("Socio-economic measures",
fontfamily = theme_georgia()$text$family,
fontface = theme_georgia()$plot.title$face, x = 0.05, hjust = 0)
plot_grid(title_theme, gridded, ncol = 1, rel_heights = c(0.2, 1))
我的问题是,我是否有任何方法可以结合这些方法来提取所有相关主题元素并将它们快速提供给 draw_label
,或者以某种方式告诉 draw_label
这个东西是一个标题和应该得到标题主题元素,而另一个是副标题,等等。我想象这样的魔法:
ggdraw() +
draw_label("Socio-economic measures", theme_georgia()$plot.title_elements)
或:
ggdraw() +
draw_label("Socio-economic measures", type = "title") + theme_georgia()
像这样?
library(ggplot2)
library(cowplot)
theme_georgia <- function(...) {
theme_gray(base_family = "Georgia", ...) +
theme(plot.title = element_text(face = "bold"))
}
title_theme <- calc_element("plot.title", theme_georgia())
ggdraw() +
draw_label(
"Socio-economic measures",
fontfamily = title_theme$family,
fontface = title_theme$face,
size = title_theme$size
)
由 reprex package (v0.2.0) 创建于 2018-06-21。
@Claus Wilke 的解决方案完成了工作,但我受到启发围绕 draw_label
编写包装函数以获得模仿主题元素所需的样式。我在这里发帖以防它对其他人有用,尽管这可能是一个奇怪的用例。
此函数采用主题函数,或者如果省略 theme
,则从 theme_get
获取当前主题。它还采用主题元素的名称,例如 "plot.title"
,它从中获取带有 calc_element
的样式。所有必需的参数都传递给 cowplot::draw_label
,连同 ...
中的任何其他内容,例如 x
或 hjust
.
library(tidyverse)
library(cowplot)
draw_label_theme <- function(label, theme = NULL, element = "text", ...) {
if (is.null(theme)) {
theme <- ggplot2::theme_get()
}
if (!element %in% names(theme)) {
stop("Element must be a valid ggplot theme element name")
}
elements <- ggplot2::calc_element(element, theme)
cowplot::draw_label(label,
fontfamily = elements$family,
fontface = elements$face,
colour = elements$color,
size = elements$size,
...
)
}
title <- ggdraw() +
draw_label_theme("Socio-economic measures",
theme = theme_georgia(), element = "plot.title",
x = 0.05, hjust = 0, vjust = 1)
subtitle <- ggdraw() +
draw_label_theme("By census tract, 2016",
theme = theme_georgia(), element = "plot.subtitle",
x = 0.05, hjust = 0, vjust = 1)
现在唯一困难的部分是在 rel_heights
中整齐地排列东西,我可以在这里弄乱更多东西。可能会有定位信息,可以拉出主题,用来设置高度。
plot_grid(title, subtitle, gridded, ncol = 1, rel_heights = c(0.1, 0.1, 1))
我有一个数据框列表,我用它来制作 ggplot
的列表,然后 assemble 进入带有 cowplot
的绘图网格。然后我需要附加一个共享标题、副标题和标题。我想以这些标签将具有相同主题元素(大小、字体等)的方式来执行此操作,就好像它们是由 labs
而不是 cowplot::draw_label
.
在我的真实情况下,我有几个分区表,每个分区表都有自己的单位和比例,这就是为什么我不能只分面,而是需要构建彼此独立的地块。
这是数据的简化版本,以及我加载的包:
library(tidyverse)
library(cowplot)
dfs <- list(
adults_no_diploma = structure(list(
tract = c("09003405100", "09003405200", "09003405300", "09003405401", "09003405402", "09003405500", "09003405600", "09003405700", "09003405800", "09003405900"),
value = c(0.08, 0.108, 0.095, 0.099, 0.105, 0.103, 0.161, 0.279, 0.056, 0.055)),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")),
severe_cost_burden = structure(list(
tract = c("09003405100", "09003405200", "09003405300", "09003405401", "09003405402", "09003405500", "09003405600", "09003405700", "09003405800", "09003405900"),
value = c(0.128, 0.147, 0.165, 0.1, 0.151, 0.11, 0.179, 0.184, 0.14, 0.038)),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
)
我正在使用自定义主题(同样是简化版):
theme_georgia <- function(...) {
theme_gray(base_family = "Georgia", ...) +
theme(plot.title = element_text(face = "bold"))
}
plots <- dfs %>%
imap(~{
ggplot(.x, aes(x = value)) +
geom_density() +
ggtitle(.y) +
theme_georgia()
})
gridded <- plot_grid(plotlist = plots, nrow = 1)
在 cowplot
annotations vignette 之后,我可以使用 ggdraw() + draw_label("Socio-economic measures")
制作标题并手动设置字体大小等内容,但我更喜欢以某种方式将标签定义为 一个标题;也就是说,主题会应用 plot.title
中的所有内容,创建副标题和标题也是如此。
我目前的解决方法是为标题和副标题创建一个空的 ggplot
和 labs
,对标题做同样的事情,然后用 cowplot::plot_grid
垂直堆叠它们。
title_gg <- ggplot() +
labs(title = "Socio-economic measures", subtitle = "By census tract, 2016") +
theme_georgia()
plot_grid(title_gg, gridded, ncol = 1, rel_heights = c(0.15, 1))
解决方法还可以,但我喜欢 draw_label
带来的整洁和对齐。我可以将这些主题元素一一添加以模仿标题:
title_theme <- ggdraw() +
draw_label("Socio-economic measures",
fontfamily = theme_georgia()$text$family,
fontface = theme_georgia()$plot.title$face, x = 0.05, hjust = 0)
plot_grid(title_theme, gridded, ncol = 1, rel_heights = c(0.2, 1))
我的问题是,我是否有任何方法可以结合这些方法来提取所有相关主题元素并将它们快速提供给 draw_label
,或者以某种方式告诉 draw_label
这个东西是一个标题和应该得到标题主题元素,而另一个是副标题,等等。我想象这样的魔法:
ggdraw() +
draw_label("Socio-economic measures", theme_georgia()$plot.title_elements)
或:
ggdraw() +
draw_label("Socio-economic measures", type = "title") + theme_georgia()
像这样?
library(ggplot2)
library(cowplot)
theme_georgia <- function(...) {
theme_gray(base_family = "Georgia", ...) +
theme(plot.title = element_text(face = "bold"))
}
title_theme <- calc_element("plot.title", theme_georgia())
ggdraw() +
draw_label(
"Socio-economic measures",
fontfamily = title_theme$family,
fontface = title_theme$face,
size = title_theme$size
)
由 reprex package (v0.2.0) 创建于 2018-06-21。
@Claus Wilke 的解决方案完成了工作,但我受到启发围绕 draw_label
编写包装函数以获得模仿主题元素所需的样式。我在这里发帖以防它对其他人有用,尽管这可能是一个奇怪的用例。
此函数采用主题函数,或者如果省略 theme
,则从 theme_get
获取当前主题。它还采用主题元素的名称,例如 "plot.title"
,它从中获取带有 calc_element
的样式。所有必需的参数都传递给 cowplot::draw_label
,连同 ...
中的任何其他内容,例如 x
或 hjust
.
library(tidyverse)
library(cowplot)
draw_label_theme <- function(label, theme = NULL, element = "text", ...) {
if (is.null(theme)) {
theme <- ggplot2::theme_get()
}
if (!element %in% names(theme)) {
stop("Element must be a valid ggplot theme element name")
}
elements <- ggplot2::calc_element(element, theme)
cowplot::draw_label(label,
fontfamily = elements$family,
fontface = elements$face,
colour = elements$color,
size = elements$size,
...
)
}
title <- ggdraw() +
draw_label_theme("Socio-economic measures",
theme = theme_georgia(), element = "plot.title",
x = 0.05, hjust = 0, vjust = 1)
subtitle <- ggdraw() +
draw_label_theme("By census tract, 2016",
theme = theme_georgia(), element = "plot.subtitle",
x = 0.05, hjust = 0, vjust = 1)
现在唯一困难的部分是在 rel_heights
中整齐地排列东西,我可以在这里弄乱更多东西。可能会有定位信息,可以拉出主题,用来设置高度。
plot_grid(title, subtitle, gridded, ncol = 1, rel_heights = c(0.1, 0.1, 1))