Function for season

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

Function to get data manipulated

seasonweekdata<- function(data1,coordinates=coor){
  data1<- dplyr:: select(data1,"StartStation.Id","EndStation.Id","Start.Date",
                          "StartStation.Name","EndStation.Name")
  
  ###merge coordinate
  #########################################################################
  a<- coor
  colnames(a)<- c("StartStation.Id","s.lat","s.lon")
  x<-join(data1,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)
  z<- na.omit(z)
  x<- count(z,vars = c("StartStation.Id","EndStation.Id"))
  data1<- join(z,x)
  #################weekdays############################################################
  data1$weekdays<-weekdays(
    strptime(data1$Start.Date,format="%d/%m/%Y %H:%M",tz="UTC")
  )
  data1$season<-Season(data2=data1)
  
  ########################################seasons
  return(data1)
  ##########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))]
  str(data1)
  
}

Function for plots

Summer

sfullplot<- function(data5){

  
  q<- seasonweekdata(data1 = bikedata)
  Summer<- q[q$season=="Summer",]

    ######################################winter
  plot<-ggmap(map)+
    geom_segment(data=Summer,aes(x=s.lon,y=s.lat,xend=e.lon, yend=e.lat,alpha=freq),
                 colour= "black",size=0.00001)+
    scale_alpha_continuous(range = c(0, 0.01))+
    ggtitle("2017 Summer bicycle trips")+
    theme(plot.title = element_text(hjust = 0.5))+
    xlim(range(q$s.lon))+
    ylim(range(q$s.lat))+
    geom_point(data=Winter,aes(x=s.lon,y=s.lat),size=0.001,alpha=0.3)+
    geom_label_repel(data=data5, aes(x=s.lon,y=s.lat,label=StartStation.Name),
                    color="blue",nudge_y = 0.03,nudge_x = -0.1,label.size = 0.05,
                    size=3,alpha=0.4
                    )+
                #    arrow =arrow(length =unit(0.5,"cm")))+
    geom_label_repel(data=data5, aes(x=e.lon,y=e.lat,label=EndStation.Name),
                    color="red",segment.size = 0.5,segment.colour = "red",
                    #arrow = arrow(length = unit(0.5,"cm")),
                  nudge_y = -0.03,nudge_x = 0.15,size=3,alpha=0.4)

  ############################################################summer
  
  return(plot)

  
}

Autumn

Afullplot<- function(data5){

  
  q<- seasonweekdata(data1 = bikedata)
  Autumn<- q[q$season=="Autumn",]

    ######################################winter
  plot<-ggmap(map)+
    geom_segment(data=Autumn,aes(x=s.lon,y=s.lat,xend=e.lon, yend=e.lat,alpha=freq),
                 colour= "black",size=0.00001)+
    scale_alpha_continuous(range = c(0, 0.01))+
    ggtitle("2017 Autumn bicycle trips")+
    theme(plot.title = element_text(hjust = 0.5))+
    xlim(range(q$s.lon))+
    ylim(range(q$s.lat))+
    geom_point(data=Winter,aes(x=s.lon,y=s.lat),size=0.001,alpha=0.3)+
    geom_label_repel(data=data5, aes(x=s.lon,y=s.lat,label=StartStation.Name),
                    color="blue",nudge_y = 0.03,nudge_x = -0.1,label.size = 0.05,
                    size=3,alpha=0.4
                    )+
                #    arrow =arrow(length =unit(0.5,"cm")))+
    geom_label_repel(data=data5, aes(x=e.lon,y=e.lat,label=EndStation.Name),
                    color="red",segment.size = 0.5,segment.colour = "red",
                    #arrow = arrow(length = unit(0.5,"cm")),
                  nudge_y = -0.03,nudge_x = 0.15,size=3,alpha=0.4)

  ############################################################summer
  
  return(plot)

  
}

Spring

Spfullplot<- function(data5){

  
  q<- seasonweekdata(data1 = bikedata)
  Spring<- q[q$season=="Spring",]

    ######################################winter
  plot<-ggmap(map)+
    geom_segment(data=Spring,aes(x=s.lon,y=s.lat,xend=e.lon, yend=e.lat,alpha=freq),
                 colour= "black",size=0.00001)+
    scale_alpha_continuous(range = c(0, 0.01))+
    ggtitle("2017 Spring bicycle trips")+
    theme(plot.title = element_text(hjust = 0.5))+
    xlim(range(q$s.lon))+
    ylim(range(q$s.lat))+
    geom_point(data=Winter,aes(x=s.lon,y=s.lat),size=0.001,alpha=0.3)+
    geom_label_repel(data=data5, aes(x=s.lon,y=s.lat,label=StartStation.Name),
                    color="blue",nudge_y = 0.03,nudge_x = -0.1,label.size = 0.05,
                    size=3,alpha=0.4
                    )+
                #    arrow =arrow(length =unit(0.5,"cm")))+
    geom_label_repel(data=data5, aes(x=e.lon,y=e.lat,label=EndStation.Name),
                    color="red",segment.size = 0.5,segment.colour = "red",
                    #arrow = arrow(length = unit(0.5,"cm")),
                  nudge_y = -0.03,nudge_x = 0.15,size=3,alpha=0.4)

  ############################################################summer
  
  return(plot)

  
}

Work out the leading stations

q<- seasonweekdata(data1 = bikedata)
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
Winter<- q[q$season=="Winter",]
Spring<- q[q$season=="Spring",]
Summer<- q[q$season=="Summer",]
Autumn<- q[q$season=="Autumn",]

topplace<- function(data4=Winter){
  top<- dplyr::select(data4,"s.lat","s.lon","StartStation.Name","EndStation.Name",
                      "freq",
               "e.lat","e.lon")
  top<-top[!duplicated(top),]
  top<-top_n(top, 5, freq)
  top<-arrange(top,desc(freq))
  return(top)

}
map<- get_map(location = c(lon =mean(q$s.lon),lat = mean(q$s.lat)),
                 source="google",maptype="roadmap",zoom=11)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=51.508541,-0.127694&zoom=11&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
Afullplot(data5= topplace(data4 = Autumn))
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## 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.

Spfullplot(data5=topplace(data4 = Spring))
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## 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.

sfullplot(data5=topplace(data4 = Summer))
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## 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.
## Warning: Removed 1 rows containing missing values (geom_rect).