0

I'd like to have the y-axis be scaled to percentage.x rather than percentage.y. However, when I do that the scale is incorrect.

"PID","severity","percentage.x","sideEffectScore","n","percentage.y"
189,"moderate",40,"1",25,69.44
189,"moderate",40,"2",5,13.89
189,"moderate",40,"3",5,13.89
189,"moderate",40,"4",1,2.78
189,"severe",26.67,"1",8,33.33
189,"severe",26.67,"2",9,37.5
189,"severe",26.67,"3",7,29.17
189,"transient",33.33,"1",12,40
189,"transient",33.33,"2",8,26.67
189,"transient",33.33,"3",8,26.67
189,"transient",33.33,"4",2,6.67
library(ggplot2)

df <- read.csv("testing.csv",
               header = T)

ggplot(testing, aes(severity, percentage.y)) +
        geom_bar(aes(fill = sideEffectScore), stat="identity") 

y-axis with percentage.y sums to 100%. enter image description here

However, percentage.x does not sum to 100% but it "should" and stacking is incorrect. enter image description here

  • What do you mean by "the y-axis be scaled to percentage.x". I would guess from your data that 40% of all cases are moderate, 26.67% are severe and 33.33% are transient, adding up to 100% – Henry Apr 21 '19 at 00:20
  • Thanks for your comment @Henry. Edited the post for clarification – Santi Allende Apr 21 '19 at 00:52
  • Can you include a sketch of what your ideal plot would look like? You may be looking for something along the lines of a [mosaic / marimekko plot](https://stackoverflow.com/q/19233365/8449629), if you want to reflect the relative proportion of each category along the x-axis? – Z.Lin Apr 21 '19 at 03:22
  • 1
    The values in `percentage.x` do _not_ sum up to 100% on each level of severity (which you have on the x axis). They are percent of 1 level of severity on all observations. You could try to depict that using the `width` aesthetic of `geom_bar()` like this: `ggplot(testing, aes(severity, percentage.y)) + geom_bar(aes(fill = as.factor(sideEffectScore), width=percentage.x/100), stat="identity") ` – Simon Apr 21 '19 at 10:55
  • Aha! Thank you @Simon – Santi Allende Apr 26 '19 at 20:16

0 Answers0