Search code examples
rggplot2geom-bar

geom_bar, geom_point conditional colours based on column value


I asked a related question earlier and thanks to help I received I am almost there but struggling at the last hurdle...

I would like to assign conditional colours to bars and points based on values in columns (Green if diff_1 >= ci Red if diff_1 <= ci). However, my code so far is only showing bars where this true. There should be some bars where there is a value but this value is not greater or less than CI. Ideally these would be grey. The point colours seem to be working fine.

As you can see from the chart, it is almost there but there are missing bars where a colour has not been assigned.

chart

Would be grateful for help, thank you.

Adam

my code looks like this:

Ex$diff_ci1 <- factor(Ex$diff_ci1, levels = c("Red", "Green"))
Ex$diff_ci2 <- factor(Ex$diff_ci2, levels = c("Red", "Green"))

    Ex %>% 
      ggplot(aes(x = diff_1, fill = diff_ci1, y = ques)) +
      geom_bar(stat = 'identity', alpha = .6, width = .2)+
      scale_fill_manual(values=c("red", "green"))+
      geom_point(aes(x = diff_sect, colour = diff_ci2, y = ques, size = 3))+
      geom_vline(xintercept = 0)+
      theme_bw()+
      theme(legend.position = "none")+
      facet_grid(~cah, labeller=label_wrap_gen(width=10))```



Here is my data:

    ```structure(list(school = c("School1", "School1", "School1", "School1", 
    "School1", "School1", "School1", "School1", "School1"), cah = c("bio", 
    "bio", "bio", "geo", "geo", "geo", "eng", "eng", "eng"), ques = c("q1", 
    "q2", "q3", "q1", "q2", "q3", "q1", "q2", "q3"), diff_1 = c(8.68, 
    16.07, -4.68, 6.05, 1.72, -10.74, 13.22, 13.8, 15.77), ci = c(14.8872687, 
    14.8872687, 14.8872687, 4.715027198, 4.715027198, 4.715027198, 
    7.559941135, 7.559941135, 7.559941135), diff_ci1 = structure(c(NA, 
    2L, NA, 2L, NA, 1L, 2L, 2L, 2L), .Label = c("Red", "Green"), class = "factor"), 
        diff_sect = c(5.95, 0.79, -14.76, 2.04, -15.55, -20.87, 4.34, 
        -3.93, 5.99), diff_ci2 = structure(c(NA, NA, NA, NA, 1L, 
        1L, NA, NA, NA), .Label = c("Red", "Green"), class = "factor")), row.names = c(NA, 
    -9L), spec = structure(list(cols = list(school = structure(list(), class = c("collector_character", 
    "collector")), cah = structure(list(), class = c("collector_character", 
    "collector")), ques = structure(list(), class = c("collector_character", 
    "collector")), diff_1 = structure(list(), class = c("collector_double", 
    "collector")), ci = structure(list(), class = c("collector_double", 
    "collector")), diff_ci1 = structure(list(), class = c("collector_character", 
    "collector")), diff_sect = structure(list(), class = c("collector_double", 
    "collector")), diff_ci2 = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df", 
    "tbl_df", "tbl", "data.frame"))```

 

Solution

  • If you comment out the scale_fill_manual line, you do get the grey bars.

    output