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

看板R_Language作者時間6年前 (2018/12/15 23:01), 6年前編輯推噓0(0010)
留言10則, 3人參與, 6年前最新討論串1/1
[問題類型]: 程式諮詢(我想用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 時間軸間隔 星期改回日期 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.115.217.19 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1544886070.A.0CF.html

12/15 23:14, 6年前 , 1F
12/15 23:14, 1F

12/16 16:57, 6年前 , 2F
先提供一小段原始資料,特別是日期被記錄的方式。
12/16 16:57, 2F

12/16 17:14, 6年前 , 3F
不太確定你使用的函數和程式碼不好給建議,但若重給axis
12/16 17:14, 3F

12/16 17:15, 6年前 , 4F
比如原本時間太長以至於時間軸時間被自動改掉 你可以自給
12/16 17:15, 4F

12/16 17:16, 6年前 , 5F
tx=seq.Date(ymd('2018-01-01'),ymd('2018-12-31'),by=
12/16 17:16, 5F

12/16 17:17, 6年前 , 6F
"month"); axis(1,at=tx,labels=tx) 類似這樣重新給label
12/16 17:17, 6F
已在原文附上附上程式碼 ※ 編輯: Retangle (140.115.217.19), 12/17/2018 10:18:37

12/17 11:33, 6年前 , 7F
強制 axis.POSIXct(1, x = x3, format = "%F")
12/17 11:33, 7F

12/17 11:34, 6年前 , 8F
記得plot()中加xaxt="n"使x-axis先不畫刻度
12/17 11:34, 8F

12/17 11:35, 6年前 , 9F
format的用法請看 ?strptime
12/17 11:35, 9F

12/17 11:37, 6年前 , 10F
如果刻度位置有正負偏,可注意時區。
12/17 11:37, 10F
文章代碼(AID): #1S5HSs3F (R_Language)
文章代碼(AID): #1S5HSs3F (R_Language)