Search code examples
rggplot2alphanumeric

Why does the sorted dataframe rearrange when plotting?


Originally the dataframe, df, was sorted I'm assuming as a string but was able to sort the alphanumeric vector:

df <- df[mixedorder(as.character(df$ID)),]

When creating a barplot the (x-axis) order changes back to 1 10a 10b 11 even though I explicitly changed the order to 1 2 3 4 5


Solution

  • library(tidyverse)
    
    df <- data.frame(ID=(c("1", "2", "3", "4", "5", "10a", "10b", "11")), 
                     y=c(seq(100,500,100), 150, 155, 180), stringsAsFactors = FALSE)
    

    Simple Fix for Simple Data

    df$numId<-1:nrow(df)
    
    ggplot(df, aes(x=reorder(ID,numId), y = y)) +
      geom_col() +
      labs(x='ID', y='Value')
    

    Result

    enter image description here

    Create a function to make numberic value

    create_id<-function(x) {
      if(!grepl('[a-z]',x,ignore.case = TRUE)) {
        return(as.numeric(x))
      } else {
        letter<-tolower(gsub('[0-9]+',"",x))
        letter_value<-which(letters==letter)/100
        number<-as.numeric(gsub('[a-z]',"",x)) + letter_value
        return(number)
      }
    }
    
    df<-df %>%
      group_by(ID, y) %>%
      mutate(nid = round(create_id(ID),3))
    
    ggplot(df, aes(x=reorder(ID,nid), y = y)) +
      geom_col() +
      labs(x='ID', y='Value')
    

    Result

    enter image description here

    Thank you to @user12728748 for the answer as well as providing the dataframe code. My answer is only here to satisfy the ggplot2 tag in the question. The answer above is just as suitable.