2017-08-31 51 views
0

我想將一些時間序列數據放在類似熱圖的圖中。下面我做了一個簡明的例子。我對「大型」數據blob表示歉意,但我用更多的人造示例難以再現它。X軸上的數據中斷出現在一天結束而不是開始

問題是,在這個例子中,數據從28日到30日。但在情節中,你會認爲有從27日到29日的數據。 休息是放在一天結束時,而不是在開始時,這是很自然的。

我已經嘗試過任何我能想到的事情,手動設置休息時間,但這並不會改變他們放置的位置。

那麼我該如何改變它,以便顯示的日期是29日和30日?

DATA:

data <- structure(list(time_run = structure(c(1503968136, 1503938008, 
1503962343, 1503956548, 1503917153, 1504059128, 1504011336, 1504078913, 
1503915993, 1503968136, 1503938008, 1503962343, 1503956548, 1503917153, 
1504059128, 1504011336, 1504078913, 1503915993, 1503968136, 1503938008, 
1503962343, 1503956548, 1503917153, 1504059128, 1504011336, 1504078913, 
1503915993, 1503968136, 1503938008, 1503962343, 1503956548, 1503917153, 
1504059128, 1504011336, 1504078913, 1503915993, 1503968136, 1503938008, 
1503962343, 1503956548, 1503917153, 1504059128, 1504011336, 1504078913, 
1503915993, 1503968136, 1503938008, 1503962343, 1503956548, 1503917153, 
1504059128, 1504011336, 1504078913, 1503915993, 1503968136, 1503938008, 
1503962343, 1503956548, 1503917153, 1504059128, 1504011336, 1504078913, 
1503915993, 1503968136, 1503938008, 1503962343, 1503956548, 1503917153, 
1504059128, 1504011336, 1504078913, 1503915993, 1503968136, 1503938008, 
1503962343, 1503956548, 1503917153, 1504059128, 1504011336, 1504078913, 
1503915993, 1503968136, 1503938008, 1503962343, 1503956548, 1503917153, 
1504059128, 1504011336, 1504078913, 1503915993, 1503968136, 1503938008, 
1503962343, 1503956548, 1503917153, 1504059128, 1504011336, 1504078913, 
1503915993, 1503968136, 1503938008, 1503962343, 1503956548, 1503917153, 
1504059128, 1504011336, 1504078913, 1503915993), class = c("POSIXct", 
"POSIXt"), tzone = "Europe/Paris"), x_text = c("NMP (17)", "NMP (17)", 
"NMP (17)", "NMP (17)", "NMP (17)", "NMP (17)", "NMP (17)", "NMP (17)", 
"NMP (17)", "TEA (22)", "TEA (22)", "TEA (22)", "TEA (22)", "TEA (22)", 
"TEA (22)", "TEA (22)", "TEA (22)", "TEA (22)", "ACN (23)", "ACN (23)", 
"ACN (23)", "ACN (23)", "ACN (23)", "ACN (23)", "ACN (23)", "ACN (23)", 
"ACN (23)", "Acetonitrile/Dimethylformamide (29)", "Acetonitrile/Dimethylformamide (29)", 
"Acetonitrile/Dimethylformamide (29)", "Acetonitrile/Dimethylformamide (29)", 
"Acetonitrile/Dimethylformamide (29)", "Acetonitrile/Dimethylformamide (29)", 
"Acetonitrile/Dimethylformamide (29)", "Acetonitrile/Dimethylformamide (29)", 
"Acetonitrile/Dimethylformamide (29)", "DMAP (33)", "DMAP (33)", 
"DMAP (33)", "DMAP (33)", "DMAP (33)", "DMAP (33)", "DMAP (33)", 
"DMAP (33)", "DMAP (33)", "DIPEA (36)", "DIPEA (36)", "DIPEA (36)", 
"DIPEA (36)", "DIPEA (36)", "DIPEA (36)", "DIPEA (36)", "DIPEA (36)", 
"DIPEA (36)", "TPA (42)", "TPA (42)", "TPA (42)", "TPA (42)", 
"TPA (42)", "TPA (42)", "TPA (42)", "TPA (42)", "TPA (42)", "ACN (43)", 
"ACN (43)", "ACN (43)", "ACN (43)", "ACN (43)", "ACN (43)", "ACN (43)", 
"ACN (43)", "ACN (43)", "ACN (46)", "ACN (46)", "ACN (46)", "ACN (46)", 
"ACN (46)", "ACN (46)", "ACN (46)", "ACN (46)", "ACN (46)", "Acetonitrile/Methanol (47)", 
"Acetonitrile/Methanol (47)", "Acetonitrile/Methanol (47)", "Acetonitrile/Methanol (47)", 
"Acetonitrile/Methanol (47)", "Acetonitrile/Methanol (47)", "Acetonitrile/Methanol (47)", 
"Acetonitrile/Methanol (47)", "Acetonitrile/Methanol (47)", "Pthalic Anhydride (48)", 
"Pthalic Anhydride (48)", "Pthalic Anhydride (48)", "Pthalic Anhydride (48)", 
"Pthalic Anhydride (48)", "Pthalic Anhydride (48)", "Pthalic Anhydride (48)", 
"Pthalic Anhydride (48)", "Pthalic Anhydride (48)", "ACN (26)", 
"ACN (26)", "ACN (26)", "ACN (26)", "ACN (26)", "ACN (26)", "ACN (26)", 
"ACN (26)", "ACN (26)"), c_ord = c(24L, 24L, 24L, 24L, 24L, 24L, 
24L, 24L, 24L, 82L, 82L, 82L, 82L, 82L, 82L, 82L, 82L, 82L, 29L, 
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 27L, 27L, 27L, 27L, 
27L, 27L, 27L, 27L, 27L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 
31L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L), value = c(9837.2490234375, 
9145.20068359375, 8794.9443359375, 8782.52001953125, 10083.5, 
9837.2900390625, 8973.68896484375, 8972.79248046875, 10112.3569335938, 
3006.42431640625, 2483.09973144531, 2297.7724609375, 2388.14697265625, 
3180.48327636719, 2907.27026367188, 2407.63500976562, 2381.24938964844, 
3600.30932617188, 809.412658691406, 790.167205810547, 698.132263183594, 
715.636016845703, 1123.26507568359, 728.464904785156, 653.518646240234, 
581.541839599609, 1139.99267578125, 1109.13757324219, 1007.88500976562, 
960.396789550781, 992.408813476562, 1136.28955078125, 1090.0615234375, 
1000.72674560547, 963.919616699219, 1155.56555175781, 2972.392578125, 
3045.79138183594, 2519.54443359375, 2830.70581054688, 4292.25, 
2794.20654296875, 2464.75134277344, 2213.01623535156, 3474.59985351562, 
6243.59130859375, 5039.56372070312, 4662.6640625, 4833.80029296875, 
6457.42724609375, 6183.39208984375, 5053.65649414062, 4982.33056640625, 
7820.1474609375, 569.280151367188, 525.962310791016, NA, NA, 
568.004791259766, 568.837036132812, 526.013427734375, 529.354675292969, 
601.071899414062, 6509.4921875, 5798.27709960938, 5015.94189453125, 
5225.66235351562, 8109.38256835938, 5580.72314453125, 4410.88842773438, 
3981.27697753906, 8123.06518554688, 3046.7998046875, 2847.50915527344, 
2434.40698242188, 2575.9501953125, 3872.27795410156, 2554.265625, 
2210.39123535156, 2017.80932617188, 3846.21325683594, 720.536865234375, 
744.396423339844, 743.815673828125, 683.698913574219, 800.720550537109, 
693.875, 719.847290039062, 708.015197753906, 767.747467041016, 
10337.0947265625, 9417.3125, 9031.552734375, 9194.966796875, 
10259.4594726562, 10270.6875, 9491.2001953125, 9433.85546875, 
10691.4228515625, NA, NA, NA, NA, 551.797607421875, NA, NA, NA, 
547.68701171875)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-108L), .Names = c("time_run", "x_text", "c_ord", "value")) 

