Search code examples
rggplot2dplyrrstudiolevels

Factor function in R returning NAs


I have a data frame with a column of 'months' and coordinating values. When I create a graph, the months are ordered alphabetically. I want to order the months using the factor function, but now my graph is only showing the month of May and 'NAs'.

xnames<-c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Data$Month<-factor(Data$Month, levels = xnames)
ggplot(DAtaTidy_MergeRWPeaks2, (aes(x=factor(Month, xnames), y=Volume)), na.rm=TRUE) + 
    geom_bar() 

I tried embedding the factor in the ggplot function but it produced the same result. When I delete 'May' from 'xnames', the graph just shows NAs.

enter image description here


Solution

  • We can't see your data, but the behavior is indicative of Data$Month containing a value that is not included in your level term xnames. Is anything misspelled? I would suggest you compare levels(as.factor(Data$Month)) and xnames - it will certainly show you the issue.

    Example dataset that shows the same problem you have:

    yums <- c('soup', 'salad', 'bread')
    nums <- c(10, 14, 5)
    df1 <- data.frame(yums, nums)
    
    yum.levels <- c('soup', 'salad', 'bread', 'pasta')
    ggplot(df1, aes(x=factor(yums, yum.levels), y=nums)) + geom_col()
    

    That gives you this:

    enter image description here

    ...but if we mispell one of them (like capitalizing "Soup" in yums), you get this:

    yums1 <- c('Soup', 'salad', 'bread')
    nums <- c(10, 14, 5)
    df2 <- data.frame(yums1, nums)
    
    yum.levels <- c('soup', 'salad', 'bread', 'pasta')
    ggplot(df2, aes(x=factor(yums1, yum.levels), y=nums)) + geom_col()
    

    enter image description here