0
我試圖繪製隨着時間的推移值時,與正在錯誤:設置x軸的最小和最大使用ggplot ploting在POSIXct時間
Error: Invalid input: date_trans works with objects of class Date only
我通過前面的問題曾與this post以及the post here。我想設置x軸的最小值和最大值,以刪除沒有數據的地塊極端的空白區域。
該代碼調用一個圖表,但沒有校正的x軸。
ggplot(dta, aes(x= WeekStarting, y = AvgWeekEle, group = IndID))+
geom_line(size = 1)+
theme(axis.text.x=element_text(angle=30, hjust=1))
我試着用scale_x_date
和下面的代碼來設置x軸的極限 - 這會產生錯誤。
ggplot(dta, aes(x= WeekStarting, y = AvgWeekEle, group = IndID))+
geom_line(size = 1)+
theme(axis.text.x=element_text(angle=30, hjust=1))+
scale_x_date(breaks = "1 week", labels=date_format("%b-%d-%Y"),
limits = as.Date(c("2014-01-6","2014-02-15")))
A dput()
我的數據如下。我正在使用最新版本的R並運行Windows 7.
在此先感謝。
dta <- structure(list(IndID = c("BHS_011_A", "BHS_011_A", "BHS_011_A",
"BHS_011_A", "BHS_011_A", "BHS_011_A", "BHS_011_A", "BHS_011_A",
"BHS_015_A", "BHS_015_A", "BHS_015_A", "BHS_015_A", "BHS_015_A",
"BHS_015_A", "BHS_015_A", "BHS_015_A", "BHS_018_A", "BHS_018_A",
"BHS_018_A", "BHS_018_A", "BHS_018_A", "BHS_018_A", "BHS_018_A",
"BHS_018_A"), WeekStarting = structure(c(1388905200, 1389510000,
1390114800, 1390719600, 1391324400, 1391929200, 1392534000, 1393138800,
1388905200, 1389510000, 1390114800, 1390719600, 1391324400, 1391929200,
1392534000, 1393138800, 1388905200, 1389510000, 1390114800, 1390719600,
1391324400, 1391929200, 1392534000, 1393138800), class = c("POSIXct",
"POSIXt"), tzone = ""), AvgWeekEle = c(1717.21428571429, 1770.07142857143,
1737.53571428571, 1673.32142857143, 1648.42857142857, 1760.71428571429,
1668.48148148148, 1654.5, 1643.5, 1794.85714285714, 1740.64285714286,
1664.73076923077, 1704.73076923077, 1685.2962962963, 1707.89285714286,
1661.46428571429, 1702.87096774194, 1691.17647058824, 1653.41176470588,
1650.30303030303, 1741.54545454545, 1652.33333333333, 1573.125,
1570.82352941176)), .Names = c("IndID", "WeekStarting", "AvgWeekEle"
), class = "data.frame", row.names = c(3362L, 3363L, 3364L, 3365L,
3366L, 3367L, 3368L, 3369L, 3470L, 3471L, 3472L, 3473L, 3474L,
3475L, 3476L, 3477L, 3535L, 3536L, 3537L, 3538L, 3539L, 3540L,
3541L, 3542L))
是的,我看到鏈接的帖子這個建議。但按照你的建議,我得到了錯誤'無效的輸入:time_trans只適用於類POSIXct的對象'由於這兩個建議導致了一個錯誤,我想我會從頭開始。對你的建議之後的第二個錯誤有任何想法? – 2015-02-05 22:14:40
更改爲「scale_x_datetime」適用於我。請注意,您還需要將'limits ='從'as.Date'更改爲'limits = as.POSIXct' – user20650 2015-02-05 22:25:25
尷尬和卑鄙......謝謝!很高興接受答案... – 2015-02-05 22:27:59