CODE:

library(ggplot2) 
library(scales) 


    p <- ggplot(data=data, aes(x=time_run, y = reorder(x_text, c_ord), fill=log10(value))) + 
     geom_tile() + 
     scale_x_datetime(labels = date_format("%Y-%m-%d"), breaks=date_breaks("day")) + 
     theme_classic() + 
     theme(axis.text.x = element_text(angle=30, hjust = 1)) + 
     theme(panel.background=element_rect(fill="lightgrey", colour="lightgrey")) 


     p 

enter image description here

sort(unique(data$time_run)) 

[1] "2017-08-28 12:26:33 CEST" "2017-08-28 12:45:53 CEST" "2017-08-28 18:33:28 CEST" "2017-08-28 23:42:28 CEST" "2017-08-29 01:19:03 CEST" 
[6] "2017-08-29 02:55:36 CEST" "2017-08-29 14:55:36 CEST" "2017-08-30 04:12:08 CEST" "2017-08-30 09:41:53 CEST" 

回答

2

它似乎在DATE_FORMAT使用時區造成的()。你可以編寫自己的格式函數,它不包含任何時區信息。

my_date_format <- function (format = "%Y-%m-%d") { 
    function(x) format(x, format) 
} 

p <- ggplot(data=data, aes(x=time_run, y = reorder(x_text, c_ord), fill=log10(value))) + 
    geom_tile() + 
    scale_x_datetime(labels = my_date_format("%Y-%m-%d"), breaks=date_breaks("day")) + 
    theme_classic() + 
    theme(axis.text.x = element_text(angle=30, hjust = 1)) + 
    theme(panel.background=element_rect(fill="lightgrey", colour="lightgrey")) 


p 
+0

謝謝!基於這個提示,我發現你也可以使用'lubridate'來做'labels = date_format(「%Y-%m-%d」,tz = tz(data $ time_run))''。看起來情節的實際位置不受時區的影響。 –

相關問題