【问题标题】:Creating a loop over time series rainfall data在时间序列降雨数据上创建循环
【发布时间】:2018-08-10 17:15:21
【问题描述】:

我是 R 新手。我已编写此代码以使用 highcharter 库生成, 这是基于我拥有 11 年的数据框,即 2005 年 - 2011 年(4 月 - 10 月)

以下代码适用于一年。我想创建一个循环或类似的东西来分别为每年创建图表。此代码工作正常,但我必须手动更改每年的日期并生成图表。

Year_2005_rain <- subset(Seven, 
                         time >= as.Date('2005-04-01') & 
                         time <= as.Date('2005-10-31'))
Year_2005_flow<- subset(Seven_flow, 
                        time >= as.Date('2005-04-01') & 
                        time <= as.Date('2005-10-31'))
Year_2005_inflow<- subset(Seven_inflow, 
                          time >= as.Date('2005-04-01') & 
                          time <= as.Date('2005-10-31'))

merge1_05 <- merge(Year_2005_rain,
                   Year_2005_flow,
                   Year_2005_inflow, by="time")

names(Year_2005_rain) <- names(Year_2005_flow) <- names(Year_2005_inflow)
merge1_05 <- rbind(Year_2005_rain, Year_2005_flow,Year_2005_inflow)

colnames(merge1_05)[colnames(merge1_05)=="time"] <- "date"
colnames(merge1_05)[colnames(merge1_05)=="Discharge"] <- "value"

merge1_05$date = as.Date(merge1_05$date, format = "%Y/%m/%d")    
merge1_05$variable <- c(rep("rain", 214), rep("discharge", 214), rep("inflow", 214))

hc_14<- highchart() %>% 
  hc_yAxis_multiples(list(title = list(text = "rainfall depth (mm)"), reversed = TRUE), 
                     list(title = list(text = "flow (m3/s)"), opposite = TRUE)) %>% 
  hc_add_series(data = filter(merge1_05, variable == "rain") %>% 
                mutate(value = value) %>% .$value, type = "column") %>% 
  hc_add_series(data = filter(merge1_05, variable == "discharge") %>% .$value,   
                type = "spline", yAxis = 1) %>%
  hc_add_series(data = filter(merge1_05, variable == "inflow") %>% .$value,  
                type = "spline", yAxis = 1) %>%
  hc_xAxis(categories = merge1_05$date, title = list(text = "date"))

hc_14

【问题讨论】:

  • 您可以使用dput() 分享您的数据吗?在此处查看更多信息meta.stackoverflow.com/questions/315885/…
  • 我想你可以试着把它包装成一个函数。然后你可以调用函数来绘制数据。
  • 如果有样本数据,我可以帮你做这个功能。
  • @WenlongLiu 如果你能帮我实现一个很好的功能,这里是数据链接drive.google.com/open?id=1I76ASCvu7V7Iv-MjeVAAXONnyJqCyOWi
  • @Tung 我想分享我在这个例子中使用的数据,因为 dput() 太大而无法分享,我已经为数据添加了一个谷歌驱动器链接

标签: r loops time-series timeserieschart r-highcharter


【解决方案1】:

这些代码在我的电脑上运行。请将数据文件放在R脚本的同一文件夹中。

# import data and library
library(ggplot2)
library(dplyr)
library(highcharter)

Seven_flow = read.csv("Seven_flow.csv")
Seven_inflow = read.csv("Seven_inflow.csv")
Seven = read.csv("Seven.csv")

# cleanning data.
# put all the data into one dataframe. 
# add a column year as the iter in for loop.
hydrograph = Seven_flow
names(hydrograph) = c("X","date", "discharge")
hydrograph$inflow = Seven_inflow$value
hydrograph$rain = Seven$value
hydrograph$date = as.Date(hydrograph$date, format = "%Y-%m-%d")
hydrograph$year = format(hydrograph$date, "%Y")
summary(hydrograph)

# plot the data in for loop.

