2017-12-18 35 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