簡體   English   中英

一列中不同日期的多個繪圖 dataframe (R)

[英]Multiple plots for different dates in one column from one dataframe (R)

我有一個 dataframe 以 12 秒的間隔采集 271 天的熱流溫度數據,總共超過 100 萬次觀察。 我正在嘗試為每個日期制作單獨的線圖,以便我可以觀察模式和 select 某些日子用於我的目的。 我引用了一些與該主題相關的現有問題/答案,但我得到的最好的代碼來自這篇文章

這是名為 home47_heat_flow 的數據框內容的圖片。

我的代碼如下:

 for (var in unique(home47_heat_flow$date)) {
ggplot(subset(home47_heat_flow, date == var), aes(time, heat_flow_T)) +
      geom_line(aes(group=1)) + 
      labs(x = "Time", y = "Heat Flow Temperature (degrees C)", title = paste0(var))+
    scale_x_discrete(breaks=c("00:00:00", "01:00:00", "02:00:00", "03:00:00", "04:00:00", "05:00:00", "06:00:00", "07:00:00", "08:00:00", "09:00:00", "10:00:00", "11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00", "19:00:00", "20:00:00", "21:00:00", "22:00:00", "23:00:00"), labels=c("00:00:00", "01:00:00", "02:00:00", "03:00:00", "04:00:00", "05:00:00", "06:00:00", "07:00:00", "08:00:00", "09:00:00", "10:00:00", "11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00", "19:00:00", "20:00:00", "21:00:00", "22:00:00", "23:00:00"))+
    theme(axis.text.x = element_text(angle = -90, vjust = 1, hjust=1))
    ggsave(paste0(var,'.png'), width = 20, height = 20, units = "cm")
        } #maybe try removing breaks and instead of ggsave try to print plots
    
    cat('your plots were saved into;', getwd())
    }

這段代碼是我最接近工作的,我喜歡它把所有的情節都保存到我的驅動器上。 我也嘗試了以下方法,但一夜之間多次離開它仍然沒有完成,可能是因為有 271 個不同的日期? 很多情節要創建?

ggplot(home47_heat_flow, aes(time, heat_flow_T)) + geom_line(group=1) + facet_grid(. ~ date)

盡管第一個代碼(使用 for 循環)運行並創建了所有 271 個圖,但它們的輸出不正確。 標題與對應的日期不匹配,x 軸也不像我希望的那樣 label - 例如這里:在此處輸入圖像描述 在此處輸入圖像描述

The first date 2016-08-10 produces the following plot titled 17023 and saved as "17023.png" and the second date 2016-08-11 produces the plot titled 17024 and saved as "17024.png"- all 271 plots are titled並按時間順序保存為數字 17023-17294,並以與 dataframe 中出現的日期相同的順序保存,但我必須為其他幾個數據幀/天執行此操作,並且希望標題/文件名是正確的日期。 所有的 x 軸標簽也不同,每個 plot 隨機出現不同的時間。 我希望 x 軸標簽每個小時都有一個標記,就像我認為我編碼的那樣。 我認為日期與標題或文件名不對應且時間未正確顯示的錯誤是由於兩列的 class 造成的。 創建上述圖像時,日期 class 為“日期”,時間 class 為“字符”。 我將日期 class 更改為字符串/字符,因此它們將類似於“2016-08-10”,但代碼的 ggsave 部分給了我一個錯誤“grDevices::dev.off() 中的錯誤:QuartzBitmap_Output - 無法打開文件”我查找了解決方案,據說嘗試另存為 pdf 但隨后出現錯誤:“grDevices :: pdf中的錯誤(文件=文件名,...,版本=版本):pdf中的文件名太長( )"

然后我嘗試將數據保留為日期 class 並將時間更改為 POSIXct。 我得到的 plot 是這個(這是日期 2016-08-11,不知何故,日期在 x 軸上顯示為 9 月 20 日(即今天的日期)並且不正確,標題仍然是 17024,它應該是正確的日期 2016-08-11)在此處輸入圖像描述

所以基本上我不確定我是否在這里遺漏了一些愚蠢的東西,比如日期和時間需要在某個 class 到 plot 中正確獲取正確的 output 並保存正確的文件。 如果您有任何見解,將不勝感激!

if語句中, var被隱式轉換為數字,因此您需要先將其轉換回日期。 您的 x 軸應使用scale_x_datetime ,並將utc_datetime作為 x acis 變量。 您可以使用適當的標簽指定每小時休息時間。

顯然我們沒有您的數據,因此我使用了與您的實際數據具有相同名稱和結構的虛構數據集。 以下代碼應該適合您

library(ggplot2)

for (var in unique(home47_heat_flow$date)) {
  
  var <- as.Date(var, origin = "1970-01-01")
  
  ggplot(subset(home47_heat_flow, date == var), 
         aes(utc_datetime, heat_flow_T)) +
    geom_line(aes(group = 1)) + 
    labs(x = "Time", y = "Heat Flow Temperature (degrees C)", 
         title = var) +
    scale_x_datetime(date_breaks = "1 hour", date_labels = "%H:%M",
                     limits = c(as.POSIXct(var), as.POSIXct(var + 1)),
                     expand = c(0, 0)) +
    theme(axis.text.x = element_text(angle = -90, vjust = 1, hjust = 1))
  
    ggsave(paste0(var,'.png'), width = 20, height = 20, units = "cm")
}

這導致

2016-08-10.png

在此處輸入圖像描述

2016-08-11.png

在此處輸入圖像描述

等等


使用的數據

set.seed(1)
home47_heat_flow <- data.frame(utc_datetime = seq(
  as.POSIXct("2016-08-10 00:00:00"), by = "12 sec", length.out = 30000),
  heat_flow_T = cumsum(runif(30000, -1, 1)) + 200)
home47_heat_flow$date <- as.Date(home47_heat_flow$utc_datetime)

head(home47_heat_flow)
#>          utc_datetime heat_flow_T       date
#> 1 2016-08-10 00:00:00    199.5310 2016-08-10
#> 2 2016-08-10 00:00:12    199.2753 2016-08-10
#> 3 2016-08-10 00:00:24    199.4210 2016-08-10
#> 4 2016-08-10 00:00:36    200.2374 2016-08-10
#> 5 2016-08-10 00:00:48    199.6408 2016-08-10
#> 6 2016-08-10 00:01:00    200.4375 2016-08-10

使用reprex v2.0.2創建於 2022-09-20

暫無
暫無

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

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