Route and Direction based Analysis

Line Plot

All Route_Direc

# Load required libraries
library(ggplot2)
library(dplyr)
library(wesanderson)


setwd("C:/Users/mvx13/OneDrive - Texas State University/Projects/2023/NJDOT Paint Marking/0000_Final/Analysis/older")
df= read.csv("MasterData_V03_un01_lim.csv")
dim(df)
## [1] 17138     4
head(df)
##   RoutewithDir   Phase Driven  RL
## 1       GSP_NB Phase 1    0.0 265
## 2       GSP_NB Phase 1    0.1 272
## 3       GSP_NB Phase 1    0.2 262
## 4       GSP_NB Phase 1    0.3 221
## 5       GSP_NB Phase 1    0.4 259
## 6       GSP_NB Phase 1    0.5 258
names(df)
## [1] "RoutewithDir" "Phase"        "Driven"       "RL"
table(df$RoutewithDir)
## 
## GSP Bridge_NB GSP Bridge_SB        GSP_NB        GSP_SB       I 80_EB 
##          1021          1082           415           423          1049 
##       I 80_WB      SH 15_NB      SH 15_SB      SH 33_EB      SH 33_WB 
##          1099           665           596           499           552 
##      SH 37_EB      SH 37_WB      SH 50_EB      SH 50_WB      SH 53_NB 
##           514           555           316           326           664 
##      SH 53_SB      SH 54_EB      SH 54_WB      SH 72_EB      SH 72_WB 
##           622           258           260           280           291 
##      SH 73_NB      SH 73_SB     US 130_NB     US 130_SB     US 206_NB 
##           444           453           650           705           806 
##     US 206_SB     US 322_EB     US 322_WB      US 40_EB      US 40_WB 
##           898           384           420           459           432
# Set custom colors
my_colors <- wes_palette("FantasticFox1", n = 3, type = "discrete")

# Filter for RL < 800
df_filtered <- subset(df, RL < 800)

# Enhanced plot
ggplot(subset(df, RL < 800), aes(x = Driven, y = RL, color = Phase, group = Phase)) +
  geom_line(size = 1, alpha = 0.8) +                      # Thicker line
  geom_point(size = 1.5, alpha = 0.05, shape = 16) +           # Small solid points
  facet_wrap(~ RoutewithDir, scales = "free_x") +
  scale_color_manual(values = my_colors) +
  labs(x = "Driven Distance (miles)",
       y = "RL Value",
       color = "Phase") +
  theme_minimal(base_size = 14) +
  theme(
    legend.position = "bottom",
    strip.text = element_text(face = "bold", size = 14),
    plot.title = element_text(face = "bold", size = 16)
  )

By Route_Direc

# Get unique RoutewithDir values
routes <- unique(df_filtered$RoutewithDir)

# Loop over each route and plot
for (route in routes) {
  df_route <- df_filtered %>% filter(RoutewithDir == route)
  
  p <- ggplot(df_route, aes(x = Driven, y = RL, color = Phase, group = Phase)) +
    geom_line(size = 2.2, alpha = 0.9) +                    # Thick lines
    geom_point(size = 1.5, alpha = 1, shape = 16) +         # Solid small points
    scale_color_manual(values = my_colors) +
    labs(
      title = paste("RL Variation by Phase -", route),
      x = "Driven Distance (miles)",
      y = "RL Value",
      color = "Phase"
    ) +
    theme_minimal(base_size = 14) +
    theme(
      legend.position = "bottom",
      plot.title = element_text(face = "bold", size = 16)
    )
  
  print(p)  # Display the plot (use ggsave() to save)
  
  # Optional: Save plot as PNG
  # ggsave(filename = paste0("RL_plot_", route, ".png"), plot = p, width = 8, height = 5)
}

Density Plot

All Route_Direc

# Keep plausible RL values
df_filtered <- df %>%
  filter(RL < 800) %>%                       # Remove extreme RLs
  mutate(
    Phase = factor(Phase, levels = c("Phase 1", "Phase 2", "Phase 3"))
  )

# Distinctive palette (3 colours)
my_colors <- wes_palette("FantasticFox1", n = 3, type = "discrete")

# ---------- 1. Faceted density plot for all routes ----------
ggplot(df_filtered, aes(x = RL, fill = Phase, colour = Phase)) +
  geom_density(alpha = 0.35, linewidth = 1) +
  facet_wrap(~ RoutewithDir, scales = "free_y") +
  scale_fill_manual(values = my_colors) +
  scale_colour_manual(values = my_colors) +
  labs(
    title  = "RL Distribution by Phase (All Routes)",
    x      = "RL Value",
    y      = "Density",
    fill   = "Phase",
    colour = "Phase"
  ) +
  theme_minimal(base_size = 14) +
  theme(
    legend.position = "bottom",
    strip.text      = element_text(face = "bold", size = 13)
  )

By Route_Direc

# ---------- 2. One density plot per RoutewithDir ----------
routes <- unique(df_filtered$RoutewithDir)

for (route in routes) {
  df_route <- df_filtered %>% filter(RoutewithDir == route)
  
  p <- ggplot(df_route, aes(x = RL, fill = Phase, colour = Phase)) +
    geom_density(alpha = 0.35, linewidth = 1) +
    scale_fill_manual(values = my_colors) +
    scale_colour_manual(values = my_colors) +
    labs(
      title  = paste("RL Distribution by Phase –", route),
      x      = "RL Value",
      y      = "Density",
      fill   = "Phase",
      colour = "Phase"
    ) +
    theme_minimal(base_size = 14) +
    theme(
      legend.position = "bottom",
      plot.title      = element_text(face = "bold", size = 16)
    )
  
  print(p)    # Show in plot window
  
  # Optional: save each plot
  # ggsave(filename = paste0("RL_density_", route, ".png"),
  #        plot = p, width = 8, height = 5, dpi = 300)
}