简体   繁体   English

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

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

This is my first time posting on here so please go easy on me. 这是我第一次在这里发帖,所以请放轻松我。 I've been googling this problem for days and have not been able to find a solution so sorry if this has been answered elsewhere. 我一直在谷歌上搜索这个问题已经好几天了,但是如果在其他地方已经回答了这个问题就很难过。

I am making several stacked bar graphs in ggplot and want the legend categories to be identical on all the graphs (ie each category has the same color on each graph) without having to manually set all the colors. 我在ggplot中制作了几个堆积条形图,并希望所有图形上的图例类别相同(即每个图形在每个图形上具有相同的颜色),而无需手动设置所有颜色。 The issue is that the categories are not identical between graphs so simply specifying a palette results in the categories being different colors. 问题是图表之间的类别不相同,因此简单地指定调色板会导致类别为不同的颜色。

I can't use the actual data I'm working with so I've created a similar data frame that mimics the problem. 我无法使用我正在使用的实际数据,因此我创建了一个模拟问题的类似数据框。

Here is the example df: 这是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
  ...      ...          ...         ...

Here is the code for the graphs: 这是图表的代码:

  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
}

And here are the resulting graphs: 以下是结果图:

堆积条形图

So for example, on the 2013 graph the brown-yellow = benzene and on the 2014 graph brown-yellow = butane. 因此,例如,在2013年图表上,棕黄色=苯,2014年图表为棕黄色=丁烷。 What I would like is for the legend to be identical on both graphs (ie the 2014 graph will show benzene in the legend, even though it was not measured in that year) and for each chemical to be the same color on each graph. 我想要的是两个图表上的图例都是相同的(即2014图表将显示图例中的苯,即使在那一年没有测量),并且每个图表上的每种化学品都是相同的颜色。 Like this: 像这样:

理想的堆积条形图

I know how to do this by hand with scale_file_manual, however I have about 30 chemicals so I would prefer not to set them manually. 我知道如何用scale_file_manual手动完成这个,但是我有大约30种化学物质,所以我不想手动设置它们。 Let me know if you have questions or need any additional information. 如果您有任何疑问或需要任何其他信息,请与我们联系。 Thanks in advance for any help! 在此先感谢您的帮助!

I would set up a table ahead of time linking the colors and the chemical names 我会提前建立一个表格来连接颜色和化学名称

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
)

在此输入图像描述

Here is the answer I got to work for my large data set. 这是我为大型数据集工作的答案。 I used yake84's answer above and added the colorRampPalette() function to be able to extract more colors from a palette. 我在上面使用yake84的答案并添加了colorRampPalette()函数,以便能够从调色板中提取更多颜色。 I also changed chem_colors into a named vector because as a tibble the colors were not being mapped to the chemicals in my dataframe. 我还将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.

相关问题 如何按 ggplot2/R 中填充类别之一的值对堆积条形图 x 类别进行排序 - How to order stacked bar plot x categories by value of one of the fill categories in ggplot2/R 根据ggplot2中的类别比例调整(堆叠)条形宽度 - Adjusting (Stacked) Bar Width according to Proportions of Categories in ggplot2 如何使用 ggplot2 创建三个变量(不是类别?)的堆积条形图? - How to use ggplot2 to create a stacked bar chart of three variables (not categories!)? 如何在 ggplot2 中的堆积条形图类别之间引入分区? - How can I introduce a partition between the categories of a stacked bar chart in ggplot2? 如何更改 ggplot2 中条形图的顺序(类别)? 翻转我的传说? - How can I change the order (categories) of my bar charts in ggplot2? and flip my legend? 根据类别绘制手动图例ggplot2 - Plotting manual legend ggplot2 based on categories 如何使用ggplot2中的多个变量更好地创建堆积条形图? - How to better create stacked bar graphs with multiple variables from ggplot2? 您如何在 ggplot 中按大小对堆叠的条形类别进行排序? - How do you order stacked bar categories by size in ggplot? 如何在ggplot2中获得堆叠geom_bar和geom_point的共同图例? - How to get a common legend for stacked geom_bar and geom_point in ggplot2? ggplot2:如何对水平条形图中的类别进行排序? - ggplot2: how to sort the categories in horizontal bar charts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM