3

I have a stacked bar chart and, inspired by Figure 3.8 on this page, I would like to highlight one bar of the bar chart. For example, I'd like all three colours for middle bar to be a notch darker and all the colours in the 1st and 3rd bars to be a little lighter. I'm assuming that the darken argument to scale_fill_OkabeIto could come in handy in some way.

library(ggplot)    
library(colorblindr)
ID <- rep(1:3, each = 3)
group <- rep(letters[1:3], times = 3)    
prop <- c(0.8, 0.1, 0.1, 0.6, 0.3, 0.1, 0.4, 0.3, 0.3)

toy_df <- data.frame(ID = ID, group = group, prop = prop)

ggplot(toy_df, aes(x = ID, y = prop, fill = group)) +
  geom_bar(stat = "identity") +
  scale_fill_OkabeIto()

Below is the graph so far:

I have seen posts showing how to do this for regular bar charts but can't figure out how to do this for a stacked bar chart.

Thanks for your help.

Cristian E. Nuno
  • 2,822
  • 2
  • 19
  • 33
EllaK
  • 209
  • 2
  • 9

2 Answers2

2

The easiest way to do something like this is to change the alpha based on whether the ID is the one you want highlighted. You could make a boolean variable in your dataframe, something like isHilite = ID == 2, or you can just do it inline when you plot.

I dropped your color scale just because I don't have that package installed, and it wasn't necessary to show you this example.

Another option if you want to get into more complex color functions is the munsell package that works with a whole different color system. I haven't used it enough to put together a good answer with it, but it has functions for darkening, lightening, and desaturating color.

library(ggplot2)    
ID <- rep(1:3, each = 3)
group <- rep(letters[1:3], times = 3)    
prop <- c(0.8, 0.1, 0.1, 0.6, 0.3, 0.1, 0.4, 0.3, 0.3)

toy_df <- data.frame(ID = ID, group = group, prop = prop)

ggplot(toy_df, aes(x = ID, y = prop, fill = group)) +
    geom_bar(aes(alpha = ID == 2), stat = "identity") +
    scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0.6), guide = F)

Created on 2018-04-23 by the reprex package (v0.2.0).

camille
  • 16,432
  • 18
  • 38
  • 60
1

Here is an easy way to do it, simply mapping alpha to whether the ID is a given value.

ggplot(toy_df, aes(x = ID, y = prop, fill = group, alpha=toy_df$ID != 2)) +
  geom_bar(stat = "identity") +
  scale_alpha_manual(values=c(1, 0.7)) +
  guides(alpha=F)

enter image description here

Of course, you can still use your colour-blind friendly colour set with this

Luke Hayden
  • 692
  • 4
  • 8
  • Having `toy_df$` inside an `aes` call could cause errors – camille Apr 23 '18 at 15:18
  • If so, add an extra line: `toy_df$isdark <- toy_df$ID != 2`, then map alpha to `toy_df$isdark` – Luke Hayden Apr 23 '18 at 15:40
  • That works, but you want to always do `aes` calls like `alpha = isdark` or `alpha = ID != 2`, without including the name of the dataframe – camille Apr 23 '18 at 15:43
  • it can cause scoping problems, for one. Here are a couple posts where that comes up: https://stackoverflow.com/questions/47860112/how-to-properly-use-variable-in-ggplot and https://stackoverflow.com/questions/29156155/misplaced-points-in-ggplot `ggplot` is expecting the columns you give it to be within the dataframe it's operating on, so it's better to just match the expected input – camille Apr 23 '18 at 16:31