首页 > 解决方案 > 基于因子循环多个绘图对象

问题描述

我想根据一个因素生成单独的绘图对象,以便我可以使用 grid_arrange 而不是 facet_grid 将它们绘制在一起 - 因为我发现这很笨重。

我想我需要一个 for 循环,但我不太了解它们 - 如果这是我需要的,你能详细说明它是如何工作的。

p <- ggplot(All, aes(x=variable, y=value, fill = Type))
p <- p + geom_bar(stat="identity" ) + facet_grid(~ Month)
p   


#dummy data
All <- structure(list(Type = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("Cargo ship", "Cargo ship:DG,HS,MP(A)", 
"Cargo ship:DG,HS,MP(B)", "Cargo ship:DG,HS,MP(D)", "Fishing", 
"Other:DG,HS,MP(B)", "Tanker", "Tanker:DG,HS,MP(B)"), class = 
"factor"), 
 Month = c("Jan", "Jan", "Jan", "Nov", "Jan", "Jan", "Jan", 
"Nov", "Jan", "Mar", "Jan", "Jan", "Jan", "Jan", "Jan", "Nov", 
 "Jan", "Mar", "Nov", "Mar", "Mar", "Feb", "Mar", "Mar", "Nov", 
 "Nov", "Jan", "Feb", "Mar", "Mar", "Nov", "Nov", "Dec", "Dec", 
 "Dec", "Dec", "Jan", "Jan", "Jan", "Jan", "Jan", "Jan", "Jan"
 ), Year = c(2019, 2019, 2019, 2018, 2019, 2019, 2019, 2018, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2018, 2019, 2019, 
2018, 2019, 2019, 2019, 2019, 2019, 2018, 2018, 2019, 2019, 
2019, 2019, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 
2019, 2019, 2019, 2019, 2019), variable = structure(c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0-12", 
"0-25", "0-50", "0-100"), class = "factor"), value = c(1, 
0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
0, 0, 0, 0)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 360L, 361L, 362L, 363L, 364L, 365L, 366L, 367L, 368L, 
369L, 370L, 3300L, 3301L, 3302L, 3303L, 3304L, 3305L, 3306L, 
3307L, 3308L, 3309L, 3310L, 2460L, 2461L, 2462L, 2463L, 2464L, 
2465L, 2466L, 2467L, 2468L, 2469L, 2470L), class = "data.frame")

我希望数据集中每个月都有多个绘图对象。

标签: rloopsggplot2facet

解决方案


您可以通过Month(use ) 拆分数据框,然后使用&group_split遍历该列表mapplot_function()

library(tidyverse)
theme_set(theme_minimal(base_size = 14))

plot_function <- function(df) {
  p <- ggplot(df, aes(x = Month, y = value, fill = Type))
  p <- p + geom_col() +
    scale_fill_manual("",
                       values = c('Cargo ship' = '#7570b3',
                                  'Fishing' = '#1b9e77',
                                  'Tanker'='#d95f02'))
  return(p)
}

# Save all plots in a list
plot_list <- All %>% 
  mutate(Month = factor(Month, levels = c("Jan", "Feb", "Mar", "Nov", "Dec"))) %>% 
  group_split(Month) %>% 
  map(~ plot_function(.x))

# Combine all plots into one
cowplot::plot_grid(plotlist = plot_list, 
                   nrow  = 3,
                   align = 'hv',
                   axis  = 'tblr')

编辑:只保留 1 个常见的图例

# remove all legends
all_plot <- cowplot::plot_grid(plotlist = 
  lapply(seq_along(plot_list), function(x) {plot_list[[x]] + theme(legend.position = 'none')}), 
                   nrow = 3,
                   align = 'hv',
                   axis = 'tblr')

# extract legend from one plot              
common_legend <- cowplot::get_legend(plot_list[[1]] + theme(legend.position = 'bottom'))

# combine plot and legend
p <- cowplot::plot_grid(all_plot, common_legend, 
                        nrow = 2,
                        rel_heights = c(3, .3))
p

reprex 包(v0.2.1)于 2019 年 5 月 10 日创建


推荐阅读