Search code examples
rggplot2as.datedataformat

I need to convert a row of characters (month names) to dates


This feels so simple but I just can't get it to work. I have a very simple excel that I imported with 3 columns- one is called MONTH, the other two are a sum and an average of counts that were made in that month. I am trying to make a line graph but the month names are alphabetizing instead of ordering chronologically. How can I format the month names to be recognized as dates? All of the other answers confuse me because they show making a data frame or a c() string and I just don't know what that means. My data is already imported and read.

This is the code I'm using for my line graph.

Library(ggplot2)

ggplot(data=Book2, aes(x=MONTH, y=AMROSUM, group=1))+geom_line()

I'm sorry if this isn't clear. I'm really inexperienced.


Solution

  • Just convert your variable MONTH into a factor and define the levels of the factor, then ggplot knows the right order.

    Book2$MONTH <- factor(Book2$MONTH, levels = month.name)
    

    If your month names are in a different format, just enter a vector with your month names in the levels argument