r - 在 ggplot 版本 3+ 中反转日期时间(POSIXct 数据)轴
问题描述
试图让ggplot 中的反向日期时间(POSIXct 数据)轴在当前版本(R 3.6.2,ggplot 3.2.1)下工作。
### THIS NOW WORKS ###
library(lubridate)
library(tidyverse)
library(scales)
# Some random-ish date and values to plot
n <- 3700
myData <- tibble(timestamp = now() + seq(1:n) , value = sin(seq(1:n)/100) + rnorm(n)/10)
# the 'solution' per above link
c_trans <- function(a, b, breaks = b$breaks, format = b$format) {
a <- scales::as.trans(a)
b <- scales::as.trans(b)
name <- paste(a$name, b$name, sep = "-")
trans <- function(x) a$trans(b$trans(x))
inv <- function(x) b$inverse(a$inverse(x))
trans_new(name, trans, inverse = inv, breaks = breaks, format=format)
}
rev_date <- c_trans("reverse", "time")
## I would like this output with the y-axis in reverse order (top to bottom) as this is a standard practise in this application.
ggplot(myData, aes(x=value, y=timestamp)) +
geom_path() +
scale_y_continuous( trans=rev_date)
给出错误Error: Invalid input: time_trans works with objects of class POSIXct only
但时间戳属于 POSIXct 类
myData %>% str()
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3700 obs. of 2 variables:
$ timestamp: POSIXct, format: "2019-12-31 10:08:00" "2019-12-31 10:08:01" "2019-12-31 10:08:02" "2019-12-31 10:08:03" ...
$ value : num -0.00202 -0.06392 -0.01515 0.11406 0.16945 ...
解决方案
推荐阅读
- swupdate - 如何对 swupdate Web 界面实施身份验证(用户/密码)
- java - Java binarySearch 找不到所有字符
- python - 为什么我的代码返回随机字母标记,而不是单词标记?
- java - 将 node.js 应用程序作为依赖项添加到 java maven 项目
- c - else if 和 else {if} 的区别
- java - 如何生成、编译、jar、依赖一个gradle模块
- regex - “至少包含以下字母之一”类型的正则表达式问题
- google-cloud-platform - 如何在 Bigquery 中使用字符串作为列名
- node.js - Google Drive Api v3 获取下载进度
- android - KOTLIN 不是 Java!如何在片段中使用 maketoast(或在片段中进行点击)