2

I have a dataset where individual samples belong to a large group and a smaller subgroup. Each group has several subgroups, but each subgroup can only belong to one larger group. Likewise, each sample can only belong to one subgroup, and thus one larger group.

I want to make a True/False stacked bar plot with two color meanings:

  • Outline (color) is the larger group
  • Fill is the True/False data, but is two shades of the larger group outline color.

This is close to what I want, but instead of light and dark grey, I would like light and dark red for the red fruits, light and dark green for the green fruits, and light and dark blue for the blue fruits.

fruit <- data.frame(Sample=1:20, 
                Fruit=c(rep("Apple", 3), rep("Strawberry", 2), rep("Grape", 4), 
                      rep("Watermelon", 4), rep("Lime", 3), rep("Blueberry", 2), 
                      rep("Plum", 2)), 
                Color=c(rep("Red", 9), rep("Green", 7), 
                        rep("Blue", 4)), 
                Ripe=c(rep(c(T, F), 10)))

fruit$Fruit <- factor(fruit$Fruit, unique(fruit$Fruit))
fruit$Color <- factor(fruit$Color, unique(fruit$Color))

ggplot(fruit, aes(Fruit)) +
    theme_bw() +
    geom_bar(stat="count", position="fill",
             aes(fill=Ripe, color=Color)) +
    scale_fill_manual(values=c("grey65", "grey85")) +
    scale_y_continuous(labels=scales::percent)

example

Is this possible? Or is there a better way I can visually distinguish the larger group membership with the true/false values? Thanks

hmg
  • 412
  • 1
  • 4
  • 18

1 Answers1

7

Edit: this is probably the more correct way to do it, using interaction to assign unique colors for each factor pair:

ggplot(fruit, aes(Fruit)) +
  geom_bar( aes(fill=interaction(Color,Ripe), color=Color), stat="count", position="fill")+ 
  scale_y_continuous(labels=scales::percent)+
  scale_fill_manual(values=c("pink","lightgreen","lightblue","red", "green","blue"))+
  theme_bw()

enter image description here

Map Color to fill, and Ripe to alpha:

ggplot(fruit, aes(Fruit)) +
  geom_bar(stat="count", position="fill",
             aes(fill=Color, color=Color,alpha=Ripe)) + 
  scale_y_continuous(labels=scales::percent)+
  scale_alpha_discrete(range=c(.5,1))+
  theme_bw()

enter image description here

Mako212
  • 6,787
  • 1
  • 18
  • 37
  • Thanks! I prefer the look of using alpha. – hmg Oct 05 '17 at 14:26
  • In the second example, what if I want the y axis to be "counts" of fruits and not percentage? Also, the Ripe as "counts"... – guidebortoli Jan 27 '18 at 12:11
  • 1
    @guidebortoli I would use `dplyr` to aggregate first: `fruit2 <- fruit %>% group_by(Fruit, Ripe) %>% summarize(count = n(), Color = first(Color))` then `ggplot(fruit2, aes(Fruit,count))+ geom_bar(stat="identity",aes(fill=interaction(Color,Ripe), color=Color))+scale_fill_manual(values=c("pink","lightgreen","lightblue","red", "green","blue"))+ theme_bw()` – Mako212 Jan 27 '18 at 23:18
  • Works perfectly!! I'm wonder if you know how to put the total n of observation for TRUE in each color bar. In this case would be two n observation for each bar, with one above the bar as the total n of each Color and above the TRUE bar the TRUE n observation for that particular Color... – guidebortoli Jan 28 '18 at 15:35
  • @guidebortoli ask a new question if you can't find an answer please. That way other people can find it too. – Mako212 Jan 29 '18 at 19:55