0

I'm trying to make a stacked bar chart, but the bars overlap.

This is the code that I am using:

library(ggplot2)   
 
ggplot(data = opstaR, aes(x = Indicator, y = Percentage, fill = factor(tip))) +
    geom_bar(position = "dodge", stat = "identity") + 
    coord_flip() +
    scale_fill_grey(labels = c("Case load change", "Compliance change")) +
    theme_bw() +
    scale_y_continuous(labels = scales::percent, limits = c(-0.15, 0.05),breaks = seq(-0.15, 0.05, 0.05)) +
    theme(axis.title.y = element_blank()) +
    theme(legend.title = element_blank()) +
    theme(legend.position = "bottom")

This is the output:

bar plot

How can I make the bars not stack to each other?

And how to invert the black/gray pattern?

Data

opstaR <- structure(list(Indicator = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L), .Label = c("HbA1c measurment among diabetic patients", 
"LDL-cholesterol measurement among diabetic patients", "Diet advice among diabetic patients", 
"Blood preasure measurment among diabetic patients", "Foot examination among diabetic patients", 
"Blood preasure among hypertensive patients", "Total cholesterol among hypertensive patients", 
"Blood glucose among hypertensive patients", "Lifestyle advice among hypertensive patients", 
"ECG among hypertensive patients", "Lifestyle advice among patients with MI", 
"Blood preasure measurment among patients with MI", "Total cholesterol among patients with MI", 
"LDL-cholesterol measurement among patients with MI", "HDL-cholesterol measurement among patients with MI", 
"Triglycerides measurement among patients with MI", "Blood glucose among patients with MI", 
"ECG among patients with MI"), class = c("ordered", "factor")), 
    Percentage = c(-0.119461152, -0.119461152, -0.119461152, 
    -0.119461152, -0.119461152, -0.125256348, -0.125256348, -0.125256348, 
    -0.125256348, -0.125256348, -0.139557636, -0.139557636, -0.139557636, 
    -0.139557636, -0.139557636, -0.139557636, -0.139557636, -0.139557636, 
    -0.016017788, -0.01085703, 0.030175594, -0.02377841, -0.011164678, 
    -0.020884472, -0.014592098, -0.013684149, 0.033509796, -0.009671902, 
    0.037509542, -0.018565706, -0.01177768, -0.007261598, -0.005188794, 
    -0.008428487, -0.010241197, 0.002690945), tip = c("Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_case_load", 
    "Change_in_case_load", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load", "Change_in_compliance_rate", 
    "Change_in_case_load", "Change_in_compliance_rate", "Change_in_case_load", 
    "Change_in_compliance_rate", "Change_in_case_load")), row.names = c(NA, 
-36L), class = "data.frame")

Andrea M
  • 2,314
  • 1
  • 9
  • 27
  • Andrea, thank you. How did you put data. I could not find that option. – aleksandar_m May 04 '22 at 08:50
  • If you run `dput(opstaR)` you will get the output starting with `structure(list(c(`. It's helpful because it's quicker to load into R compared to downloading a text file and importing it. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info. – Andrea M May 04 '22 at 10:20

1 Answers1

0

Just a small tweak, remove the position = "dodge". For the colors, one option is to assign it yourself.

opstaR %>% 
  ggplot(aes(x = Indicator, y = Percentage, fill = factor(tip))) +
  geom_bar(stat = "identity") +
  coord_flip() +
  scale_fill_manual(labels = c("Case load change", "Compliance change"),values = c("grey","black")) +  theme_bw() +
  scale_y_continuous(labels = scales::percent, limits = c(-0.15, 0.05),breaks = seq(-0.15, 0.05, 0.05)) +
  theme(axis.title.y = element_blank(),
        legend.title = element_blank(),
        legend.position = "bottom")

enter image description here

Anurag N. Sharma
  • 362
  • 2
  • 10
  • Anurag, thanks. However, I prefer that each bar stats from 0, depening % value. Those bars represent population change and event change. For instance is it not clear what the second pair represents. So, it is better to see every bar in the pair with one above another, as it can be seen in the excel. – aleksandar_m May 04 '22 at 11:26
  • Can you be a little more clear in how you want the bar chart to look? – Anurag N. Sharma May 04 '22 at 11:28
  • Yes. Thanks. https://media.springernature.com/lw685/springer-static/image/art%3A10.1186%2Fs13584-022-00516-x/MediaObjects/13584_2022_516_Fig2_HTML.png I hope you can see this picture. Interestingly, I made plots as I wanted but less number of pars. Perhaps, the reason is the large number of groups. – aleksandar_m May 04 '22 at 11:40
  • By the way have you noticed that some of your indicator data have two percentages for the same "tip" value? Try ```library(tidyverse) opstaR %>% dplyr::filter(str_detect(Indicator,"ECG among patients with MI"))``` – Anurag N. Sharma May 04 '22 at 12:14
  • It is going to impossible to reproduce the kind of graph you have linked if all the values in "Indicator" do not have two values, one each for - Change_in_case_load and Change_in_compliance_rate – Anurag N. Sharma May 04 '22 at 12:21
  • Thanks for helping me. I made some plot using this code and similar dataset. https://ibb.co/jRRZQCg – aleksandar_m May 04 '22 at 13:19