首页 > 解决方案 > R:如何在 ggplot 字幕中将 2019-04-29 显示为 2019 年 5 月 29 日?

问题描述

我需要将最小日期值和最大日期值放入ggplot subtitle.

我发现了一个类似的问题,但是对于轴标签,我需要将其应用于 subtitle 参数:

subtitle = paste0("Del ", vitocho_likes_min_date, " al ", vitocho_likes_max_date)

最小日期看起来像

"2010-10-14" #Expect: "14 de octubre del 2010"

dput(vitocho_likes_min_date)
structure(14896, class = "Date")

最大日期如下所示

"2019-04-29" #Expect: "29 de abril del 2019"

dput(vitocho_likes_max_date)
structure(18015, class = "Date")

这是我的 ggplot 图表:

vitocho_chart <- t_kids_faves %>%
  filter(user == "VictorAndresGB") %>%
  ggplot(aes(x = fct_reorder(screen_name, n), y = n)) +
  geom_col(fill = "#494A4F") +
  coord_flip() +
  theme_tweets() +
  labs(
    x = "",
    y = "",
    title = "Cuentas de Twitter con más likes de Victor Andrés García Belaunde.",
    subtitle = paste0("Del ", vitocho_likes_min_date, " al ", vitocho_likes_max_date)
  ) +
  geom_text(
    aes(x = screen_name,
        y = n - 15,
        label = n
    ),
    size = 4,
    color = "gray95"
  )

标签: rggplot2

解决方案


利用:

Sys.setlocale("LC_TIME", "Spanish") 
vitocho_likes_min_date= as.character(format(as.Date(14896, origin="1970-01-01"), "%d de %B del %Y"))
vitocho_likes_max_date= as.character(format(as.Date(18015, origin="1970-01-01"), "%d de %B del %Y"))

推荐阅读