Stacked bar chart across multiple columns

前端 未结 1 1167
遥遥无期
遥遥无期 2021-01-25 09:39

I managed to transform my initial dataset into this form:

   ElemId total_count coef_true coef_false coef_ratio
 1  a1           2         2          0                   


        
相关标签:
1条回答
  • 2021-01-25 10:17

    If I understand correctly, you want to plot coef values over ElemId where ElemId is reordered by coef_ratio. If this is the case, you could do the following:

    library(tidyverse)
    
    df %>% 
      gather(key, value, -c(coef_ratio, ElemId, total_count)) %>% 
      ggplot(aes(reorder(ElemId, coef_ratio), value)) +
      geom_col(aes(fill = key)) +
      labs(x = "ElemId reordered by coef_ratio")
    

    I modified the data so that ElemId a10 has a coef_ratio of 0 to show the reordering of the x axis.

    text <- "ElemId total_count coef_true coef_false coef_ratio
           1   a1           2         2          0          1
           2   a2           4         4          0          1
           3   a3           1         1          0          1
           4   a4           5         5          0          1
           5   a5           1         1          5          1
           6   a6           4         4          0          1
           7   a7           4         4          3          1
           8   a8           2         2          2          1
           9   a9           3         3          1          1
           10  a10          1         1          4          0"
    
    df <- read.table(text = text, header = TRUE, stringsAsFactors = FALSE)
    
    0 讨论(0)
提交回复
热议问题