【问题标题】:How to use roman numerals in ggplot's scale_x_date?如何在 ggplot 的 scale_x_date 中使用罗马数字?
【发布时间】:2021-10-17 14:41:08
【问题描述】:

我需要将格式字符串“2016-06-29”更改为:29.V.2016 我试过了:

scale_x_date(date_labels = paste("%d", as.roman("%m"), "%Y", sep = "."))

但我得到的唯一结果是: 错误:输入无效:date_trans 仅适用于 Date 类的对象 另外:警告信息: 在 .roman2numeric(x) 中:无效的罗马数字:%m

【问题讨论】:

    标签: r ggplot2 roman-numerals


    【解决方案1】:

    实现所需结果的一个选项是通过scale_x_datelabels 参数。在下面的代码中,我使用自定义函数将日期转换为您想要的格式,其中包含月份的罗马文字:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多