Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

r - Order of dates is not chronological in ggplot2

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 and in order. However, once the code below is run, the graph presented does not have the dates in the correct order. The starting date is in the middle and the ending date is on the left of the starting date. Is there any way I can fix it?

sumc <- aggregate(ex2$C, bu=list(ex2$Date, ex2$Temp), FUN=sum)
colnames(sumc) <- c("Date", "Temperature", "Individuals")
ggplot(data= sumc, aes(x=Date, y=Individuals, group=Temperature, colour=Temperature )) + geom_line() + theme(plot.title = element_text(face="bold") 
    ,plot.background = element_blank()
    ,panel.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()
    ,axis.line = element_line(colour="black", size=1)
    ,axis.text.x = element_text(colour="black", size=10)
    ,axis.text.y = element_text(color="black", size=8)
    ,axis.title.x = element_text(colour="black", size=10, face="bold", vjust=-.2)
    ,axis.title.y = element_text(color="black", size=10, face="bold", vjust=1.2)
    ,legend.text=element_text(size=8))

This image is the population over time. But as you can see the dates on the x axis are incorrect and off:

This image is the population over time. But as you can see the dates on the x-axis are incorrect and off.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...