rggplot2visualize

powerful ways or suggestions to visualize data by ggplot2 in R


I want to use ggplot2 to visualize my data in R.The dataset where have Group is participating teams,the locat is where they competed ,and the reason why they fail(in %).I am trying to visualize Group ,locat and reason_type1 to reason_type4.The plot need to indicate every team's fail_day of reason group by locat. I tried simple plotting at excel first.

enter image description here

But I want to group_by every group,for example team1 color by locat and show 4 reason. Below is what my dataset looks like:

df <- data.frame(Group = c(rep(paste0('team',1:7), each = 3), 'team7'),
                 locat= c(rep(c("AL","MD","OK"), 7), "NY"),
                 Days =c(653 ,855 ,232 ,321 ,796 ,818 ,636 ,701 ,799 ,559 ,5 ,905 ,861 ,2 ,1425 ,1131 ,192 ,95 ,380 ,542 ,610, 799),
                 survial_days=c(651 ,852 ,230 ,266 ,392 ,767 ,579 ,491 ,734 ,503 ,1 ,846 ,405 ,2 ,1133 ,822 ,192 ,94 ,379 ,452 ,587, 701),
                 fail_days=c(2 ,3 ,2 ,55 ,404 ,51 ,57 ,210 ,65 ,56 ,4 ,59 ,456 ,0 ,292 ,309 ,0 ,1 ,1 ,90 ,23, 98),
                 fail_P=c(0.31 ,0.35 ,0.86 ,17.13 ,50.75 ,6.23 ,8.96 ,29.96 ,8.14 ,10.02 ,80.00 ,6.52 ,52.96 ,0.00 ,20.49 ,27.32 ,0.00 ,1.05 ,0.26 ,16.61 ,3.77, 12.26),
                 reason_type1=c(0 ,0 ,0 ,0 ,6.56 ,0 ,0 ,0 ,0 ,0 ,0 ,0.01 ,26.83 ,0 ,20 ,4.69 ,0 ,0 ,0 ,1.51 ,0, 2.3),
                 reason_type2=c(0 ,0 ,0 ,0 ,0 ,0 ,5.5 ,0 ,7.51 ,0 ,0 ,1.51 ,0 ,0 ,0 ,8.4 ,0 ,0 ,0 ,3.79 ,0, 0),
                 reason_type3=c(0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0.31 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0, 0.8),
                 reason_type4=c(0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0.77 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0, 0))

I would really appreciate some help here. How do I restructure my plot or any suggestion how to show my data more visualize? Any help is much appreciated.Thank you.


Solution

  • One way could be:

    library(tidyverse)
    
    df %>% 
      dplyr::select(Group, locat, starts_with("reason")) %>% 
      pivot_longer(starts_with("reason")) %>% 
      ggplot(aes(x = locat, y=value, fill=name))+
      geom_col(position = position_dodge())+
      facet_wrap(~ Group) + 
      labs(title="Fail Days by Reason Type and Location", x="Location", y="Fail Days") +
      theme_bw()
    

    enter image description here