ggplot2:创建主题标题,带Cowplot的字幕 [英] ggplot2: Creating themed title, subtitle with cowplot
问题描述
我有一个数据框列表,用于创建一个ggplot
列表,然后使用cowplot
组合成一个网格图.然后,我需要附加一个共享的标题,字幕和标题.我想用一种方式来使这些标签具有相同的主题元素(大小,字体等),就像它们是由labs
而不是cowplot::draw_label
创建的一样.
I have a list of data frames that I use to make a list of ggplot
s, and then assemble into a grid of plots with cowplot
. I need to then attach a shared title, subtitle, and caption. I want to do this in a way that these labels will have the same theme elements (size, fontface, etc) as if they were created by labs
instead of cowplot::draw_label
.
在我的实际情况下,我有几个choropleth,每个choropleth都有自己的单位和比例,这就是为什么我不能只是刻面,而是需要彼此独立地构建图.
In my real situation, I have several choropleths that each have their own units and scales, which is why I can't just facet, but instead need to build the plots independent of one another.
这是数据的简化版本,以及我已加载的软件包:
Here's a simplified version of the data, and the packages I have loaded:
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"))
)
我正在使用自定义主题(同样是简化版):
I'm working with a custom theme (again, a simplified version):
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
注释插图之后,我可以用ggdraw() + draw_label("Socio-economic measures")
做一个标题,并手动设置字体大小,但是我更喜欢以某种方式将标签定义为.也就是说,主题会将plot.title
中的所有内容应用到该主题,并且创建字幕和标题也是如此.
Following the cowplot
annotations vignette, I can make a title with ggdraw() + draw_label("Socio-economic measures")
and manually set things like font size, but what I'd prefer is to somehow define that label as a title; that is, the theme would apply everything in plot.title
to it, and the same for creating a subtitle and caption.
我当前的解决方法是使用标题和副标题为labs
的空ggplot
,对标题进行相同的操作,然后使用cowplot::plot_grid
将其垂直堆叠.
My current workaround is to make an empty ggplot
with labs
for the title and subtitle, do the same for a caption, and stack them vertically with 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
带来的整洁和对齐.相反,我可以一个个地添加这些主题元素来模仿标题:
The workaround is okay, but I like the neatness and alignment you get with draw_label
. I could instead add in these theme elements one by one to mimic a title:
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
这件事是标题,应该获得标题主题元素,而另一件事是字幕,依此类推.我想象这样的魔术:
My question is whether there's any way I might combine these approaches to pull all the relevant theme elements and feed them to draw_label
quickly, or somehow tell draw_label
that this thing is a title and should get title theme elements, and this other thing is a subtitle, and so on. I imagine some sort of magic like this:
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软件包(v0.2.0)于2018-06-21创建.
Created on 2018-06-21 by the reprex package (v0.2.0).
这篇关于ggplot2:创建主题标题,带Cowplot的字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!