【发布时间】:2015-10-14 04:24:42
【问题描述】:
我想在 R 中绘制 n(y 轴)与日期(x 轴)的图表,但由于我的数据中显示的日期格式,日期的顺序不正确升序。我该如何解决这个问题?感谢您的帮助。
hybrid <- readWorksheetFromFile(excel.file, sheet="ResultSet", header=TRUE)
wb <- loadWorkbook(excel.file)
setMissingValue(wb,value=c("NA"))
hybrid1 <- readWorksheet(wb, sheet="ResultSet", header=TRUE)
我使用了 dplyr 函数。假设每个 Pub.Number 都有一个唯一的代码,我将其替换为一个。然后,我统计了某个日期的数量。
hybrid <- mutate(hybrid1, n=sum(Publication.Number=1))
p1 <- select(hybrid1, Publication.Date, n)
pt <- count(p1, Publication.Date, wt=n)
输出如下所示:
pt
Source: local data frame [627 x 2]
Publication.Date n
(chr) (dbl)
1 01.01.2013 1
2 01.01.2014 8
3 01.01.2015 10
4 01.02.2012 3
5 01.03.2012 16
6 01.04.2015 2
7 01.05.2012 1
8 01.05.2013 7
9 01.05.2014 23
10 01.06.2011 1
.. ... ...
然后,我绘制了它,但 R 将 Pub.Date 识别为字符
qplot(x=Publication.Date, y=n, data=pt, geom="point")
x <- hybrid1[,2]
class(x)
[1] "character"
The graph I've plotted is a mess because of the wrong order of the date
我尝试使用 as.Date 函数,但它似乎不完整(我使用的是 R 版本 3.2.2)
> pt[,1] <- as.Date(pt[,1], format='%d.%m.%Y’)
+
【问题讨论】:
标签: r date graph ggplot2 format