Search code examples
rggplot2geom-bar

Plot a horizontal bar chart with default values in R


I have a DF like this:

df_test <- data.frame (ID  = c(88888, 99999),
                   Cluster1 = c(0, 1),Cluster2 = c(0, 2),Cluster3 = c(1, 3)
                   )

     ID Cluster1 Cluster2 Cluster3
1 88888        0        0        1
2 99999        1        2        3

Now I want a horizontal bar graph with the clusters on the y axis. All bars should go from 0-3 (min - max), because this is the range of the clusters. As color I want to have three gradations, 0-1 red, 1-2 yellow and 2-3 green. The values from the DF should then be shown as an arrow or line on the overall bar. Is this somehow possible with ggplot2?


Solution

  • You can use the geom_col following example here: https://ggplot2.tidyverse.org/reference/geom_bar.html

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    

    First, make the data tidy:

    df <- df_test %>% pivot_longer(cols = 2:4,
                             names_to = "Cluster", 
                             values_to = "value")
    

    Keep the largest of each cluster for making a bar chart:

    df <- df %>% group_by(Cluster) %>% 
      filter(value == max(value)) %>% 
      ungroup() %>% 
    # identify color scheme:
      mutate(cols = case_when(value <=1 ~ "red",
                         value > 1 & value <= 2 ~ "yellow",
                         value > 2 ~ "green"))
    
    ggplot(df) + geom_col(aes(x = value, y=Cluster, fill = Cluster)) + 
      scale_colour_manual(
        values = df$cols,
        aesthetics = c("colour", "fill")
      )
    

    enter image description here