简体   繁体   中英

Plotting time series data in R by year

I have a series of observations taken at almost monthly intervals and want to plot them. Some months may have more than one observation so the Day is important.

I can easily plot them as a single line but I want each years observations to be plotted on a separate line so that comparisons can be made between years (ie one line for 2018, another for 2019, another for 2020).

I want my x-axis to run from 1st Jan to 31st December.

Data

      Date      Value
2018-04-30         NA
2018-05-31        102
2018-06-29         27
2018-07-01          3
2018-07-31         27
2018-09-04         52
2018-09-05          1
2018-10-01         78
2018-10-31        117
2018-11-30        245
2019-01-02        201
2019-01-31        256
2019-02-28        228
2019-04-01        155
2019-05-01        111
2019-05-31        105
2019-07-01         77
2019-07-31         61
2019-08-30         79
2019-10-01         76
2019-10-31        104
2019-12-03        196
2020-01-02        162
2020-01-31        292
2020-02-28        266
2020-03-26        145
2020-05-01        130
2020-06-03         86
2020-07-01         42

This is what I have tried so far and have access to the packages "ggplot2", "dplyr", "zoo" and "lubridate"

ggplot(a, aes(x = Date, y = Value)) +
  geom_point() +
  geom_line() +
  scale_x_date(date_minor_breaks = "1 month", date_labels = "%B %Y") +
  theme_bw()

在此处输入图像描述

Maybe this could help. You have to format the date first by year to make groups and colors. You could trick the axis adding a common year like 2020. Here the possible solution:

library(tidyverse)
#Data
df <- structure(list(Date = c("2018-04-30", "2018-05-31", "2018-06-29", 
"2018-07-01", "2018-07-31", "2018-09-04", "2018-09-05", "2018-10-01", 
"2018-10-31", "2018-11-30", "2019-01-02", "2019-01-31", "2019-02-28", 
"2019-04-01", "2019-05-01", "2019-05-31", "2019-07-01", "2019-07-31", 
"2019-08-30", "2019-10-01", "2019-10-31", "2019-12-03", "2020-01-02", 
"2020-01-31", "2020-02-28", "2020-03-26", "2020-05-01", "2020-06-03", 
"2020-07-01"), Value = c(NA, 102L, 27L, 3L, 27L, 52L, 1L, 78L, 
117L, 245L, 201L, 256L, 228L, 155L, 111L, 105L, 77L, 61L, 79L, 
76L, 104L, 196L, 162L, 292L, 266L, 145L, 130L, 86L, 42L)), row.names = c(NA, 
-29L), class = "data.frame")

Next code:

#Format dates
df$Date <- as.Date(df$Date)
#Create year
df$Year <- format(df$Date,'%Y')
#Create day and month of year
df$Day <- format(df$Date,'%d')
df$Month <- format(df$Date,'%m')
#Assign a dummy date
df$DayMonth <- as.Date(paste0(2020,'-',df$Month,'-',df$Day))
#Now sketch for plot
ggplot(df, aes(x = DayMonth, y = Value,group=Year,color=Year)) +
  geom_point() +
  geom_line() +
  scale_x_date(date_minor_breaks = "1 month", date_labels = "%B %d")+
  theme_bw()+
  theme(axis.text.x = element_text(angle=90))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM