繁体   English   中英

当数据类别不相同时,如何在多个堆积条形图上获取相同的图例类别ggplot2

[英]How to get same legend categories on multiple stacked bar graphs when data categories are not identical ggplot2

这是我第一次在这里发帖,所以请放轻松我。 我一直在谷歌上搜索这个问题已经好几天了,但是如果在其他地方已经回答了这个问题就很难过。

我在ggplot中制作了几个堆积条形图,并希望所有图形上的图例类别相同(即每个图形在每个图形上具有相同的颜色),而无需手动设置所有颜色。 问题是图表之间的类别不相同,因此简单地指定调色板会导致类别为不同的颜色。

我无法使用我正在使用的实际数据,因此我创建了一个模拟问题的类似数据框。

这是df的示例:

  Year    Trial    Concentration    Chemical
  2013      1           0.8         Benzene
  2013      1           1.5         Toluene
  2013      1           0.8         Hexane 
  2013      2           1.5         Toluene
  2013      2           0.8         Carboxylic Acid
  2013      2           1.5         Acetone
  2013      3           0.8         Ethanol
  2013      3           1.9         Carboxylic Acid
  2013      3           3.1         Acetone
  2014      1           1.8         Benzene
  2014      1           2.5         Toluene
  2014      1           0.6         Methanol 
  2014      2           1.3         Toluene
  2014      2           1.8         Carboxylic Acid
  2014      2           2.5         Butane
  2014      3           1.5         Ethanol
  2014      3           1.2         Carboxylic Acid
  2014      3           3.5         Acetone
  ...      ...          ...         ...

这是图表的代码:

  list <- split(df, df$Year)
  plot_list <- list()

for (i in 1:5) {
    df <- list[[i]]

    p <- ggplot(df, aes(x = Trial, y = Concentration, width=0.8)) +
         geom_bar(stat = "identity", aes(fill = Chemical))
    plot_list = p
}

以下是结果图:

堆积条形图

因此,例如,在2013年图表上,棕黄色=苯,2014年图表为棕黄色=丁烷。 我想要的是两个图表上的图例都是相同的(即2014图表将显示图例中的苯,即使在那一年没有测量),并且每个图表上的每种化学品都是相同的颜色。 像这样:

理想的堆积条形图

我知道如何用scale_file_manual手动完成这个,但是我有大约30种化学物质,所以我不想手动设置它们。 如果您有任何疑问或需要任何其他信息,请与我们联系。 在此先感谢您的帮助!

我会提前建立一个表格来连接颜色和化学名称

library(data.table)
library(tidyverse)
library(RColorBrewer)

df <-
  fread("
    Year    Trial    Concentration    Chemical
    2013      1           0.8         Benzene
    2013      1           1.5         Toluene
    2013      1           0.8         Hexane 
    2013      2           1.5         Toluene
    2013      2           0.8         Carboxylic_Acid
    2013      2           1.5         Acetone
    2013      3           0.8         Ethanol
    2013      3           1.9         Carboxylic_Acid
    2013      3           3.1         Acetone
    2014      1           1.8         Benzene
    2014      1           2.5         Toluene
    2014      1           0.6         Methanol 
    2014      2           1.3         Toluene
    2014      2           1.8         Carboxylic_Acid
    2014      2           2.5         Butane
    2014      3           1.5         Ethanol
    2014      3           1.2         Carboxylic_Acid
    2014      3           3.5         Acetone
  ")

chem_colors <-
  tibble(Chemical = factor(unique(df$Chemical))) %>% 
  mutate(color = brewer.pal(n = n(), name = "RdBu")[as.integer(Chemical)])

# you can use your loop here instead
plot_trials <- function(year) {
  ggplot(filter(df, Year == year), aes(x = Trial, y = Concentration, width=0.8)) +
    geom_bar(stat = "identity", aes(fill = Chemical)) +
    scale_fill_manual(values = chem_colors$color, labels = chem_colors$Chemical)
}


gridExtra::grid.arrange(
  plot_trials(2013),
  plot_trials(2014), 
  nrow = 1
)

在此输入图像描述

这是我为大型数据集工作的答案。 我在上面使用yake84的答案并添加了colorRampPalette()函数,以便能够从调色板中提取更多颜色。 我还将chem_colors更改为命名向量,因为作为一个元素,颜色没有映射到我的数据框中的化学物质。

getPalette = colorRampPalette(brewer.pal(9, "Set1")   #create a palette with more than 9 colors

chem_colors  <- 
   tibble(Chemical = factor(unique(df$Chemical))) %>%
   mutate(color = getPalette(30))
chem_colors <- setNames(chem_colors$color, as.character(chem_colors$Chemical) #create named vector

plot_trials <- function(year) {
 ggplot(filter(df, Year == year), aes(x = Trial, y = Concentration, width=0.8)) +
  geom_bar(stat = "identity", aes(fill = Chemical)) +
  scale_fill_manual(values = chem_colors)
}

暂无
暂无

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

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