r - 如何使 scale_y_reverse 与 scale_y_time 一起工作
问题描述
我在 y 轴上绘制HH :MM:SS ,在 x 轴上绘制YYYY:MM,但是在尝试将scale_y_reverse与scale_y_time一起使用时遇到问题。
样本数据如下:
library(hms)
library(ggplot2)
df_stack <- data.frame(yr_mn = rep(c("2020-01", "2020-02", "2020-03", "2020-04"),2),
timept_type = c(rep("A",4),rep("B",4)),
timept = as_hms(c("08:00:00", "09:10:00", "11:05:30", "10:45:00", "09:30:10", "10:00:00", "11:25:00", "08:40:00")))
df_stack %>%
ggplot(., aes(x=yr_mn, y=timept, color=timept, fill=timept)) +
geom_point() +
geom_line(aes(group=1))+
facet_wrap(vars(timept_type), scales="free", ncol=2) +
scale_y_time(limits = c(as.POSIXct(as_hms("07:00:00")), as.POSIXct(as_hms("11:30:00")))) +
scale_y_reverse() # this cause the y-axis labels to no longer be in hh:mm:ss format
如果我注释掉最后一行scale_y_reverse()那么图表看起来很好,但如果我包含它,scale_y_reverse似乎将HH:MM:SS转换为秒。我不知道这是我做错了什么,还是scale_y_time中有内置方法来解决这个问题。谢谢!
解决方案
您可以省略scale_y_time
和提供函数来scale_y_reverse
生成标签和中断。
df_stack %>%
ggplot(aes(yr_mn, timept, color = timept, fill = timept)) +
geom_point() +
geom_line(aes(group = 1)) +
facet_wrap(vars(timept_type), scales = "free", ncol = 2) +
scale_y_reverse(labels = function(x) as_hms(x),
breaks = seq(as.numeric(as_hms("07:00:00")),
as.numeric(as_hms("11:30:00")),
1800))
结果:
推荐阅读
- c# - c# winform webview2如何浏览器inputtag光标显示?
- webgl - Autodesk Forge Reality Capture 生成 OBJ 到 3D Web 查看器的简单示例
- cytoscape.js - 在 cytoscape.js 中,mouseover 事件仅在鼠标移动时触发
- vuejs2 - Vue-js2 将 props 传递给组件是意外的反应式
- rss - 从 MailChimp 中的 rss 提要获取自定义标签
- r - 如何按行名在 2 个不同的数据集中附加 2 列
- phpstorm - Ubuntu 20 上 PhpStorm 中的异常鼠标行为
- authorization - 当从 Identity Server 4 令牌中检索到多个角色时,User.IsInRole() 失败
- docker - 如何使用 zfs 在 docker 卷上设置 qouta
- python - ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 在 Prediciton 中具有值 1