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

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:

enter image description here

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?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.