簡體   English   中英

R:在 ggplot 時間序列圖中的 x 軸(日期)中添加中斷

[英]R: Add breaks in x-axis (date) in ggplot timeseries plot

對於每一天(兩周: 2015-01-01 - 2015-01-15 ),我有 24 個(每小時值)使用 R ggplot2 包進行繪圖。 日期列dateChrcharacter format )如下所示:

> str(Data$dateChr)
chr [1:360] "1/1/2015 2:00" "1/1/2015 3:00" "1/1/2015 4:00" "1/1/2015 5:00" 
"1/1/2015 6:00" "1/1/2015 7:00" ...

這是代碼,我正在使用:

ggplot() + 
geom_line(data = Data, aes(x = dateChr, y = val1, group=1), color = "red") +
geom_line(data = Data, aes(x = dateChr, y = val2, group=1), color = "blue") +
theme_bw() +
xlab("Date") + 
ylab("Value")

情節是這樣的:

陰謀

x-axis看起來很糟糕。 我想在x-axis添加中斷,以便它只顯示 4 天中斷的日期(沒有小時或時間戳),即 2015-01-01、2015-01-04、2015-01-08 等等。 有人可以建議我如何添加這樣的休息時間嗎?

通過使用字符類型變量dateChr ,OP選擇了離散的 X軸。

scale_x_discrete()函數可用於自定義離散軸的外觀。 根據help("discrete_scale") ,它采用一個break參數來控制中斷(和標簽)。 可能要break一種輸入類型是

一個函數,當使用單個參數調用該函數時,它給出了比例尺的極限,它返回一個字符矢量,該字符矢量指定要顯示的中斷。

因此,額外調用scale_x_discrete()

library(ggplot2)
ggplot() + 
  geom_line(data = Data, aes(x = dateChr, y = val1, group=1), color = "red") +
  geom_line(data = Data, aes(x = dateChr, y = val2, group=1), color = "blue") +
  theme_bw() +
  xlab("Date") + 
  ylab("Value") + 
  scale_x_discrete(breaks = function(x) x[seq(1, length(x), by = 4*24)])

我們得到

在此處輸入圖片說明

每隔4天顯示一次休息和標簽。


現在,OP已要求僅顯示日期(無小時或時間戳) 這需要操縱dateChr但僅用於繪制標簽:

# define named functions for breaks
my_breaks <- function(x) x[seq(1, length(x), by = 4*24)]
library(ggplot2)
ggplot() + 
  geom_line(data = Data, aes(x = dateChr, y = val1, group=1), color = "red") +
  geom_line(data = Data, aes(x = dateChr, y = val2, group=1), color = "blue") +
  theme_bw() +
  xlab("Date") + 
  ylab("Value") + 
  scale_x_discrete(breaks = my_breaks,
                   labels = my_breaks(stringr::str_sub(Data$dateChr, 1, 10)))

在此處輸入圖片說明

數據

不幸的是,OP沒有提供數據來重現圖形。 因此,我必須組成自己的樣本數據集來模擬OP的數據。

df1 <- data.table::fread("https://tidesandcurrents.noaa.gov/api/datagetter?product=wind&application=NOS.COOPS.TAC.MET&begin_date=20150101&end_date=20150114&station=8594900&time_zone=GMT&units=metric&interval=h&format=csv")
df2 <- data.table::fread("https://tidesandcurrents.noaa.gov/api/datagetter?product=wind&application=NOS.COOPS.TAC.MET&begin_date=20150101&end_date=20150114&station=8638999&time_zone=GMT&units=metric&interval=h&format=csv")

Data <- data.frame(dateChr = format(as.POSIXct(df1$`Date Time`), "%d/%m%/%Y %H:%M"),
                   val1 = df1$Speed, val2 = df2$Speed, stringsAsFactors = FALSE)
str(Data)
 'data.frame': 336 obs. of 3 variables: $ dateChr: chr "01/01/2015 00:00" "01/01/2015 01:00" "01/01/2015 02:00" "01/01/2015 03:00" ... $ val1 : num 1.42 0.51 0.91 2.08 1.3 1.27 2.08 2.33 1.7 1.95 ... $ val2 : num 1.1 0.1 2.7 3.5 4 4.1 4.1 4 3.8 4.4 ... 

舊帖子,但以防萬一其他人正在尋找。 如評論中所述,最好將日期變量轉換為真正的日期字段,然后使用scale_x_date

library(ggplot2)
Data$dateDate <- lubridate::dmy_hm(Data$dateChr)

ggplot() + 
  geom_line(data = Data, aes(x = dateDate, y = val1, group=1), color = "red") +
  geom_line(data = Data, aes(x = dateDate, y = val2, group=1), color = "blue") +
  theme_bw() +
  scale_x_date("Date", date_breaks = "4 days") + 
  ylab("Value")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM