2017-12-18 399 views
1

我很難在ggplot2盒圖上繪製Y軸上的時間。作爲ggplot2中的y軸標籤的時間

任何想法如何以時間呈現我的y軸?

目前,我的Y軸是數字,日期標籤適用於該系列。

我寧願展會時間:而不是顯示所有的數據在Y軸(HH MM),標籤

我的數據:

structure(list(Date = structure(c(17511, 17512, 17513, 17514, 17515), class = "Date"), 
T.min = c(1513584134, 1513580301, 1513582918, 1513583058, 1513584465), 
T.mean = c(1513585975.14286, 1513584408.14286, 1513584580.57143, 1513583202.2, 1513585681), 
T.max = c(1513587691, 1513587419, 1513585508, 1513583516, 1513587100), 
min_labels = c("08:02", "06:58", "07:41", "07:44", "08:07"), 
mean_labels = c("08:32", "08:06", "08:09", "07:46", "08:28"), 
max_labels = c("09:01", "08:56", "08:25", "07:51", "08:51")), .Names = c("Date", "T.min", "T.mean", "T.max", "min_labels", "mean_labels", "max_labels"), row.names = c(NA, -5L), class = "data.frame") 

我的陰謀(y軸和繪製的值是POSIXct格式):

#Library 
library(ggplot2) 
library(scales) 

#Plot 
theme_set(theme_bw()) 
ggplot(df, aes(x = Date)) + 
    geom_boxplot(aes(ymin = T.min, lower = T.min, middle = T.mean, upper = T.max, ymax = T.max), 
      stat = "identity", fill = "antiquewhite", color = "black") + 
    geom_line(aes(x=Date, y=T.mean),color='firebrick2', size=3, show.legend = FALSE) + 
    xlab('Shift Start Date') + 
    ylab('Time') + 
    coord_cartesian(ylim =c(Y.min, Y.max)) + 
    scale_x_date(date_labels = "%d-%b", breaks = pretty_breaks(5)) + 
    theme(axis.text=element_text(size=15), axis.title=element_text(size=15,face="bold")) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
    theme(axis.text.y = element_blank()) + 
    geom_text(aes(x=Date, y = T.min, label = min_labels), size=5, vjust=1.5, check_overlap=TRUE) + 
    geom_text(aes(x=Date, y = T.mean, label = mean_labels), size=5, vjust=-0.5, check_overlap=TRUE) + 
    geom_text(aes(x=Date, y = T.max, label = max_labels), size=5, vjust=-1.5, check_overlap=TRUE) + 
    theme(axis.text=element_text(size=15), axis.title=element_text(size=15,face="bold")) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

Current plot output

是我的目標對於(在MS Paint中添加標籤並且與數據不匹配,但您可以看到我想要的內容):-) What I'm aiming for

+0

y軸應該怎麼樣? – Jimbou

+0

能否請您解釋一下:「隱藏Y軸標籤....顯示y軸標籤並隱藏最小和最大數據標籤」 – PoGibas

+0

感謝您提出的問題,我編輯的帖子更加清晰。非常感謝! – Jernau

回答

1

這是我的嘗試。鑑於1513584000表示2017-12-18 08:00:00 GMT,我發現2017年12月18日06:00至10:00期間每30分鐘的值。這些數字存儲在nums中。

nums <- seq(from = 1513584000 - 7200, to = 1513584000 + 7200, length.out = 9) 

[1] 1513576800 1513578600 1513580400 1513582200 1513584000 1513585800 1513587600 1513589400 
[9] 1513591200 

然後,我轉換nums爲日期對象並使用format()提取小時和分鐘。這允許我爲y軸創建新的標籤。它們存儲在labels中。

labels <- as.POSIXct(nums, origin = "1970-01-01", tz = "GMT") %>% 
      format("%H:%M") 

# [1] "06:00" "06:30" "07:00" "07:30" "08:00" "08:30" "09:00" "09:30" "10:00" 

我使用scale_y_continuous()改變Y比例。我使用nums中的最小值和最大值設置了limits參數。

