繁体   English   中英

ggplot2 中具有折叠因子值的自定义图例

[英]Custom legend with collapsed factor values in ggplot2

以前可能有人问过这个问题,但我搜索了一段时间后找不到。

我有以下data.frame

structure(list(genotype = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4), treatment = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2), group_val = c(1.57837321136062, 1.76334487045417, 
1.73586017158848, 2.04109599956349, 1.80010448171344, 2.07090618591467, 
1.07574792716769, 1.18397923178828, 1.21889101529495, 1.20248500773822, 
1.3808338457315, 1.42210495550068, 1.64573799027085, 1.55264650622629, 
1.70883543195709, 1.50659245289343, 0.90200663935181, 0.881584819347461, 
0.954018876774318, 0.930280832877143, 1.85156683945601, 1.84753564786241, 
1.96298425756247, 1.97329138022375, 1.89502726316024, 1.88250460242058, 
1.12763625255165, 0.849376374224505, 1.04073813233643, 1.00903241221572, 
1.58053330474755, 1.60670456352336, 2.02389070564365, 1.88873097588837, 
2.05477131909231, 1.9945072156688, 1.25082256791521, 1.19811638234775, 
1.06975634816231, 1.20976663827858, 2.10380372095596, 2.14921911265538, 
2.18892848376085, 2.15381486434453, 1.82607480270083, 1.98677173426624, 
0.954242509439325, 1.26717172840301, 1.02118929906994, 0.8750612633917, 
0.602059991327962, 0.751757501701102, 1.62038696281561, 1.20836885846782, 
1.32651612490137, 1.13698195289592, 1.6421025338509, 1.41206291695827, 
1.6101194399672, 1.6712113404111, 2.11429641123473, 1.84505371972817, 
2.27595666174897, 2.2231986751043, 2.24564757180665, 2.24707729700922, 
1.47310327692139, 1.1447387331723, 1.24550565752405, 1.07766801873253, 
1.85452622982568, 1.87613186339641, 2.09397999968991, 1.96262712830201, 
2.2095435542086, 2.10814923581137, 1.00067107824743, 0.983971241990881, 
1.24468845794328, 1.15181012595794)), row.names = c(NA, -80L), groups = structure(list(
    genotype = c(1, 1, 2, 2, 3, 3, 4, 4), treatment = c(1, 2, 
    1, 2, 1, 2, 1, 2), .rows = structure(list(1:10, 11:20, 21:30, 
        31:40, 41:50, 51:60, 61:70, 71:80), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 8L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

我的目标是拥有以下 plot

在此处输入图像描述

但是随着interaction(genotype, treatment)的水平崩溃了。 这里唯一相关的信息是“light”colors 等于处理级别 1,“dark”colors 等于处理级别“2”。

我想要一个图例来反映,只有两个点,可以是"#CFCFCF""gray50"来反映treatment的价值分别为1和2。

这是制作 plot 的代码,如图所示

library(tidyverse)
target_colors <- c("#FF9BB4", "#FA234C", "#A2D3FF", "#2987FA", "#47C947", "darkgreen", 
                   "#CFCFCF", "gray50")
color_order <- interaction(df$genotype, df$treatment) %>% levels() %>% sort


df %>% 
  ggplot(aes(genotype, group_val,
             color=interaction(genotype, treatment)))+
  ggbeeswarm::geom_quasirandom(dodge.width = 1,
                               show.legend = T) +
  # if flipping, the levels of the factor must be modified
  #coord_flip()+
  geom_boxplot(
    position=position_dodge(1),
    width=0.1, fill='black', show.legend = F)+
  scale_color_manual(values = setNames(target_colors,
                                       color_order))

一种选择是仅使用四个 colors、map color genotypealpha treatment


library(tidyverse)
target_colors <- c("#FA234C", "#2987FA", "darkgreen", "gray50")

df %>% 
  ggplot(aes(genotype, group_val,
             color = factor(genotype),
             alpha = factor(treatment),
             group = interaction(genotype, treatment)))+
  ggbeeswarm::geom_quasirandom(dodge.width = 1,
                               show.legend = T) +
  geom_boxplot(
    position=position_dodge(1),
    width=0.1, fill='black', show.legend = F)+
  scale_color_manual(values = target_colors) +
  scale_alpha_manual(values = c(.6, 1))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM