如何用两组因子变量填充ggplot?
How to use fill in ggplot with two sets of factor variables?
我正在绘制堆积条形图。例如:
library(data.table)
library(ggplot2)
dt <- data.table(category = c("A", "B", "C", "D", "A", "B", "C", "D"),
variable = c("X", "X", "X", "X", "Y", "Y", "Y", "Y"),
value = c(7, 5, 4, 2, 1, 1, 2, 1))
dt$variable <- factor(dt$variable, levels = c("Y", "X"))
p <- ggplot(dt, aes(x=category, y=value, fill=category)) +
geom_bar(stat="identity", colour = "black") +
coord_flip() +
scale_fill_brewer(palette="Set3") +
theme_bw() +
theme(
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin=unit(c(0.8,0.8,0.8,0.8),"cm"),
text = element_text(size=14)
)
p
我试图让变量为 "A" 的每个类别都变成不同的颜色,并且所有在 "B" 中有变量的条形图都变成黑色。有谁知道如何做到这一点?有没有办法对类别和变量都使用填充?
我试过:
dt$col <- ifelse(dt$variable == "Y", "black", dt$category)
dt$col <- factor(dt$col, levels = c("black","A", "B", "C", "D"))
p <- ggplot(dt, aes(x=category, y=value, fill=col)) +
geom_bar(stat="identity", colour = "black") +
coord_flip() +
scale_fill_brewer(palette="Set3") +
theme_bw() +
theme(
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin=unit(c(0.8,0.8,0.8,0.8),"cm"),
text = element_text(size=14)
)
p
但是,我似乎无法使用 scale_fill_brewer() 并将颜色设置为黑色。
我认为您第二次尝试的方向是正确的。最好按照您的方式创建一个新列 col
。
接下来的诀窍是制作自定义色标和颜色图,明确地将值 "black" 映射到颜色 "black"。然后可以将此自定义颜色图输入 scale_fill_manual
.
这看起来像您想要的结果吗?
library(data.table)
library(ggplot2)
library(RColorBrewer)
dt <- data.table(category = c("A", "B", "C", "D", "A", "B", "C", "D"),
variable = c("X", "X", "X", "X", "Y", "Y", "Y", "Y"),
value = c(7, 5, 4, 2, 1, 1, 2, 1))
dt$variable <- factor(dt$variable, levels = c("Y", "X"))
all_levels <- c(unique(dt$category), "black")
dt$col <- ifelse(dt$variable == "Y", "black", dt$category)
dt$col <- factor(dt$col, levels = all_levels)
color_scale <- c(
brewer.pal(length(all_levels) - 1, name = "Set3"),
"black"
)
color_map <- setNames(color_scale, all_levels)
ggplot(dt, aes(x=category, y=value, fill=col)) +
geom_bar(stat="identity", colour = "black") +
scale_fill_manual(values = color_map) +
theme_bw() +
theme(
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin=unit(c(0.8,0.8,0.8,0.8),"cm"),
text = element_text(size=14)
)
由 reprex package (v0.3.0)
于 2019-09-04 创建
我正在绘制堆积条形图。例如:
library(data.table)
library(ggplot2)
dt <- data.table(category = c("A", "B", "C", "D", "A", "B", "C", "D"),
variable = c("X", "X", "X", "X", "Y", "Y", "Y", "Y"),
value = c(7, 5, 4, 2, 1, 1, 2, 1))
dt$variable <- factor(dt$variable, levels = c("Y", "X"))
p <- ggplot(dt, aes(x=category, y=value, fill=category)) +
geom_bar(stat="identity", colour = "black") +
coord_flip() +
scale_fill_brewer(palette="Set3") +
theme_bw() +
theme(
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin=unit(c(0.8,0.8,0.8,0.8),"cm"),
text = element_text(size=14)
)
p
我试图让变量为 "A" 的每个类别都变成不同的颜色,并且所有在 "B" 中有变量的条形图都变成黑色。有谁知道如何做到这一点?有没有办法对类别和变量都使用填充?
我试过:
dt$col <- ifelse(dt$variable == "Y", "black", dt$category)
dt$col <- factor(dt$col, levels = c("black","A", "B", "C", "D"))
p <- ggplot(dt, aes(x=category, y=value, fill=col)) +
geom_bar(stat="identity", colour = "black") +
coord_flip() +
scale_fill_brewer(palette="Set3") +
theme_bw() +
theme(
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin=unit(c(0.8,0.8,0.8,0.8),"cm"),
text = element_text(size=14)
)
p
但是,我似乎无法使用 scale_fill_brewer() 并将颜色设置为黑色。
我认为您第二次尝试的方向是正确的。最好按照您的方式创建一个新列 col
。
接下来的诀窍是制作自定义色标和颜色图,明确地将值 "black" 映射到颜色 "black"。然后可以将此自定义颜色图输入 scale_fill_manual
.
这看起来像您想要的结果吗?
library(data.table)
library(ggplot2)
library(RColorBrewer)
dt <- data.table(category = c("A", "B", "C", "D", "A", "B", "C", "D"),
variable = c("X", "X", "X", "X", "Y", "Y", "Y", "Y"),
value = c(7, 5, 4, 2, 1, 1, 2, 1))
dt$variable <- factor(dt$variable, levels = c("Y", "X"))
all_levels <- c(unique(dt$category), "black")
dt$col <- ifelse(dt$variable == "Y", "black", dt$category)
dt$col <- factor(dt$col, levels = all_levels)
color_scale <- c(
brewer.pal(length(all_levels) - 1, name = "Set3"),
"black"
)
color_map <- setNames(color_scale, all_levels)
ggplot(dt, aes(x=category, y=value, fill=col)) +
geom_bar(stat="identity", colour = "black") +
scale_fill_manual(values = color_map) +
theme_bw() +
theme(
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin=unit(c(0.8,0.8,0.8,0.8),"cm"),
text = element_text(size=14)
)
由 reprex package (v0.3.0)
于 2019-09-04 创建