Search code examples
rggplot2ggplotly

how to transfer the following dateset to longformat and make the bar plot


I got the dataset as following

year<-seq(2000, 2010,2)
erate1<-runif(6,0,1)/6
erate2<-runif(6,0,1)/6
erate3<-runif(6,0,1)/6
erate4<-runif(6,0,1)/6
barplotdf<-data.frame(erate1,erate2,erate3,erate4,year)

The female position will be 2000, 2002, 2004....

The Friday position will be erate1,erate2,

enter image description here


Solution

  • On StackOverflow it's recommended to provide your own attempts to solve your problem. Looking at your previous questions however, I can see that you usually do that, and this question is clear, so here is an answer:

    library(tidyverse)
    year<-seq(2000, 2010,2)
    erate1<-runif(6,0,1)/6
    erate2<-runif(6,0,1)/6
    erate3<-runif(6,0,1)/6
    erate4<-runif(6,0,1)/6
    barplotdf<-data.frame(erate1,erate2,erate3,erate4,year)
    
    barplotdf %>% 
      pivot_longer(cols = -c(year)) %>% 
      mutate(sex = ifelse(year %in% c(2000, 2002, 2004), "Female", "Male"),
             day = case_when(name == "erate1" ~ "Fri",
                             name == "erate2" ~ "Sat",
                             name == "erate3" ~ "Sun",
                             name == "erate4" ~ "Thur")) %>% 
      ggplot(aes(x = day, y = value, fill = day)) +
      geom_col() +
      facet_wrap("sex") +
      scale_y_continuous(labels = scales::percent_format(),
                         name = "Percent") +
      scale_fill_discrete(label = c(1, 2, 3, 4))
    

    example_1.png

    EDIT

    To add the percentages, you need to 'format' the dataframe (pivot longer, add the "sex" variable based on year, and change "erate*" to "day") then create the labels in a second step:

    # Load libraries
    library(tidyverse)
    
    # Create the dataframe
    year<-seq(2000, 2010,2)
    erate1<-runif(6,0,1)/6
    erate2<-runif(6,0,1)/6
    erate3<-runif(6,0,1)/6
    erate4<-runif(6,0,1)/6
    barplotdf<-data.frame(erate1,erate2,erate3,erate4,year)
    
    # 'First step': format the dataframe to 'feed into' ggplot
    formatted_barplotdf <- barplotdf %>% 
      pivot_longer(cols = -c(year)) %>%
      mutate(sex = ifelse(year %in% c(2000, 2002, 2004), "Female", "Male"),
             day = case_when(name == "erate1" ~ "Fri",
                             name == "erate2" ~ "Sat",
                             name == "erate3" ~ "Sun",
                             name == "erate4" ~ "Thur"))
    
    # 'Second step': create a new variable called "perc"
    labels_df <- formatted_barplotdf %>% 
      group_by(sex, day) %>% 
      summarise(perc = round(sum(value), 4))
    
    # Plot the dataframe
    ggplot(formatted_barplotdf, aes(x = day, fill = day, y = value)) +
      geom_col() +
      facet_wrap("sex") +
      scale_y_continuous(labels = scales::percent_format(),
                         name = "Percent") +
      scale_fill_discrete(label = c(1, 2, 3, 4)) +
    # Add the labels from "labels_df"
      geom_text(data = labels_df, aes(y = perc,
                                     label = paste(perc * 100, "%", sep = ""),
                                     vjust = -0.5))
    

    example_2.png