Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot: filling color based on condition

Tags:

r

colors

ggplot2

I want to plot two categorical variables (group, condition) and one numeric variable (value). In addition, I want to base the filling color on the significance of the values (significant bars should be grey, the rest white). With the following code, however, only some significant bars are colored in grey.

plot <-  ggplot(dat, aes(group, value))+ 
  geom_col(aes(fill = condition), position = position_dodge(0.8), width = .7, color= "black") + 
   scale_fill_manual(values = ifelse(dat$significance > .05, "white", "grey")) + 
  geom_linerange(aes(group = condition,  ymin = ci_lower, ymax= ci_upper), position = position_dodge(0.8)) +
  coord_flip(ylim =c(-.2,1)) 
plot

here is my data:

dat <-  structure(list(group = c("friends", "parent", "esm", "friends", "parent", "esm"), 
                       value = c(0.25, 0.44, 0.33, 0.47, 0.25, 0.32), 
                       significance = c(0.08, 0, 0, 0, 0.01, 0), 
                       condition = c("S1", "S1", "S1", "S2", "S2", "S2"), 
                       trait = c("E", "E", "E", "E", "E", "E"), 
                       ci_lower = c(0.52, 0.74, 0.53, 0.67, 0.44, 0.49), 
                       ci_upper = c(-0.03, 0.14, 0.14, 0.27, 0.06, 0.15)),
                  row.names = c(1L,2L, 3L, 16L, 17L, 18L), class = "data.frame")
like image 527
Anabel Avatar asked Jun 20 '26 21:06

Anabel


1 Answers

You can add an inline mutate to create a column to specify the color group based on significance. The key here is to use the group aesthetic so the bars can still be dodged and positioned correctly based on the condition variable.

dat %>%
  mutate(sig = significance < .05) %>%
  ggplot(aes(group, value, group = condition)) +
  geom_col(
    aes(fill = sig),
    position = position_dodge(0.8),
    color = "black",
    width = .7
  ) +
  scale_fill_manual(values = c("white", "grey")) +
  geom_linerange(aes(ymin = ci_lower, ymax = ci_upper),
                 position = position_dodge(0.8)) +
  coord_flip(ylim = c(-.2, 1))

Gives this plot:

enter image description here

However, I think you need another aesthetic to distinguish condition in addition to significance. Color is one option, but this is a nice place to use ggpattern which will be more obvious than the outline color and keep the B&W look.

Here's an example:

library(ggpattern)

dat %>%
  mutate(sig = significance > .05) %>%
  ggplot(aes(group, value, group = condition)) +
  geom_col_pattern(
    aes(fill = sig, pattern_angle = condition),
    position = position_dodge(0.8),
    pattern_fill = "black",
    pattern_spacing = 0.025,
    pattern = "stripe",
    width = .7,
    color = "black"
  ) +
  scale_pattern_angle_discrete(range = c(45, 135)) +
  scale_fill_manual(values = c("grey", "white")) +
  geom_linerange(aes(ymin = ci_lower, ymax = ci_upper),
                 position = position_dodge(0.8)) +
  coord_flip(ylim = c(-.2, 1))

Which gives this plot:

with ggpattern

Finally, it's worth noting that the color of a bar is not usually used to denote significance of a statistical metric; a much more common convention would be to use asterisk to indicate relevant p value thresholds (e.g. ** p < 0.01) or letters to indicate membership in a grouped analysis such as an ANOVA. These can be easily implemented using the ggpubr package. That would leave fill color free to indicate the grouping by condition.

like image 185
Dan Adams Avatar answered Jun 22 '26 11:06

Dan Adams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!