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

epi*_*00b 5 r bar-chart ggplot2 color-palette

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

按药物分类的每种药物的患者数量

虽然颜色看起来不错,但是如此多的相似颜色代表不同的药物会令人困惑.我想为图中的每个条形图分别设置一个调色板,例如,class1可以使用调色板"Blues",而class2可以使用调色板"BuGn"(此处找到的调色板名称)

我发现了一些人们为每个栏手动编码颜色的例子(比如这里),但我不确定我问的是否可能 - 这些栏需要基于调色板,因为有这么多每个药物类别的药物.

用于创建上图的代码:

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
Run Code Online (Sandbox Code Playgroud)

eip*_*i10 6

如此多的颜色,你的情节将会令人困惑.用药物名称和计数标记每个条形部分可能更好.下面的代码显示了为每个条形图制作单独调色板的一种方法,以及如何标记条形图.

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

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)
Run Code Online (Sandbox Code Playgroud)

其次,创建调色板.下面的代码使用了四种不同的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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

创建调色板有许多策略和功能.这是另一种使用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))
Run Code Online (Sandbox Code Playgroud)


dan*_*T_W 4

上面的各种调色板并不一致地转移到不同的类 - 相反,它们根据命名向量(a,b,c ...)进行绘制,因此被分割到各个类中。??scale_fill_manual详情请参阅。

为了将它们“匹配”到每组条形,我们需要按类排序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))
Run Code Online (Sandbox Code Playgroud)

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

 df_count_sorted <- df_count[order(df_count$drug_class),]
Run Code Online (Sandbox Code Playgroud)

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

 df_count_sorted$labOrder <- df_count$drug_name
Run Code Online (Sandbox Code Playgroud)

添加测试调色板。

 df_count$colours<-repeating.pal
Run Code Online (Sandbox Code Playgroud)

使用 = labOrder 更改绘图例程fill

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)
Run Code Online (Sandbox Code Playgroud)

调色板遵循预期顺序