【问题标题】:Time as y- axis labels in ggplot2时间作为 ggplot2 中的 y 轴标签
【发布时间】:2017-12-18 12:42:05
【问题描述】:

我很难在 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))

我的目标是什么(在 MS Paint 中添加的标签与数据不匹配,但你可以看到我想要什么):-)

【问题讨论】:

  • y 轴应该是什么样子?
  • 您能否解释一下:“隐藏 Y 轴标签....显示 y 轴标签并隐藏最小和最大数据标签”
  • 感谢您的提问,我已将帖子编辑得更清楚。非常感谢!
  • T.mean 中的单位是秒还是特定小时?
  • 您好,数值是POSIXct,所以单位是秒。我想要实现的输出格式是小时和分钟。

标签: r plot ggplot2 time


【解决方案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)]))

【讨论】:

  • 感谢您的帮助,太好了!我是 ggplot2 的新手,这真的很有帮助。
  • @Jernau 我以前没有做过这个任务,所以这对我来说也是一个很好的学习机会。可能有更好的移动方式。但这种方法有效。如果您的案例已完成,请点击支持/反对投票三角形旁边的绿色勾号并关闭您的查询。
  • 这里的关键是将 y 轴设置为连续并指定“中断”、“标签”和“限制”。我将在下面发布我的最终代码。
【解决方案2】:

感谢@Jazzurro。建议的方法只需稍加调整,即可轻松修改轴,以便在进一步的绘图中重复使用

最终输出(现在可以隐藏额外的数据标签)

我确定的“最终”代码如下所示:

#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)

【讨论】:

  • 很高兴看到您实现了目标。 :)
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多