gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:32:11 25 4
gpt4 key购买 nike

我有一个看起来像这样的堆积条形图:

Number of patients on each drug by drug class

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

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

创建上图的代码:

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)

enter image description here

有许多创建调色板的策略和功能。这是另一种方法,使用 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))

关于r - 为 ggplot2 堆积条形图中的每个条形创建不同的色标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35947046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com