0

My data.frame() has 4 columns, the first one with the name of the Installation, the second one with the percentage of corrective work, the 3rd with preventive work and the last one with the percentage of other works.

library(ggplot2)

grafico <- ggplot(taula_final, aes(x = Instal.lació)) +
  geom_col(aes(y = Porcentajes_Correctius, fill = "Correctius"), width = 0.7, color = "black") +
  geom_col(aes(y = Porcentajes_Preventius, fill = "Preventius"), width = 0.7, color = "black") +
  geom_col(aes(y = Porcentajes_Altres, fill = "Altres"), width = 0.7, color = "black") +
  scale_fill_manual(values = c("Correctius" = "red", "Preventius" = "green", "Altres" = "orange"), drop = FALSE) +
  labs(x = "Instal.lació", y = "Porcentaje", fill = "Tipo de Trabajo") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

print(grafico)

Figure

I was expecting that it would fill all the bars to a 100% with the 3 colors, as you see they leave a blank space on top of each bar :(

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Apparently your percentages do not sum to 100%. You can either scale your data to 100%, or add the remainder as "unknown". Which strategy you take depends on what you want to show in your data. – mhovd Jul 12 '23 at 09:08
  • 1
    Welcome on SO, Tomas Oriol. Have you already checked whether one of these answers your question? https://stackoverflow.com/search?q=%5Br%5D+stacked+bar+chart+relative – I_O Jul 12 '23 at 09:13
  • 3
    The issue is that you are trying to create a stacked bar chart from three different columns which will not work. Instead you have to reshape your data so that the percentages are in one column and the categories or groups in another, e.g. `taula_final %>% pivot_longer(-Instal.lació, names_to = "category", values_to = "percentage") %>% ...`. After doing so you can create a stacked barchart with one geom_col. – stefan Jul 12 '23 at 10:27
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jul 12 '23 at 14:23

0 Answers0