[英]x axis as week number and secondary x-axis as date
我正在嘗試 plot 時間序列,其中主要 x 軸作為數字,次要 x 軸作為 ggplot 中的日期。 這是我可憐的嘗試。
library(tidyverse)
tibble::tribble(
~date, ~ndvi,
"2020-05-18", 0.7655,
"2020-06-14", 0.723,
"2020-07-12", 0.6178,
"2020-08-21", 0.437,
"2020-09-07", 0.4763,
"2020-09-10", 0.4928,
"2020-09-12", 0.4831,
"2020-09-22", 0.4774,
"2020-10-02", 0.5794,
"2020-10-07", 0.606
) %>%
mutate(date = lubridate::ymd(date),
weeks = difftime(date, min(date), units="weeks")) %>%
ggplot(aes(weeks, ndvi)) +
geom_line()+
scale_x_datetime(
sec.axis = dup_axis(name = "", #breaks = date,
labels = scales::time_format("%b")))
#> Error: Invalid input: time_trans works with objects of class POSIXct only
這是所需的 output,其中主要 x 軸有幾周,次要 x 軸有時間序列的月份
分別為兩個 x 軸設置標簽。 也請檢查這個討論
library(tidyverse)
mydat <- tibble::tribble(
~date, ~ndvi,
"2020-05-18", 0.7655,
"2020-06-14", 0.723,
"2020-07-12", 0.6178,
"2020-08-21", 0.437,
"2020-09-07", 0.4763,
"2020-09-10", 0.4928,
"2020-09-12", 0.4831,
"2020-09-22", 0.4774,
"2020-10-02", 0.5794,
"2020-10-07", 0.606
) %>%
mutate(date = lubridate::ymd(date),
weeks = as.numeric(difftime(date, min(date), units="weeks")))
mydat %>%
ggplot(aes(date, ndvi)) +
geom_line() +
scale_x_date(date_breaks = "4 weeks", labels = scales::date_format("%W"),
sec.axis = dup_axis(name = "", labels = scales::date_format("%b")))
由reprex package (v1.0.0) 於 2021 年 2 月 10 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.