0

I want to combine stacked bar plots that add up to 1 (proportion), Column values indicate parts of proportion, Estimate is the corresponding proportion values of A,B,C that adds to 1, Treatments are the categories I want to maintain. Within each treatment, there should be two stacked bar plots (as shown in Column). How should I go about doing this in ggplot? Thanks! My data is as follow:

Column  Estimate    Treatment
  A.1     0.7           1
  B.1     0.2           1
  C.1     0.1           1
  A.2     0.5           1
  B.2     0.4           1
  C.2     0.1           1
  A.1     0.9           2
  B.1     0.1           2
  C.1     0             2
  A.2     0.2           2
  B.2     0.2           2
  C.2     0.6           2

enter image description here

So that A + B + C = 1, and the legend will show A,B,C. Plot should end up similar to this but instead of one bar for one treatment, there should be two (e.g. A.1 + B.1 + C.1 and A.2.+ B.2 + C.2), so four in total in two distinct groups. Perhaps I can facet wrap this?

Thanks!

Marvin.J
  • 1
  • 2
  • It's not clear from your data how to decide which estimate goes in which bar - presumably we will split `Column` into two columns, one with the letter, one with the number, and you could, say, put the number on the x-axis and facet by letter - but maybe you want the reverse? It's also very hard to answer without tangible sample data - would you mind filling in the `*`s with numbers so that the data can be imported and a solution can be demonstrated using it? – Gregor Thomas Jun 05 '20 at 13:36
  • Thanks for the comments, I have made some edits to my question. Hopefully, they are clearer now. – Marvin.J Jun 05 '20 at 14:26

1 Answers1

0
library(dplyr)
library(ggplot2)
library(tidyr)

df %>%
  separate(Column, into = c("letter", "number")) %>%
  ggplot(aes(x = number, y = Estimate, fill = letter)) +
  geom_col(position = "stack") +
  facet_wrap(~Treatment)

enter image description here


Using this sample data:

df = read.table(text = 'Column  Estimate    Treatment
  A.1     0.7           1
  B.1     0.2           1
  C.1     0.1           1
  A.2     0.5           1
  B.2     0.4           1
  C.2     0.1           1
  A.1     0.9           2
  B.1     0.1           2
  C.1     0             2
  A.2     0.2           2
  B.2     0.2           2
  C.2     0.6           2', header = TRUE)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294