x轴日期上的R位移标度具有不连续的时间序列 [英] R shift scale at x axis date with a non-continuous sequence of time
问题描述
我有从每年一月到十二月的连续两年(2017-2018)的时间序列数据。然后我需要绘制从9月17日到4月18日的数据。
我可以用非常手工制作的代码来做,但是我意识到可以做到使用当今可用的软件包非常简单的方法来管理地块上的日期(软件包刻度,润滑等)。
有人可以帮我简化我的工作吗?做第二个情节?
我真的会很感激。
preventWarnings (suppressMessages(library( tidyverse,悄悄地= T)))
dat<-tibble(
date = seq(as.Date( 2017-01- 01),as.Date( 2018-12-31),by = 1),
var = rgamma(length(date),shape = 2,scale = 2))%>%
mutate(年= lubridate ::年(日期),
month = lubridate ::月(日期),
朱利安= lubridate :: yday(日期))
dat
#> #小动作:730 x 5
#> date var year month julian
#> < date> < dbl> < dbl> < dbl> < dbl>
#> 1 2017-01-01 12.9 2017 1 1
#> 2 2017-01-02 6.69 2017 1 2
#> 3 2017-01-03 6.11 2017 1 3
#> 4 2017-01-04 1.68 2017 1 4
#> 5 2017-01-05 1.22 2017 1 5
#> 6 2017-01-06 10.2 2017 1 6
#> 7 2017-01-07 5.13 2017 1 7
#> 8 2017-01-08 4.61 2017 1 8
#> 9 2017-01-09 3.79 2017 1 9
#> 10 2017-01-10 1.11 2017 1 10
#> #…有720多行
dat%&%;%
ggplot()+
geom_line(aes(julian,var,color = factor(month),linetype = factor(年)))
dat%>%
filter(( year == 2017& month%in%c( 9, 10, 11, 12))|
(year == 2018& month%in%c( 1 , 2, 3)))%>%
mutate(julian_AWS = ifelse(julian> = 244,julian-243,julian + 123))%> %% b $ b ggplot() +
geom_line(aes(julian_AWS,var,color = factor(month),linetype = factor(year)))+
scale_x_continuous(breaks = c(1,#S
31,# O
61,#N
91,#D
121,#E
151,#F
181),#M
la bels = c( Sep, Oct, Nov, Dec, Jan, Feb, Mar))+
主题(axis.text.x = element_text(hjust = -1))
由
有关日期标签格式的更多信息,请参见?strftime
I have a time serie data from two consecutive years (2017-2018), from january to december of each year. Then I need to plot the data from sept-17 to april-18.
I could do it with a very hand-made code, however I realize it could be done very much straightforward way with the packages availabe today for managing dates on plots (packages "scales", "lubridate", etc.)
Can someone help me to simplify my work for doing the second plot? I will really appreciate it.
suppressWarnings(suppressMessages(library("tidyverse", quietly = T)))
dat <- tibble(
date = seq(as.Date("2017-01-01"), as.Date("2018-12-31"), by=1),
var = rgamma(length(date), shape=2, scale=2)) %>%
mutate(year = lubridate::year(date),
month = lubridate::month(date),
julian = lubridate::yday(date))
dat
#> # A tibble: 730 x 5
#> date var year month julian
#> <date> <dbl> <dbl> <dbl> <dbl>
#> 1 2017-01-01 12.9 2017 1 1
#> 2 2017-01-02 6.69 2017 1 2
#> 3 2017-01-03 6.11 2017 1 3
#> 4 2017-01-04 1.68 2017 1 4
#> 5 2017-01-05 1.22 2017 1 5
#> 6 2017-01-06 10.2 2017 1 6
#> 7 2017-01-07 5.13 2017 1 7
#> 8 2017-01-08 4.61 2017 1 8
#> 9 2017-01-09 3.79 2017 1 9
#> 10 2017-01-10 1.11 2017 1 10
#> # … with 720 more rows
dat %>%
ggplot() +
geom_line(aes(julian, var, color = factor(month), linetype=factor(year)))
dat %>%
filter((year == 2017 & month %in% c("9","10", "11", "12"))|
(year == 2018 & month %in% c("1", "2", "3"))) %>%
mutate(julian_AWS = ifelse(julian>=244, julian-243, julian+123)) %>%
ggplot() +
geom_line(aes(julian_AWS, var, color = factor(month), linetype=factor(year)))+
scale_x_continuous(breaks = c(1,#S
31,#O
61,#N
91,#D
121,#E
151,#F
181),#M
labels = c("Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar"))+
theme(axis.text.x=element_text(hjust=-1))
Created on 2019-05-05 by the reprex package (v0.2.1)
I don't think you need to delve into the julian date formats. See if this gets you what you need:
dat %>%
filter(date >= '2017-09-01', date < '2018-04-01') %>%
ggplot() +
geom_line(aes(date, var, color = factor(month), linetype = factor(year))) +
scale_x_date(date_breaks = "1 month", date_labels = "%b") +
theme(axis.text.x = element_text(hjust = -1))
For more info on date label formats, see ?strftime
这篇关于x轴日期上的R位移标度具有不连续的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!