r - 如何在 ggplot 的 scale_x_date 中使用罗马数字?
问题描述
我需要将格式字符串“2016-06-29”更改为:29.V.2016 我尝试过:
scale_x_date(date_labels = paste("%d", as.roman("%m"), "%Y", sep = "."))
但我得到的唯一结果是:错误:无效输入:date_trans 仅适用于 Date 类的对象此外:警告消息:在 .roman2numeric(x) 中:无效罗马数字:%m
解决方案
实现所需结果的一种选择是labels
通过scale_x_date
. 在下面的代码中,我使用自定义函数将日期转换为您想要的格式,并使用罗马文字为月份:
library(ggplot2)
date_roman <- function(x, sep = ".") {
paste(format(x, "%Y"), as.roman(as.numeric(format(x, "%m"))), format(x, "%d"), sep = sep)
}
x <- as.Date("2016-06-29")
date_roman(x)
#> [1] "2016.VI.29"
ggplot(subset(economics, as.numeric(format(date, "%Y")) == 2000), aes(date, psavert)) +
geom_line() +
scale_x_date(labels = date_roman)
推荐阅读
- python - 如何将列表列表与字符串列表结合起来
- ember.js - 如何从已部署的 ember 项目转到本地开发项目?
- kivy - buildozer“调试安卓调试”
- sql - 在 R Markdown 中使用 SQL?
- python - 计算 FileResponse 对象内容的 SHA256 哈希
- python - 在 Python 中使用条件元组对数据进行子集化
- c# - 在 TreeView 中的绑定集合中向上/向下移动项目重复项
- c# - 声明转换期间调用 API
- blueprism - 如何更改 Blue Prism Collection 中的列名?
- rest - DTO 应该代表嵌套的实体结构,还是应该将我的路径设置为每个嵌套对象都有一个端点?