![](/img/trans.png)
[英]How to order stacked bar plot x categories by value of one of the fill categories in ggplot2/R
[英]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.