c— title: “Week2 summary” author: “haha” date: “20 January 2018” output: html_document —

Function for season

#########season function
Season <- function(bikedata) {
  d<- as.Date(strptime(bikedata$Start.Date,format="%d/%m/%Y %H:%M",tz="UTC"))
  WS <- as.Date("21/12/2016", format = "%d/%m/%Y") # Winter 
  SE <- as.Date("20/3/2016",  format = "%d/%m/%Y") # Spring 
  SS <- as.Date("21/6/2016",  format = "%d/%m/%Y") # Summer 
  FE <- as.Date("22/9/2016",  format = "%d/%m/%Y") # Fall
  
  ifelse (d >= WS | d < SE, "Winter",
          ifelse (d >= SE & d < SS, "Spring",
                  ifelse (d >= SS & d < FE, "Summer", "Fall")))
}

Function to get data manipulated

seasonweekdata<- function(bikedata,coordinates=coor){
  bikedata<-bikedata %>% select("StartStation.Id","EndStation.Id","Start.Date")
  ###merge coordinate
  #########################################################################
  a<- coor
  colnames(a)<- c("StartStation.Id","s.lat","s.lon")
  x<-join(bikedata,a)
  aa<- coor
  colnames(aa)<- c("EndStation.Id","e.lat","e.lon")
  #a<-setnames(coor,old=c("id","lat","long"),new=c("EndStation.Id","e.lat","e.lon"))
  #aa<-setnames(coor,old=c("id","lat","long"),new=c("StartStation.Id","s.lat","s.lon"))
  z<-join(x,aa)
  head(z)
  z<- na.omit(z)
  x<- count(z,vars = c("StartStation.Id","EndStation.Id"))
  bikedata<- join(z,x)
  #################weekdays############################################################
  bikedata$weekdays<-weekdays(
    strptime(bikedata$Start.Date,format="%d/%m/%Y %H:%M",tz="UTC")
  )
  
  ########################################seasons
  bikedata$season<- Season(bikedata)
  head(bikedata)
  return(bikedata)
  ##########same start and end stations #########################################
  #attach(bikedata)
 # bikedata$samestation<- rep(0,nrow(bikedata))
  #head(bikedata)
  #bikedata$samestation[which((StartStation.Id==EndStation.Id))]<- bikedata$freq[which((StartStation.Id==EndStation.Id))]
}

Function for plots

fullplot<- function(data,type="nothing", source="google",maptype="roadmap",zoom = 11,
                    lowcol="grey90",highcol="black"){
  attach(data)
  
  map<- get_map(location = "London",source="google",maptype="roadmap",zoom = 11)
  
  plot<-ggmap(map)+
    geom_segment(data=data,aes(x=s.lon,y=s.lat,xend=e.lon, yend=e.lat,colour=freq
    ),size=0.00001,alpha=0.3)+
    xlim(range(data$s.lon))+
    ylim(range(data$s.lat))+
    scale_color_gradient(low=lowcol,high=highcol)+
    geom_point(data=data,aes(x=s.lon,y=s.lat),size=0.001,alpha=0.3)
  
  if (type== "weekdays"){
    plot<- plot+ facet_wrap(~ weekdays)
  }
  else if ( type == "season"){
    attach(data)
    plot<- plot+ facet_wrap(~ season)
  }
  else{
    plot <- plot
  }
  
  plot
  
}
fullplot(data=seasonweekdata(bikedata),lowcol = "grey90",highcol = "black")
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=London&zoom=11&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=London&sensor=false
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.

fullplot(data=seasonweekdata(bikedata),lowcol = "wheat",highcol = "darkblue",type = "weekdays")
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## The following objects are masked from data (pos = 3):
## 
##     e.lat, e.lon, EndStation.Id, freq, s.lat, s.lon, season,
##     Start.Date, StartStation.Id, weekdays
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=London&zoom=11&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=London&sensor=false
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.

fullplot(data=seasonweekdata(bikedata),lowcol = "wheat",highcol = "black",type = "season")
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## The following objects are masked from data (pos = 3):
## 
##     e.lat, e.lon, EndStation.Id, freq, s.lat, s.lon, season,
##     Start.Date, StartStation.Id, weekdays
## The following objects are masked from data (pos = 4):
## 
##     e.lat, e.lon, EndStation.Id, freq, s.lat, s.lon, season,
##     Start.Date, StartStation.Id, weekdays
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=London&zoom=11&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=London&sensor=false
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.
## The following objects are masked from data (pos = 3):
## 
##     e.lat, e.lon, EndStation.Id, freq, s.lat, s.lon, season,
##     Start.Date, StartStation.Id, weekdays
## The following objects are masked from data (pos = 4):
## 
##     e.lat, e.lon, EndStation.Id, freq, s.lat, s.lon, season,
##     Start.Date, StartStation.Id, weekdays
## The following objects are masked from data (pos = 5):
## 
##     e.lat, e.lon, EndStation.Id, freq, s.lat, s.lon, season,
##     Start.Date, StartStation.Id, weekdays