更改數據框中的日期格式我想繪製R中的n(y軸)與日期(x軸)的關係圖,但是由於數據中顯示的日期格式,日期的順序不是按照正確的升序排列。我該如何解決這個問題?感謝您的幫助。如何使用R
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
我嘗試使用作爲.Date函數,但它似乎不完整(我正在使用R版本3.2.2)
> pt[,1] <- as.Date(pt[,1], format='%d.%m.%Y’)
+
You wan t檢查你如何創建日期對象(例如,as.Date())。這裏是你的[link](https://stat.ethz.ch/R-manual/R-devel/library/base/html/as.Date.html)。你可能要考慮使用'scale_x_date()'。信息是[here](http://docs.ggplot2.org/current/scale_date.html)。 – jazzurro