Order of dates is not chronological in ggplot2

前端 未结 2 1109
星月不相逢
星月不相逢 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: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)
    

提交回复
热议问题