代码之家  ›  专栏  ›  技术社区  ›  John Conor

为面板组设置单独的配色方案+ggplot2+facet_wrap

  •  1
  • John Conor  · 技术社区  · 3 年前

    我正在尝试使用ggplot 2为面板组分配单独的颜色方案。在下面的例子中,我的目标是A和B将具有相同的托盘,C将具有不同的托盘,D和E将具有相同托盘。

    # Number of IDs
    n_ids <- 10 
    
    # Generate IDs
    ids <- paste0("ID_", 1:n_ids)
    
    # Number of periods per ID
    n_periods <- 3
    
    # Generate periods
    periods <- rep(c("P1", "P2", "P3"), each = n_ids)
    
    # Number of categories 
    n_cats <- 5 
    
    # Generate categories
    categories <- rep(LETTERS[1:n_cats], times = n_ids * n_periods)
    
    # Generate values
    values <- rnorm(n_ids * n_periods * n_cats, 100, 15)
    
    # Assemble into dataframe
    df <- data.frame(ID = ids, 
                     Period = periods,
                     Category = categories,
                     Value = values)
    
    # Plot
    p <- ggplot(df, aes(x=Period, y=Value, fill=Period)) +
      geom_boxplot() +
      geom_jitter(width=0.2,alpha=0.4, size=1) +
      facet_wrap(~Category, ncol=6, scales='free') +
      labs(title="Repetitive Behavior Score", x="Group", y="Value")
    
    # Color vector
    colors <- c("#1F78B4", "#E69F00", "#33A02C", "#FB9A99")
    
    # Map colors to categories
    color_map <- c(
      "A" = "#1F78B4",
      "B" = "#1F78B4",
      
      "C" = "#E69F00",
      
      "D" = "#33A02C",
      "E" = "#33A02C"
    )
    
    # Set fill scale
    p + 
      scale_fill_manual(values = color_map)
    

    但他们都只是恢复了灰色。。。以前有人试过这个吗?我知道这是可以做到的 scale_fill 和 scale_color 但我找不到镶嵌面板的例子。

    2 回复  |  直到 3 年前
        1
  •  3
  •   Jon Spring    3 年前

    另一种方法可以是将情节与拼凑相结合:

    library(ggplot2)
    library(patchwork)
    plot_func <- function(df1, title = "Repetitive Behavior Score") {
      ggplot(df1, aes(x=Period, y=Value, fill=Period)) +
      geom_boxplot() +
      geom_jitter(width=0.2,alpha=0.4, size=1) +
      facet_wrap(~Category, ncol=6, scales='free') +
      labs(title=title, x="Group", y="Value") 
    }
    
    plot_func(subset(df, Category %in% c("A", "B"))) + 
      plot_func(subset(df, Category %in% c("C")), title = "") +
      scale_fill_viridis_d() +
      plot_func(subset(df, Category %in% c("D", "E")), title = "") + 
      scale_fill_brewer(palette = "Set3") +
      plot_layout(widths = c(2,1,2))
    

    enter image description here

        2
  •  2
  •   Allan Cameron    3 年前

    您可以将填充美学映射到 interaction 你的x轴和面变量。缺点是需要手动指定颜色。

    ggplot(df, aes(Period, Value, fill = interaction(Period, Category))) +
      geom_boxplot() +
      geom_jitter(width = 0.2, alpha = 0.4, size = 1) +
      facet_wrap(~Category, ncol = 6, scales = 'free') +
      labs(title = "Repetitive Behavior Score", x = "Group", y =" Value") +
      scale_fill_manual(values = c(rep(c("red4", "green4", "blue4"), 2),
                                   c("hotpink", "orange", "dodgerblue"),
                                   rep(c("purple3", "beige", "gray80"), 2)),
                        guide = guide_none()) +
      jtools::theme_apa() 
    

    enter image description here

        3
  •  0
  •   John Conor    3 年前

    事实上,这很容易,我所需要做的就是改变 fill 在里面 ggplot(df, aes(x=Period, y=Value, fill=Category)) 到 Category 。见下文:

    # Number of IDs
    n_ids <- 10 
    
    # Generate IDs
    ids <- paste0("ID_", 1:n_ids)
    
    # Number of periods per ID
    n_periods <- 3
    
    # Generate periods
    periods <- rep(c("P1", "P2", "P3"), each = n_ids)
    
    # Number of categories 
    n_cats <- 5 
    
    # Generate categories
    categories <- rep(LETTERS[1:n_cats], times = n_ids * n_periods)
    
    # Generate values
    values <- rnorm(n_ids * n_periods * n_cats, 100, 15)
    
    # Assemble into dataframe
    df <- data.frame(ID = ids, 
                     Period = periods,
                     Category = categories,
                     Value = values)
    
    # Plot
    p <- ggplot(df, aes(x=Period, y=Value, fill=Category)) +
      geom_boxplot() +
      geom_jitter(width=0.2,alpha=0.4, size=1) +
      facet_wrap(~Category, ncol=6, scales='free') +
      labs(title="Repetitive Behavior Score", x="Group", y="Value") +
      theme_apa() 
    
    # Color vector
    colors <- c("#1F78B4", "#E69F00", "#33A02C", "#FB9A99")
    
    # Map colors to categories
    color_map <- c(
      "A" = "#1F78B4",
      "B" = "#1F78B4",
      
      "C" = "#E69F00",
      
      "D" = "#33A02C",
      "E" = "#33A02C"
    )
    
    # Set fill scale
    p + scale_fill_manual(values = color_map)