我正在繪制路由器統計資訊(從
同樣,我希望每個方面中 x 軸的范圍覆寫整個月。因此,我希望 2021-12 方面的 X 軸從 2021 年 12 月 1 日到 2021 年 12 月 31 日運行,2022-01 方面的 X 軸從 2022 年 1 月 1 日到 2022 年 1 月 31 日運行。
有什么方法可以在 ggplot2 中強制執行此操作嗎?
一個額外的、較小的自包含示例來嘗試您的代碼:
X.df <- tribble(
~date, ~month, ~Download,
"2021-12-01T00:30:36Z","2021-12",20.13,
"2021-12-07T06:30:31Z","2021-12",38.95,
"2021-12-14T08:00:31Z","2021-12",38.44,
"2021-12-21T09:30:29Z","2021-12",28.57,
"2021-12-28T16:00:31Z","2021-12",30.78,
"2021-12-31T13:00:28Z","2021-12",55.45,
"2022-01-01T00:00:28Z","2022-1",55.44,
"2022-01-01T02:30:29Z","2022-1",55.63,
"2022-01-01T03:00:29Z","2022-1",55.75,
"2022-01-01T05:00:29Z","2022-1",55.8,
"2022-01-07T03:00:29Z","2022-1",53.6,
"2022-01-07T05:00:29Z","2022-1",51.8
)
與往常一樣,提前致謝。皮特
uj5u.com熱心網友回復:
更新 II:洗掉了以前的版本:
- 在您的資料庫中,只有一個 2022 年 1 月的日期
- 在資料框中,我們使用
completefromtidyrpackage完成了 2022 年 1 月的日期。
library(tidyverse)
library(lubridate)
X.df %>%
mutate(date = ymd(date)) %>%
group_by(month(date)) %>%
complete(date = seq(min(date), max(ceiling_date(date, unit = "month") - ddays(1)), by = 'day')) %>%
fill(month) %>%
ggplot(aes(x = date, Download, colour = Download))
geom_line()
facet_wrap(~ month, scale="free_x", ncol = 1)
scale_colour_gradient(low="red",high="green", limits=c(0.0, 50), oob = squish)
scale_x_date(date_breaks = "1 day", date_labels = "%d/%m", expand = c(0, 0))
coord_cartesian(ylim = c(0, 60))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/403328.html
標籤:
