日期的顺序在ggplot2中不按时间顺序排列 [英] Order of dates is not chronological in ggplot2

查看:710
本文介绍了日期的顺序在ggplot2中不按时间顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制一张图来描绘一段时间内的人口。但是,日期不按时间顺序排列。在导入的CSV中,日期都是正确的并按顺序排列。但是,一旦下面的代码运行,呈现的图表就没有正确顺序的日期。起始日期在中间,结束日期在开始日期的左侧。有什么方法可以修复它?

  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,color = 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(color =black,size = 1)
,axis.text.x = element_text(color =black,size = 10)
,axis.text。 y = element_text(color =black,size = 8)
,axis.title.x = element_text(color =black,size = 10,face =bold,vjust = - 。 b $ b,axis.title.y = element_text(color =black,size = 10,face =bold,vjust = 1.2)
,legend.text = element_text(size = 8))

这形象是随着时间的推移。但是正如你所看到的那样,x轴上的日期是不正确的并且关闭: pngalt =这张图片是一段时间以来的人口数据,但是您可以看到x轴上的日期不正确并且关闭。>

解决方案

如果您的日期在数据框中以正确的顺序导入,请使用

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



。这将使它们成为基于它们出现顺序的有序因素,并且ggplot会明白它必须以这种方式保留它们。



编辑 :如果日期未订购,您可以订购它们并保存到一个向量中:

 日期<  -  unique(sort sumc $ Date))
sumc $ Date< - factor(sumc $ Date,labels = dates,ordered = T)


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:

解决方案

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)

这篇关于日期的顺序在ggplot2中不按时间顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