Search code examples
rggplot2geom-bargeom-point

geom_bar and geom_point conditional colours


I have a simple geom_bar and geom_point and would like to colour bars and points based on values in data. My code is currently as follows:

```df <- (Test3)

ggplot(Test3,aes(ques,diff1,subj))+
  geom_bar(stat = "identity")+
  geom_point(aes(x=ques, y= diff2))+
  facet_grid(~subj)

My chart currently looks like this: chart

My data looks likes this:

```# A tibble: 8 x 9
  subj   ques    `%` `sect%`  diff1    ci last_y  diff2    tt
  <chr>  <chr> <dbl>   <dbl>  <dbl> <dbl>  <dbl>  <dbl> <dbl>
1 geog   q1     82.1    83.6  -1.49  10.1     78   4.14  2.1 
2 geog   q2     80      50    30     10.1     54  26    -1.28
3 hist   q1     92.9    83.6   9.21  10.1     NA  NA    NA   
4 hist   q2     85.7    82.8   2.94  10.1     NA  NA    NA   
5 Sports q1     73.8    82.4  -8.61  10.1     95 -21.2   2.97
6 Sports q2     69.2    81.8 -12.6   10.1     12  57.2  -2.59
7 sci    q1     78.6    85.2  -6.66  10.1     87  -8.43 -3   
8 sci    q2     78      50    28     10.1     55  23     2 

What I would like is for bars to be green if diff1 >= ci, red if diff1 <= ci, and grey if neither. And for the points to be green if tt >= 1.96 or red if tt <= 1.96, and grey if neither. Would be grateful for assistance.


Solution

  • This could be achieved like so:

    1. Add new variables with colors to your dataset according your conditions using e.g. dplyr::mutate and dplyr::case_when
    2. Map these new variables on color or fill.
    3. Make use of scale_color/fill_identity
    library(ggplot2)
    library(dplyr)
    
    Test3 <- Test3 %>%
      mutate(
        fill = case_when(
          diff1 >= ci ~ "green",
          diff1 < ci ~ "red",
          TRUE ~ "grey"
        ),
        color = case_when(
          tt >= 1.96 ~ "green",
          tt < 1.96 ~ "red",
          TRUE ~ "grey"
        )
      )
    
    ggplot(Test3, aes(ques, diff1, subj)) +
      geom_col(aes(fill = fill)) +
      geom_point(aes(x = ques, y = diff2, color = color)) +
      scale_color_identity() +
      scale_fill_identity() +
      facet_grid(~subj)
    #> Warning: Removed 2 rows containing missing values (geom_point).
    

    DATA

    Test3 <- structure(list(subj = c(
      "geog", "geog", "hist", "hist", "Sports",
      "Sports", "sci", "sci"
    ), ques = c(
      "q1", "q2", "q1", "q2", "q1",
      "q2", "q1", "q2"
    ), `%` = c(
      82.1, 80, 92.9, 85.7, 73.8, 69.2,
      78.6, 78
    ), `sect%` = c(
      83.6, 50, 83.6, 82.8, 82.4, 81.8, 85.2,
      50
    ), diff1 = c(-1.49, 30, 9.21, 2.94, -8.61, -12.6, -6.66, 28), ci = c(10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1), last_y = c(
      78L,
      54L, NA, NA, 95L, 12L, 87L, 55L
    ), diff2 = c(
      4.14, 26, NA, NA,
      -21.2, 57.2, -8.43, 23
    ), tt = c(
      2.1, -1.28, NA, NA, 2.97, -2.59,
      -3, 2
    )), class = "data.frame", row.names = c(
      "1", "2", "3", "4",
      "5", "6", "7", "8"
    ))