g <- ggplot(df, aes(x = Date)) + 
    geom_boxplot(aes(ymin = T.min, lower = T.min, middle = T.mean, upper = T.max, ymax = T.max), 
        stat = "identity", fill = "antiquewhite", color = "black") + 
    geom_line(aes(x=Date, y=T.mean),color='firebrick2', size=3, show.legend = FALSE) + 
    xlab('Shift Start Date') + 
    ylab('Time') + 
    ylim(c(min(df$T.min)-1800, max(df$T.max)+ 1800)) + 
    scale_x_date(date_labels = "%d-%b", breaks = pretty_breaks(5)) + 
    theme(axis.text=element_text(size=15), axis.title=element_text(size=15,face="bold")) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
    geom_text(aes(x=Date, y = T.min, label = min_labels), size=5, vjust=1.5, check_overlap=TRUE) + 
    geom_text(aes(x=Date, y = T.mean, label = mean_labels), size=5, vjust=-0.5, check_overlap=TRUE) + 
    geom_text(aes(x=Date, y = T.max, label = max_labels), size=5, vjust=-1.5, check_overlap=TRUE) + 
    theme(axis.text=element_text(size=15), axis.title=element_text(size=15,face="bold")) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
    scale_y_continuous(breaks = nums, labels = labels, limits = c(nums[1], nums[length(nums)])) 

enter image description here

+0

@Jernau我更新了我的答案。我希望這可以幫助你。 – jazzurro

+0

感謝您的幫助,非常好!我是ggplot2的新手,這真的很有幫助。 – Jernau

+0

@Jernau我之前沒有完成這項任務,所以這對我來說也是一個很好的學習機會。有可能有更好的方法來移動。但是這種方法很有效。如果您的案例已完成,請點擊向上/向下投票三角形旁邊的綠色勾號並關閉您的查詢。 – jazzurro

1

感謝@Jazzurro。提出這一方法,用小的調整,所允許的軸很容易地用於進一步的重複再利用

的最終輸出(額外數據標籤,現在可以隱藏) The Final Output

「最終」代碼被修改我解決看起來是這樣的:

#Define Y-axis range (use half hour steps) in both POSIXct and numeric forms 
Y.minT <- as.POSIXct("06:00:00" , format = "%H:%M:%S") 
Y.maxT <- as.POSIXct("12:00:00" , format = "%H:%M:%S") 
Y.min <- as.numeric(Y.minT) 
Y.max <- as.numeric(Y.maxT) 

#Create the axis breaks 
nums <- seq(from = Y.min, to = Y.max, length.out = 1+abs(2 * difftime(Y.maxT, Y.minT)[[1]])) 

#Create a vector of date labels 
labels <- as.POSIXct(nums, origin = "1970-01-01", tz = "GMT") %>% format("%H:%M") 

#Changed the y-axis as suggested, specifying 'breaks', 'labels' and 'limits' 
# scale_y_continuous(breaks = nums, labels = labels, limits = c(Y.min, Y.max)) 

#Plot 
theme_set(theme_bw()) 
ggplot(df, aes(x = Date)) + 
    geom_boxplot(aes(ymin = T.min, lower = T.min, middle = T.mean, upper = T.max, ymax = T.max), 
      stat = "identity", fill = "antiquewhite", color = "black") + 
    geom_line(aes(x=Date, y=T.mean),color='firebrick2', size=3, show.legend = FALSE) + 
    xlab('Date') + 
    ylab('Time') + 
    scale_x_date(date_labels = "%d-%b", breaks = pretty_breaks(5)) + 
    scale_y_continuous(breaks = nums, labels = labels, limits = c(Y.min, Y.max)) + 
    theme(axis.text=element_text(size=15), axis.title=element_text(size=15,face="bold")) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
    theme(axis.text=element_text(size=15), axis.title=element_text(size=15,face="bold")) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
    #geom_text(aes(x = Date, y = T.min, label = min_labels), size=5, vjust=1.5, check_overlap=TRUE) + 
    #geom_text(aes(x=Date, y = T.max, label = max_labels), size=5, vjust=-1.5, check_overlap=TRUE) + 
    geom_text(aes(x=Date, y = T.mean, label = mean_labels), size=5, vjust=-0.5, check_overlap=TRUE) 
+0

很高興看到你實現了你的目標。 :) – jazzurro