使用ggplot2更改堆叠柱状图的颜色,根据每个堆叠的数值标签。

huangapple go评论54阅读模式
英文:

Change the color of a stacked barchart in ggplot2 according to the value label of each stack

问题

以下是您要翻译的内容:

  1. 将所有值为“NA”的堆栈设置为灰色。
  2. 将“总计”列移到右端。

有人可以帮助我吗?

英文:

Consider the following dataset:

institucion <- data.frame( 
               inst = c(rep("Inst1", 6), rep("Inst2", 6), rep("Inst3", 6), rep("Inst4", 6), rep("Inst5", 6)), 
               ejes = rep(c("total", "eje 1", "eje 2", "eje 3", "eje 4", "eje 5"), 5), 
               ig = c(1, "NA", "NA", "NA", "NA", "Total: 1", 1, 2, "NA", 4, 5,  "Total: 12", 1 , 2, 3, "NA", 5,  "Total: 11",
                      1, "NA", "NA", 4, 5, "Total: 10", 1, "NA", 3, 4, 5, "Total: 13"),   
               peso = rep(100/6, 30) 
               )

With which the following ggplot chart is created:

plot6 <- ggplot(institucion, aes(x = inst, y = peso, fill = ejes, label = ig)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  geom_text(aes(label = ig),
            position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = c("white", "blue", "red", "darkgreen", "pink", "purple", "green", "gray")) +
  theme(legend.title = element_blank(),
        rect = element_blank())

The result is this:

使用ggplot2更改堆叠柱状图的颜色,根据每个堆叠的数值标签。

I want to do two thins:

  1. Set all stacks whose value is "NA" to gray.
  2. Move the "totals" column to the right end.

Someone who can help me?

答案1

得分: 1

将您的代码部分翻译如下:

# 使用以下选项来实现您期望的结果。首先,将您的 "ejes" 列转换为具有期望顺序的 "factor",以将总计列移动到右侧。其次,为了使 NA 值的填充颜色为灰色,我添加了一个新的填充列,如果 "ig" 列为 NA,则将其值设置为 NA,并且仍然通过 "ejes" 对堆叠条进行排序。此外,通过 "scale_fill_manual" 的 "breaks" 参数删除了图例中的 NA 值。最后,我将 ""NA"" 字符串转换为真正的 NA,并重新编码了 "ejes" 列,因为我认为 "ejes" 和 "ig" 之间存在不匹配。

```R
library(ggplot2)

institucion$ig[institucion$ig == "&quot;NA&quot;"] <- NA_character_
institucion$ejes <- dplyr::case_match(
  institucion$ejes,
  "total" ~ "eje 5",
  "eje 5" ~ "total",
  .default = institucion$ejes
)
institucion$ejes <- factor(institucion$ejes, levels = rev(c(paste("eje", 1:5), "total")))
institucion$fill <- institucion$ejes
institucion$fill[is.na(institucion$ig)] <- NA_character_

ggplot(institucion, aes(x = peso, y = inst, fill = fill, label = ig, group = ejes)) +
  geom_col() +
  geom_text(aes(label = ig),
    position = position_stack(vjust = 0.5)
  ) +
  scale_fill_manual(
    breaks = function(x) x[!is.na(x)],
    values = c("white", "blue", "red", "darkgreen", "pink", "purple", "green", "gray")
  ) +
  theme(
    legend.title = element_blank(),
    rect = element_blank()
  )

使用ggplot2更改堆叠柱状图的颜色,根据每个堆叠的数值标签。


[![enter image description here][1]][1]

<details>
<summary>英文:</summary>

One option to achieve your desired result would be to convert your `ejes` column to a `factor` with your desired order to move the totals column to the right. Second, to get a grey fill color for NAs I added a new fill column for which I set the value to NA if the `ig` column is NA and added the `group` to still order the stacked bar by `ejes`. Additionally I dropped the NA value from the legend via the `breaks` argument of `scale_fill_manual`. Finally, I converted the `&quot;NA&quot;` strings to proper NAs and recoded the `ejes` column as IMHO there was a mismatch between `ejes` and `ig`.

library(ggplot2)

institucion$ig[institucion$ig == "NA"] <- NA_character_
institucion$ejes <- dplyr::case_match(
institucion$ejes,
"total" ~ "eje 5",
"eje 5" ~ "total",
.default = institucion$ejes
)
institucion$ejes <- factor(institucion$ejes, levels = rev(c(paste("eje", 1:5), "total")))
institucion$fill <- institucion$ejes
institucion$fill[is.na(institucion$ig)] <- NA_character_

ggplot(institucion, aes(x = peso, y = inst, fill = fill, label = ig, group = ejes)) +
geom_col() +
geom_text(aes(label = ig),
position = position_stack(vjust = 0.5)
) +
scale_fill_manual(
breaks = function(x) x[!is.na(x)],
values = c("white", "blue", "red", "darkgreen", "pink", "purple", "green", "gray")
) +
theme(
legend.title = element_blank(),
rect = element_blank()
)


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/3lj7E.png

</details>



huangapple
  • 本文由 发表于 2023年6月9日 00:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定