2

I am new to R. I have written this code to generate with highcharter library, This is based on the dataframe that I have for 11 years i.e. 2005 - 2011 (for months April - October)

The following code is for one year. I want to create a loop or something similar to create the chart for every year separately. This code works fine but I have to manually change the date for every year and generate a chart.

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
Parfait
  • 104,375
  • 17
  • 94
  • 125
equastrian91
  • 53
  • 1
  • 1
  • 4
  • Can you share your data using `dput()`? See more here https://meta.stackoverflow.com/questions/315885/what-is-the-correct-way-to-share-r-data-on-stackoverflow – Tung Mar 02 '18 at 14:46
  • I think you can try to wrap it into a function. Then you can call the function to plot data. – Wenlong Liu Mar 02 '18 at 15:27
  • If there are some sample data, I can help you work on this function. – Wenlong Liu Mar 02 '18 at 15:30
  • 1
    @WenlongLiu If you could help me with the function that would be nice here is the link for data https://drive.google.com/open?id=1I76ASCvu7V7Iv-MjeVAAXONnyJqCyOWi – equastrian91 Mar 02 '18 at 15:55
  • @Tung I want to share the data i used in this example and it is too big to be shared be dput() i have added a google drive link for data – equastrian91 Mar 02 '18 at 15:58
  • To add a little more about the data, i am using three data frames, splitting them by date and combining them again for hc , data names are (Seven,Seven_flow,Seven_inflow) – equastrian91 Mar 02 '18 at 16:08
  • @whj: please add the link to your data to the question as well – Tung Mar 02 '18 at 17:16

2 Answers2

1

These codes work in my computer. Please put the data files in the same folder of the R scripts.

# 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)}
Wenlong Liu
  • 444
  • 2
  • 13
  • @whj, not related to data visualization, but your data looks weird. Sometime the discharge does not respond to the heavy rainfall in your area. – Wenlong Liu Mar 02 '18 at 17:07
  • I think the flow is regulated through a dam. – Tung Mar 02 '18 at 17:20
  • @WenlongLiu Thank you, the code worked fine for me, no dam in the area but, there is some distance between the discharge and rainfall stations and the area between the rainfall and discharge station is also frequently flooded. This might be the reason – equastrian91 Mar 02 '18 at 21:34
  • @WenlongLiu could you please look at my other question also? https://stackoverflow.com/questions/49078775/adding-a-timeline-style-graphic-using-r-highcharter – equastrian91 Mar 03 '18 at 02:02
0

Simply generalize your processing into a defined function where you pass year integers as a parameter. All that is needed is dynamically concatenating yr parameters with paste0() into as.Date calls, and removing any _05 suffixes to avoid confusion:

Function

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)
})

Iteration

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

# SAVING OUTPUTS TO LIST
output_list <- lapply(2005:2011, build_graph)
Parfait
  • 104,375
  • 17
  • 94
  • 125