R:如何在ggplot字幕中将2019-04-29显示为2019年5月29日?

时间:2019-05-12 14:56:25

标签: r ggplot2

我需要将最小日期值和最大日期值放在ggplot 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"
  )

1 个答案:

答案 0 :(得分:1)

使用:

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"))