gpt4 book ai didi

r - ggplot2:使用cowplot创建主题标题、副标题

转载 作者:行者123 更新时间:2023-12-02 08:55:24 30 4
gpt4 key购买 nike

我有一个数据框列表,我用它来制作 ggplot 列表s,然后用 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 中的所有内容。到它,创建副标题和说明文字也是如此。

我当前的解决方法是创建一个空的 ggplotlabs对于标题和副标题,对标题执行相同的操作,并将它们垂直堆叠为 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于2018年6月21日创建(v0.2.0)。

关于r - ggplot2:使用cowplot创建主题标题、副标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50973713/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com