为 ggplot2 堆叠条形图中的每个条创建不同的色标

Create a different color scale for each bar in a ggplot2 stacked bar graph

我有一个堆叠条形图,如下所示:

虽然颜色看起来不错,但有这么多相似的颜色代表不同的药物会让人感到困惑。我想为图表中的每个条形图设置一个单独的调色板,例如,class1 可以使用调色板 "Blues",而 class2 可以使用调色板 "BuGn"(找到调色板名称 here

我发现了一些情况,其中人们手动为每个条形编码颜色(例如 here),但我不确定我所问的是否可行 - 这些条形需要基于调色板,因为每种药物中有很多药物 class。

创建上图的代码:

library(ggplot2)
library(plyr)
library(RColorBrewer)

drug_name <- c("a", "a", "b", "b", "b", "c", "d", "e", "e", "e", "e", "e", "e",
           "f", "f", "g", "g", "g", "g", "h", "i", "j", "j", "j", "k", "k",
           "k", "k", "k", "k", "l", "l", "m", "m", "m", "n", "o")
df <- data.frame(drug_name)

#get the frequency of each drug name
df_count <- count(df, 'drug_name')

#add a column that specifies the drug class
df_count$drug_class <- vector(mode='character', length=nrow(df_count))

df_count$drug_class[df_count$drug_name %in% c("a", "c", "e", "f")] <- 'class1'

df_count$drug_class[df_count$drug_name %in% c("b", "o")] <- 'class2'

df_count$drug_class[df_count$drug_name %in% c("d", "h", "i")] <- 'class3'

df_count$drug_class[df_count$drug_name %in% c("g", "j", "k", "l", "m", "n")] <- 'class4'

#expand color palette (from http://novyden.blogspot.com/2013/09/how-to-expand-color-palette-with-ggplot.html)

colorCount = length(unique(df_count$drug_name))
getPalette = colorRampPalette(brewer.pal(9, "Set1"))

test_plot <- ggplot(data = df_count, aes(x=drug_class, y=freq, fill=drug_name) ) + geom_bar(stat="identity") + scale_fill_manual(values=getPalette(colorCount))

test_plot

这么多颜色,你的情节会很混乱。最好只用药物名称和计数标记每个条形部分。下面的代码显示了一种为每个条形图制作单独调色板的方法,以及如何标记条形图。

首先,添加我们将用于定位条形标签的列:

library(dplyr) # for the chaining (%>%) operator

## Add a column for positioning drug labels on graph
df_count = df_count %>% group_by(drug_class) %>%
  mutate(cum.freq = cumsum(freq) - 0.5*freq)

其次,创建调色板。下面的代码使用四种不同的 Colorbrewer 调色板,但您可以使用调色板创建函数或方法的任意组合来根据需要精细地控制颜色。

## Create separate palette for each drug class

# Count the number of colors we'll need for each bar
ncol = table(df_count$drug_class)

# Make the palettes
pal = mapply(function(x,y) brewer.pal(x,y), ncol, c("BrBG","OrRd","YlGn","Set2"))
pal[[2]] = pal[[2]][1:2]  # We only need 2 colors but brewer.pal creates 3 minimum
pal = unname(unlist(pal)) # Combine palettes into single vector of colors

ggplot(data = df_count, aes(x=drug_class, y=freq, fill=drug_name) ) + 
  geom_bar(stat="identity", colour="black", lwd=0.2) + 
  geom_text(aes(label=paste0(drug_name,": ", freq), y=cum.freq), colour="grey20") +
  scale_fill_manual(values=pal) +
  guides(fill=FALSE)

创建调色板的策略和函数有很多。这是另一种方法,使用 hcl 函数:

lum = seq(100, 50, length.out=4)    # Vary the luminance for each bar
shift = seq(20, 60, length.out=4)  # Shift the hues for each bar

pal2 = mapply(function(n, l, s) hcl(seq(0 + s, 360 + s, length.out=n+1)[1:n], 100, l), 
              ncol, lum, shift)
pal2 = unname(unlist(pal2))

上面的各种调色板 不会 始终如一地转移到不同的 classes - 而是根据命名的向量 (a,b,c.. .) 并因此被分成不同的 classes。有关详细信息,请参阅 ??scale_fill_manual

为了 "match" 它们到每组柱状图,我们需要按 class 排序 data.frame,并将调色板与名称适当对齐。

创建重复调色板以测试正确(预期)排序。

 repeating.pal = mapply(function(x,y) brewer.pal(x,y), ncol,        c("Set2","Set2","Set2","Set2"))

 repeating.pal[[2]] = repeating.pal[[2]][1:2]  # We only need 2 colors but brewer.pal creates 3 minimum

 repeating.pal = unname(unlist(repeating.pal))

根据 class 对数据进行排序(我们希望颜色保留的顺序!)

 df_count_sorted <- df_count[order(df_count$drug_class),]

复制药物名称的原始顺序。

 df_count_sorted$labOrder <- df_count$drug_name

添加测试调色板。

 df_count$colours<-repeating.pal

改变绘图例程,fill = labOrder.

ggplot(data = df_sorted, aes(x=drug_class, y=freq, fill=labOrder) ) + 
geom_bar(stat="identity", colour="black", lwd=0.2) + 
geom_text(aes(label=paste0(drug_name,": ", freq), y=cum.freq),     colour="grey20") +
scale_fill_manual(values=df_sorted$colours) +
guides(fill=FALSE)