[問題] plot時間關係圖的時間軸間距

作者: Retangle   2018-12-15 23:01:05
[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
入門(寫過其他程式,只是對語法不熟悉)
[問題敘述]:
我在畫一筆資料和時間的關係,但是資料的時間尺度是5天
所以橫座標的間隔被自動換成星期表示 (時間軸使用lubridate包產生)
想請教如何才能讓他用日期表示
已經試過 yaxt="n" 然後用 axis()重設
但時間軸會變成lubridate轉換時間的numeric 如1542528000、1542614400
[程式範例]:
library(readxl)
library(lubridate)
library(plotrix)
#data = read_excel("C:/Users/User/Desktop/observe.xlsx",na="NA") 原本的資料
data2=list(51,43,39,16,18,22,21,33,43,17,25,29,29,31,32) #其中一項資料
x2=seq(from=ymd_hm("2018-11-16-08-00"),to=ymd_hm("2018-11-20-20-00"),by=6*60*60)
x3=c(x2[1],x2[2],x2[3],x2[5],x2[6],x2[7],x2[9],x2[10],x2[11],x2[13],x2[14],x2[15],x2[17],x2[18],x2[19])
plot(x3,data2,type="b",xlab="時間:11/16~11/20每日0800、1400、2000",
ylab="pm10 (μg/m^3)",yaxs="i",yaxt="n",ylim=c(15,55),col="blue")
axis(2,at=seq(15,55,5))
[環境敘述]:
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Japanese_Japan.932 LC_CTYPE=Japanese_Japan.932
LC_MONETARY=Japanese_Japan.932 LC_NUMERIC=C
[5] LC_TIME=Japanese_Japan.932
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.7.4 readxl_1.1.0
loaded via a namespace (and not attached):
[1] compiler_3.5.1 magrittr_1.5 tools_3.5.1 pillar_1.3.0
tibble_1.4.2 crayon_1.3.4 Rcpp_1.0.0
[8] cellranger_1.1.0 stringi_1.1.7 stringr_1.3.1 rlang_0.2.2
[關鍵字]:
lubridate plot 時間軸間隔 星期改回日期
作者: Retangle   2018-12-15 23:14:00
作者: andrew43 (討厭有好心推文後刪文者)   2018-12-16 16:57:00
先提供一小段原始資料,特別是日期被記錄的方式。
作者: cywhale (cywhale)   2018-12-16 17:14:00
不太確定你使用的函數和程式碼不好給建議,但若重給axis比如原本時間太長以至於時間軸時間被自動改掉 你可以自給tx=seq.Date(ymd('2018-01-01'),ymd('2018-12-31'),by="month"); axis(1,at=tx,labels=tx) 類似這樣重新給label
作者: andrew43 (討厭有好心推文後刪文者)   2018-12-17 11:33:00
強制 axis.POSIXct(1, x = x3, format = "%F")記得plot()中加xaxt="n"使x-axis先不畫刻度format的用法請看 ?strptime如果刻度位置有正負偏,可注意時區。

Links booklink

Contact Us: admin [ a t ] ucptt.com