简体   繁体   English

从日期开始提取日期和月份

[英]Extract day and month from date

I'm trying to extract only the day and the month from as.POSIXct entries in a dataframe to overlay multiple years of data from the same months in a ggplot. 我试图仅从数据框中的as.POSIXct条目中提取日期和月份,以覆盖ggplot中相同月份的多年数据。

I have the data as time-series objects ts. 我有数据作为时间序列对象ts。

data.ts<-read.zoo(data, format = "%Y-%m-%d")
ts<-SMA(data.ts[,2], n=10)

df<-data.frame(date=as.POSIXct(time(ts)), value=ts)

ggplot(df, aes(x=date, y=value), 
           group=factor(year(date)), colour=factor(year(date))) +
  geom_line() +
  labs(x="Month", colour="Year") +
  theme_classic()

Now, obviously if I only use "date" in aes, it'll plot the normal time-series as a consecutive sequence across the years. 现在,显然如果我只在aes中使用“date”,它会将正常时间序列绘制为多年来的连续序列。 If I do "day(date)", it'll group by day on the x-axis. 如果我做“日(日期)”,它将在x轴上按天分组。 How do I pull out day AND month from the date? 如何从日期开始提取日期和月份? I only found yearmon(). 我只发现了一年()。 If I try as.Date(df$date, format="%d %m"), it's not doing anything and if I show the results of the command, it would still include the year. 如果我尝试as.Date(df $ date,format =“%d%m”),它没有做任何事情,如果我显示命令的结果,它仍然会包含年份。

data: 数据:

> data
         Date  V1
1  2017-02-04 113.26240
2  2017-02-05 113.89059
3  2017-02-06 114.82531
4  2017-02-07 115.63410
5  2017-02-08 113.68569
6  2017-02-09 115.72382
7  2017-02-10 114.48750
8  2017-02-11 114.32556
9  2017-02-12 113.77024
10 2017-02-13 113.17396
11 2017-02-14 111.96292
12 2017-02-15 113.20875
13 2017-02-16 115.79344
14 2017-02-17 114.51451
15 2017-02-18 113.83330
16 2017-02-19 114.13128
17 2017-02-20 113.43267
18 2017-02-21 115.85417
19 2017-02-22 114.13271
20 2017-02-23 113.65309
21 2017-02-24 115.69795
22 2017-02-25 115.37587
23 2017-02-26 114.64885
24 2017-02-27 115.05736
25 2017-02-28 116.25590

If I create a new column with only day and month 如果我创建一个只有日期和月份的新列

 df$day<-format(df$date, "%m/%d")

ggplot(df, aes(x=day, y=value), 
           group=factor(year(date)), colour=factor(year(date))) +
  geom_line() +
  labs(x="Month", colour="Year") +
  theme_classic()

I get such a graph for the two years. 这两年我得到了这样一张图。

ggplot中的两个时间序列

I want it to look like this, only with daily data instead of monthly. 我希望它看起来像这样,只使用每日数据而不是每月数据。 ggplot: Multiple years on same plot by month ggplot:按月计算多年

You are almost there. 你快到了。 As you want to overlay day and month based on every year, we need a continuous variable. 由于您希望根据每年的日期和月份进行重叠,因此我们需要一个连续变量。 "Day of the year" does the trick for you. “一年中的某一天”为您提供了诀窍。

data <-data.frame(Date=c(Sys.Date()-7,Sys.Date()-372,Sys.Date()-6,Sys.Date()-371,
                         Sys.Date()-5,Sys.Date()-370,Sys.Date()-4,Sys.Date()-369,
                         Sys.Date()-3,Sys.Date()-368),V1=c(113.23,123.23,121.44,111.98,113.5,114.57,113.44, 121.23, 122.23, 110.33))

data$year = format(as.Date(data$Date), "%Y")
data$Date = as.numeric(format(as.Date(data$Date), "%j"))

ggplot(data=data, mapping=aes(x=Date, y=V1, shape = year, color = year)) + geom_point() + geom_line() 
  theme_bw()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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