如何在 R 中的多个堆叠条形图中更改颜色?

How to change colours in multiple stacked bar charts in R?

我正在尝试构建堆积条形图。您可以在下面找到示例数据集和代码。

这是数据集:

group;answer;count;proportion
first;1;67;19
first;2;119;33,7
first;3;6;1,7
first;4;116;32,9
first;5;45;12,7
second;1;102;17,1
second;2;197;33,1
second;3;10;1,7
second;4;232;38,9
second;5;55;9,2
third;1;49;12,9
third;2;143;37,7
third;3;1;0,3
third;4;142;37,5
third;5;44;11,6
fourth;1;45;14,9
fourth;2;93;30,7
fourth;3;3;1
fourth;4;118;38,9
fourth;5;44;14,5

这是代码:

p <- ggplot(sample1, aes(y = proportion, x = group, fill = proportion)) + 
geom_bar(position = "stack", stat = "identity") + 
facet_grid(~ "") + 
theme_minimal() +
 
p1 <-  ggpar(p, xlab = F, ylab = F, legend = "", ticks = F)

p1 + geom_text(aes(label = proportion), 
               position = position_stack(vjust = 0.5), 
               check_overlap = T, 
               colour = "white")

这很好地生成了绘图,但我需要手动更改五个类别的颜色(在表示为“answer”的数据集中)。

但是,如果我添加:

scale_fill_manual(values = c("#E7344E", "#0097BF", "#E7344E", "#0097BF", "#E7344E") )

我收到错误:向离散刻度提供连续值。

您正在 fill aes 上映射一个数字。因此你会得到一个连续的填充色标。如果您想通过 answer 填充您的条形图,请将此列映射到 fill aes。但由于此列也是数字,因此将其转换为 factor 以使 scale_fill_manual 有效:

library(ggplot2)

p <- ggplot(sample1, aes(y = proportion, x = group, fill = factor(answer))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_manual(values = c("#E7344E", "#0097BF", "#E7344E", "#0097BF", "#E7344E")) +
  facet_grid(~"") +
  theme_minimal()

p1 <- ggpubr::ggpar(p, xlab = F, ylab = F, legend = "", ticks = F)

p1 + geom_text(aes(label = proportion),
  position = position_stack(vjust = 0.5),
  check_overlap = T,
  colour = "white"
)

数据

sample1 <- structure(list(group = c(
  "first", "first", "first", "first",
  "first", "second", "second", "second", "second", "second", "third",
  "third", "third", "third", "third", "fourth", "fourth", "fourth",
  "fourth", "fourth"
), answer = c(
  1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
  4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L
), count = c(
  67L,
  119L, 6L, 116L, 45L, 102L, 197L, 10L, 232L, 55L, 49L, 143L, 1L,
  142L, 44L, 45L, 93L, 3L, 118L, 44L
), proportion = c(
  19, 33.7,
  1.7, 32.9, 12.7, 17.1, 33.1, 1.7, 38.9, 9.2, 12.9, 37.7, 0.3,
  37.5, 11.6, 14.9, 30.7, 1, 38.9, 14.5
)), class = "data.frame", row.names = c(
  NA,
  -20L
))