如何在 R 中更改多個堆疊條形圖中的顏色?

Vojtech 彼得

我正在嘗試構建堆積條形圖。您可以在下面找到示例數據集和代碼。

這是數據集:

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")

這可以很好地生成繪圖,但我需要手動更改五個類別的顏色(在表示為“答案”的數據集中)。

在此處輸入圖像描述

但是,如果我添加:

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

我得到錯誤:連續值提供給離散比例。

斯蒂芬

您正在fillaes 上映射一個數字。因此,您將獲得一個連續的填充色標。如果您想通過在aesanswer上映射此列來填充您的條形圖。fill但由於此列也是數字,請將其轉換factorscale_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
))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 R 中分類變量的堆積條形圖中包含“總計”條形圖

如何在 R 中的條形圖中顯示每個條形圖的值(不使用 ggplot)?

Python:如何在 plotly 中更改多折線圖上的線條顏色?

如何為多個組繪製帶有註釋的堆疊條形圖

如何在堆疊條形圖 QuickChart 中從上到下顯示數據?

如何在堆積條形圖中的每個條形上添加值

使用 ggplot 在 R 中創建堆疊的“進度”條形圖

如何在角度條形圖中為動態添加的條形設置新顏色

如何使 geom_jitter() 在 R 中分組 ggplot2 條形圖中的各個條形圖中顯示點?

如何對堆疊的條形圖進行分組?(對幾個堆疊的條形圖使用相同的標籤)

R:如何在 ggplot 條形圖中的右軸上添加標籤?

如何根據列中的顏色為條形圖著色

python中的雙向條形圖,如何刪除所有背景顏色?

Plotly:如何始終在疊加條形圖中顯示最低的條形圖

如何基於 R 中的列數據使用兩個不同的數據幀組合條形圖?

在 python 中繪製條形圖時,如何在 3 列的 Pandas 數據框中將其堆疊為 2 列而不是為一列堆疊?

如何在堆疊條形圖上垂直集中 geom_text 輸出?

由於matplotlib,如何在同一圖中顯示具有重疊數據的多個圖形?

製作不同顏色的堆疊條形圖

將基本 R 堆積條形圖遷移到 ggplot2 並使用黑白結構而不是顏色區分條形級別

如何將兩個向量(我想要值的頻率)組合到 R 中的並排條形圖的數據框中

繪製多個圖形時如何修復 Matplotlib 中的透明度重疊?

如何在顫動中單擊切換按鈕時更改多個列表顏色

如何在D3中的刻度圖中間製作堆積條形圖的軸標籤?

在 R 中使用負值的 ggplot 堆疊條形圖

如何更改直方圖條的顏色?

如何在顫動中更改我的圖標按鈕的顏色?

如何在條形圖中添加單個標記

如何為echarts4r中的每個值設置固定顏色?