1
votes

I have a combo grouped/stacked bar graph I created using ggplot2 with this data:

Type    Category    Count   Blank
CAT1    Total   36  Not Blank
CAT1    Researched  36  Not Blank
CAT1    Structured  36  Not Blank
CAT1    Ingested    36  Not Blank
CAT1    Researched  0   Blank
CAT1    Structured  0   Blank
CAT1    Ingested    0   Blank
CAT2    Total   36  Not Blank
CAT2    Researched  36  Not Blank
CAT2    Structured  36  Not Blank
CAT2    Ingested    36  Not Blank
CAT2    Researched  0   Blank
CAT2    Structured  0   Blank
CAT2    Ingested    0   Blank
CAT3    Total   72  Not Blank
CAT3    Researched  72  Not Blank
CAT3    Structured  72  Not Blank
CAT3    Ingested    72  Not Blank
CAT3    Researched  0   Blank
CAT3    Structured  0   Blank
CAT3    Ingested    0   Blank
CAT4    Total   36  Not Blank
CAT4    Researched  36  Not Blank
CAT4    Structured  36  Not Blank
CAT4    Ingested    36  Not Blank
CAT4    Researched  0   Blank
CAT4    Structured  0   Blank
CAT4    Ingested    0   Blank
CAT5    Total   108 Not Blank
CAT5    Researched  108 Not Blank
CAT5    Structured  108 Not Blank
CAT5    Ingested    108 Not Blank
CAT5    Researched  0   Blank
CAT5    Structured  0   Blank
CAT5    Ingested    0   Blank
CAT6    Total   18  Not Blank
CAT6    Researched  18  Not Blank
CAT6    Structured  18  Not Blank
CAT6    Ingested    18  Not Blank
CAT6    Researched  0   Blank
CAT6    Structured  0   Blank
CAT6    Ingested    0   Blank
CAT7    Total   36  Not Blank
CAT7    Researched  36  Not Blank
CAT7    Structured  36  Not Blank
CAT7    Ingested    36  Not Blank
CAT7    Researched  0   Blank
CAT7    Structured  0   Blank
CAT7    Ingested    0   Blank
CAT8    Total   210 Not Blank
CAT8    Researched  150 Not Blank
CAT8    Structured  150 Not Blank
CAT8    Ingested    150 Not Blank
CAT8    Researched  60  Blank
CAT8    Structured  60  Blank
CAT8    Ingested    60  Blank
CAT9    Total   60  Not Blank
CAT9    Researched  36  Not Blank
CAT9    Structured  36  Not Blank
CAT9    Ingested    36  Not Blank
CAT9    Researched  24  Blank
CAT9    Structured  24  Blank
CAT9    Ingested    24  Blank
CAT10   Total   120 Not Blank
CAT10   Researched  90  Not Blank
CAT10   Structured  90  Not Blank
CAT10   Ingested    90  Not Blank
CAT10   Researched  30  Blank
CAT10   Structured  30  Blank
CAT10   Ingested    30  Blank

And using this code I get this image which shows the grouped and stacked charts exactly how I want to see them.

library(ggplot2)
example <- read.delim("example.txt")
example$Category <- factor(example$Category, levels = c("Total", "Researched", "Structured", "Ingested"))
ggplot(data = example, aes(x=Category, y = Count, fill = Blank)) + geom_bar(stat = "identity") + facet_grid(~Type)

R grouped stacked bar chart

enter image description here

The problem with the above image is that the 4 individual bars in each facet are the same color and the only separation is where the stacking is concerned. How do I alter my ggplot2 line to give each of the 4 bars in the facets a different color, while still showing the separation between Blanks and Not Blanks as stacked?

I've tried changing the fill option to "Category" and that will give the desired color scheme but will not break out Blank vs Not Blank as a stacked chart. As always any help is appreciated.

1

1 Answers

1
votes

you can try something like this:

ggplot(data = example, aes(x = Category, y = Count, group = Category, fill = Category, alpha = Blank)) + 
geom_bar(stat = "identity",  color = "black") +
facet_grid(~ Type)

enter image description here

HTH Tony