for (year.plot in seq(2005,2011,1)){
  # filter the year of interest.
  hydrograph.plot = filter(hydrograph, year==year.plot)

hc_14<- highchart() %>% 
hc_yAxis_multiples(list(title = list(text = "rainfall depth (mm)"), reversed   
= TRUE), list(title = list(text = "flow (m3/s)"), opposite = TRUE)) %>% 
hc_add_series(data = hydrograph.plot$rain , type = "column") %>% 
hc_add_series(data = hydrograph.plot$discharge, type = "spline", yAxis = 1) %>%
hc_add_series(data = hydrograph.plot$inflow,  type = "spline", yAxis = 1) %>%
hc_xAxis(categories = hydrograph.plot$date, title = list(text = "date"))

print(hc_14)}

【讨论】:

  • @whj,与数据可视化无关,但你的数据看起来很奇怪。有时排放对您所在地区的强降雨没有反应。
  • 我认为流量是通过大坝调节的。
  • @WenlongLiu 谢谢,代码对我来说很好,该地区没有大坝,但是排放和降雨站之间有一段距离,降雨和排放站之间的区域也经常被淹.这可能是原因
  • @WenlongLiu 你能看看我的另一个问题吗? stackoverflow.com/questions/49078775/…
【解决方案2】:

简单地将您的处理概括为一个定义的函数,您可以在其中将年份整数作为参数传递。所需要的只是将带有paste0()yr 参数动态连接到as.Date 调用中,并删除所有_05 后缀以避免混淆:

功能

build_graph <- function(yr) {    
    Year_rain <- subset(Seven, 
                        time >= as.Date(paste0(yr,'-04-01')) & 
                        time <= as.Date(paste0(yr,'-10-31')))
    Year_flow<- subset(Seven_flow, 
                       time >= as.Date(paste0(yr,'-04-01')) & 
                       time <= as.Date(paste0(yr,'-10-31')))
    Year_inflow<- subset(Seven_inflow, 
                         time >= as.Date(paste0(yr,'-04-01')) & 
                         time <= as.Date(paste0(yr,'-10-31')))

    merge1 <- merge(Year_rain, Year_flow, Year_inflow, by="time")    
    names(Year_rain) <- names(Year_flow) <- names(Year_inflow)
    merge1 <- rbind(Year_rain, Year_flow,Year_inflow)

    colnames(merge1)[colnames(merge1)=="time"] <- "date"
    colnames(merge1)[colnames(merge1)=="Discharge"] <- "value"

    merge1$date <- as.Date(merge1$date, format = "%Y/%m/%d")    
    merge1$variable <- c(rep("rain", 214), rep("discharge", 214), rep("inflow", 214))

    hc_14 <- highchart() %>% 
      hc_yAxis_multiples(list(title = list(text = "rainfall depth (mm)"), reversed=TRUE),
                         list(title = list(text = "flow (m3/s)"), opposite = TRUE)) %>% 
      hc_add_series(data = filter(merge1, variable == "rain") %>% 
                      mutate(value = value) %>% .$value, type = "column") %>% 
      hc_add_series(data = filter(merge1, variable == "discharge") %>% .$value, 
                    type = "spline", yAxis = 1) %>%
      hc_add_series(data = filter(merge1, variable == "inflow") %>% .$value,  
                    type = "spline", yAxis = 1) %>%
      hc_xAxis(categories = merge1$date, title = list(text = "date"))

    return(hc_14)
})

迭代

# OUTPUT TO CONSOLE (NO SAVING)
for (i in 2005:2011) {
   build_graph(i)
}

# SAVING OUTPUTS TO LIST
output_list <- lapply(2005:2011, build_graph)

【讨论】:

  • 谢谢,我根据您的建议精简了我的其他一些代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
  • 2021-12-21
  • 2019-02-21
  • 1970-01-01
  • 2017-02-24
  • 2015-04-24
  • 1970-01-01
相关资源
最近更新 更多