R 如何使用ggplot2计算两列的每月平均日变化
我有一个两列的时间序列数据,我想要一个每月平均每小时模式的图表,就像所附的图表一样,但有两个时间序列R 如何使用ggplot2计算两列的每月平均日变化,r,ggplot2,time-series,multiple-columns,mean,R,Ggplot2,Time Series,Multiple Columns,Mean,我有一个两列的时间序列数据,我想要一个每月平均每小时模式的图表,就像所附的图表一样,但有两个时间序列 timestamp ET_control ET_treatment 1 2016-01-01 00:00:00 NA NA 2 2016-01-01 00:30:00 NA NA 3 2016-01-01 01:00:00 NA NA 4
timestamp ET_control ET_treatment
1 2016-01-01 00:00:00 NA NA
2 2016-01-01 00:30:00 NA NA
3 2016-01-01 01:00:00 NA NA
4 2016-01-01 01:30:00 NA NA
5 2016-01-01 02:00:00 NA NA
6 2016-01-01 02:30:00 NA NA
7 2016-01-01 03:00:00 NA NA
8 2016-01-01 03:30:00 NA NA
9 2016-01-01 04:00:00 NA NA
10 2016-01-01 04:30:00 NA NA
11 2016-01-01 05:00:00 NA NA
12 2016-01-01 05:30:00 NA NA
13 2016-01-01 06:00:00 NA NA
14 2016-01-01 06:30:00 NA NA
15 2016-01-01 07:00:00 NA NA
16 2016-01-01 07:30:00 NA NA
17 2016-01-01 08:00:00 NA NA
18 2016-01-01 08:30:00 NA NA
19 2016-01-01 09:00:00 NA NA
20 2016-01-01 09:30:00 NA NA
21 2016-01-01 10:00:00 NA NA
22 2016-01-01 10:30:00 NA NA
23 2016-01-01 11:00:00 NA NA
24 2016-01-01 11:30:00 0.09863437 NA
25 2016-01-01 12:00:00 0.11465258 NA
26 2016-01-01 12:30:00 0.12356855 NA
27 2016-01-01 13:00:00 0.09246215 0.085398782
28 2016-01-01 13:30:00 0.08843156 0.072877001
29 2016-01-01 14:00:00 0.08536019 0.081885947
30 2016-01-01 14:30:00 0.08558541 NA
31 2016-01-01 15:00:00 0.05571436 NA
32 2016-01-01 15:30:00 0.04087248 0.038582547
33 2016-01-01 16:00:00 0.04233724 NA
34 2016-01-01 16:30:00 0.02150660 0.019560578
35 2016-01-01 17:00:00 0.01803765 0.019691155
36 2016-01-01 17:30:00 NA 0.005190489
37 2016-01-01 18:00:00 NA NA
38 2016-01-01 18:30:00 NA NA
39 2016-01-01 19:00:00 NA NA
40 2016-01-01 19:30:00 NA NA
41 2016-01-01 20:00:00 NA NA
42 2016-01-01 20:30:00 NA NA
43 2016-01-01 21:00:00 NA NA
44 2016-01-01 21:30:00 NA NA
45 2016-01-01 22:00:00 NA NA
46 2016-01-01 22:30:00 NA NA
47 2016-01-01 23:00:00 NA NA
48 2016-01-01 23:30:00 NA NA
49 2016-01-02 00:00:00 NA NA
50 2016-01-02 00:30:00 NA NA
给定t是您的data.frame,包含dplyr和ggplot2包:
t <- t %>% mutate(
month = format(strptime(timestamp, "%Y-%m-%d %H:%M:%S"), "%b"),
hour=format(strptime(timestamp, "%Y-%m-%d %H:%M:%S"), "%H"))
tm <- t %>% group_by(month, hour) %>%
summarize(ET_control_mean=mean(ET_control, na.rm=T))
ggplot(tm, aes(x=hour, y=ET_control_mean)) + geom_point() + facet_wrap(~ month)
t%变异(
月份=格式(strtime(时间戳,%Y-%m-%d%H:%m:%S”),%b”),
小时=格式(strtime(时间戳,%Y-%m-%d%H:%m:%S”),%H)
tm%分组依据(月、小时)%>%
总结(ET_控制组平均值=平均值(ET_控制组,na.rm=T))
ggplot(tm,aes(x=小时,y=控制平均值))+geom_point()+facet_wrap(~月)
如果您想在图形中同时显示这两列,则应将数据转换为“long”格式。非常感谢,这段代码为我节省了很多时间。(第二行rm.na的末尾有一个小的打字错误,应该是na.rm)