You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改ggplot中facet_calendar的日期标签:将月份缩写改为全名

解决方法

要将facet_calendar的月份标签从3字母缩写改为完整名称,只需利用该函数的month_format参数,指定日期格式字符串"%B"即可(该格式对应完整月份名称)。

修改后的完整代码

library(tidyverse)
library(lubridate)
library(ggtimeplot)

df1 <- read.table(text = "DT   odczyt.1 odczyt.2
'2023-08-14 00:00:00'   362 1.5
'2023-08-14 23:00:00'   633 4.3
'2023-08-15 05:00:00'   224 1.6
'2023-08-15 23:00:00'   445 5.6
'2023-08-16 00:00:00'   182 1.5
'2023-08-16 23:00:00'   493 4.3
'2023-08-17 05:00:00'   434 1.6
'2023-08-17 23:00:00'   485 5.6
'2023-08-18 00:00:00'   686 1.5
'2023-08-18 23:00:00'   487 6.8
'2023-08-19 00:00:00'   566 1.5
'2023-08-19 05:00:00'   278 7.9
'2023-08-19 17:00:00'   561 11.5
'2023-08-19 18:00:00'   365 8.5
'2023-08-19 22:00:00'   170 1.8
'2023-08-19 23:00:00'   456 6.6
'2023-08-20 00:00:00'   498 1.5
'2023-08-20 03:00:00'   961 1.54
'2023-08-20 05:00:00'   397 1.6
'2023-08-20 19:00:00'   532 6.6
'2023-08-20 23:00:00'   493 3.8
'2023-08-21 01:00:00'   441 9.2
'2023-08-21 07:00:00'   793 8.5
'2023-08-21 13:00:00'   395 5.5", header = TRUE) %>% 
  mutate (
    DT = as.POSIXct(DT),
    Rok = year(DT),
    Msc = month(DT),
    Godz = hour(DT),
    Dzien = day(DT),
    Date = as.Date(paste0(Rok, "-", Msc, "-", Dzien))
  )

ggplot(df1 %>% filter(Rok == 2023 & Msc == 8)) +
  geom_col(aes(x = Godz, y = odczyt.1)) +
  facet_calendar(~Date, month_format = "%B")  # 新增参数控制月份显示格式

额外说明

  • 若需要显示中文完整月份,可先设置本地化语言:
    Sys.setlocale("LC_TIME", "Chinese")
    
    再运行绘图代码,标签就会变成“八月”这类中文名称。
  • month_format支持多种标准日期格式:
    • "%b":默认的3字母月份缩写
    • "%B":完整月份名称
    • "%m":数字格式月份(如08)

内容的提问来源于stack exchange,提问作者GrBa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 14:23:17