Downloading GTFS Data:

Accessing the GTFS Data: First, I accessed the GTFS data provided by the STM (Société de transport de Montréal) for analysis. This data contains information about public transit routes, schedules, stops, and more.

Locating the GTFS Data: The GTFS data was downloaded from a specified location. In this case, the data was stored in a folder named gtfs_stm located at C:/Users/deyx8340/Downloads.

Now Data Analysis Steps:

Loading Necessary Packages: I began by loading the necessary R packages, particularly the tidyverse package, which includes tools for data manipulation and visualization.

Loading GTFS Data: Using the read_csv function from the tidyverse, I loaded various GTFS data files such as stops.txt, routes.txt, trips.txt, stop_times.txt, calendar.txt, calendar_dates.txt, and shapes.txt into R. These files contain information about stops, routes, trips, schedules, and more.

Visualizing Transit Service Frequency:

Using the ggplot2 package, I visualized transit service frequency for a specific route. This involved plotting the number of trips per hour against the hour of the day using a line plot (geom_line) and point markers (geom_point). The x-axis represents the hour of the day, while the y-axis represents the number of trips per hour.

library(dplyr)
library(lubridate)



stop_times_with_route_id <- stop_times %>%
  inner_join(trips, by = "trip_id")  


trip_frequency <- stop_times_with_route_id %>%
  mutate(hour = hour(arrival_time)) %>%
  group_by(route_id, hour) %>%
  summarise(trips_per_hour = n(), .groups = "drop")


popular_route_freq <- filter(trip_frequency, route_id == 1)


ggplot(popular_route_freq, aes(x = hour, y = trips_per_hour)) +
  geom_line(group = 1, color = 'darkred', size = 1) +
  geom_point(color = 'red', size = 2, alpha = 0.6) +
  theme_minimal() +
  labs(title = "Transit Service Frequency for Route 1",
       x = "Hour of the Day",
       y = "Trips per hour",
       caption = "Data source: STM GTFS") +
  scale_x_continuous(breaks = 0:24, limits = c(0, 24)) 
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#Now for ID 439
ggplot(popular_route_freq, aes(x = hour, y = trips_per_hour)) +
  geom_line(group = 1, color = 'darkred', size = 1) +
  geom_point(color = 'red', size = 2, alpha = 0.6) +
  theme_minimal() +
  labs(title = "Transit Service Frequency for Route 439",
       x = "Hour of the Day",
       y = "Trips per hour",
       caption = "Data source: STM GTFS") +
  scale_x_continuous(breaks = 0:24, limits = c(0, 24)) 

Visualizing Total Services by Day of the Week:

I created a bar plot to visualize the total number of transit services provided by day of the week. This involved aggregating service counts across all routes for each day of the week and plotting them using ggplot2.

Visualizing Stop Frequency on a Map:

Utilizing the leaflet package, I created a map to visualize stop frequency. This involved plotting stop locations as circle markers on an interactive map. The size of each marker corresponds to the frequency of service at that stop. Additionally, each marker includes a popup displaying the name of the stop along with its frequency.

This code generates an interactive map using the leaflet package in R. It adds circular markers to display stop locations, with each marker showing the stop name as a popup. The map includes base tiles from OpenStreetMap and allows users to toggle the visibility of stops using the layer control. The map’s initial view is set to a specific center and zoom level. Before plotting, the stop data is converted to spatial features using the st_as_sf function.

## Installing package into 'C:/Users/deyx8340/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'sf' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'sf'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\deyx8340\AppData\Local\R\win-library\4.3\00LOCK\sf\libs\x64\sf.dll to
## C:\Users\deyx8340\AppData\Local\R\win-library\4.3\sf\libs\x64\sf.dll:
## Permission denied
## Warning: restored 'sf'
## 
## The downloaded binary packages are in
##  C:\Users\deyx8340\AppData\Local\Temp\RtmpKstgaK\downloaded_packages
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE

Visualizing Trip Counts by Route:

Finally, I visualized trip counts by route using a bar plot. This involved counting the number of trips for each route, sorting them in descending order, and plotting the top N routes using ggplot2. I also adjusted the x-axis labels to rotate them vertically for better readability.

Following the data exploration, I proceeded to visualize various aspects of transit service using ggplot2 and leaflet packages. Firstly, I visualized transit service frequency for a specific route, plotting the number of trips per hour against the hour of the day.

Additionally, I visualized the total number of transit services provided by day of the week using a bar plot, aggregating service counts across all routes.

Furthermore, I utilized the leaflet package to create an interactive map visualizing stop frequency. This involved mapping stop locations as circle markers, where the size of each marker represents the frequency of service at that stop. Each marker also includes a popup displaying the name of the stop along with its frequency.

Lastly, I visualized trip counts by route using a bar plot. I sorted the routes by the number of trips and plotted the top N routes to identify the most popular ones. I also adjusted the x-axis labels to improve readability.

Overall, this analysis provides valuable insights into transit service frequency and trip counts, which can inform decision-making processes and improvements in public transportation systems.