Order of dates is not chronological in ggplot2

前端 未结 2 1108
星月不相逢
星月不相逢 2021-01-07 01:20

I\'m trying to make a graph to depict a population over a period of time. However, the dates are not in chronological order. In the imported CSV the dates are all correct an

相关标签:
2条回答
  • 2021-01-07 01:44

    Convert to a Date class:

    sumc$Date = as.Date(sumc$Date, format = "%m/%d%/Y")
    

    Then your same plotting code will work just fine.

    See ?as.Date or strptime for details about the conversion or the format argument.

    0 讨论(0)
  • 2021-01-07 01:48

    If your dates are imported in the correct order in the data frame, use

    sumc$Date <- factor(sumc$Date, ordered = T)

    prior to plotting. This will make them as ordered factors based on the order they appear, and ggplot will understand that it has to keep them that way.

    Edit: if the dates are not ordered, you can order them and save to a vector:

    dates <- unique(sort(sumc$Date))
    sumc$Date <- factor(sumc$Date, labels = dates,  ordered = T)
    
    0 讨论(0)
提交回复
热议问题