Skip to content

Commit

Permalink
function optimizing: clean and optimize bar chart plot
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid94 committed Dec 25, 2023
1 parent 4c2e700 commit d631560
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/main/R/tud_analysis_script.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,18 @@ plot_bar_chart_two_dimensional <- function(analyzed_data, main_title, x_label, y

plot_bar_chart <- function(analyzed_data, main_title, x_label, y_label, mode_col, output_filename) {

skip_naming = 0
if (names(analyzed_data)[1] %in% c("interval", "distance_class")) {
skip_naming = 1
}

if (!names(analyzed_data)[1] %in% c("interval", "distance_class")) {
names(analyzed_data)[1] <- "main_mode"

if (skip_naming != 1 )
# Renaming modes to official names
analyzed_data <- analyzed_data %>%
mutate(main_mode = case_when(
main_mode == "bike" ~ "Bicycle",
main_mode == "car" ~ "Car",
main_mode == "pt" ~ "Public transport",
main_mode == "ride" ~ "Car as passenger",
main_mode == "walk" ~ "Walking",
TRUE ~ main_mode
))

# Check if 'main_mode' is a column before renaming values
if ("main_mode" %in% names(analyzed_data)) {
analyzed_data$main_mode <- ifelse(analyzed_data$main_mode == "bike", "Bicycle",
ifelse(analyzed_data$main_mode == "car", "Car",
ifelse(analyzed_data$main_mode == "pt", "Public transport",
ifelse(analyzed_data$main_mode == "ride", "Car as passenger",
ifelse(analyzed_data$main_mode == "walk", "Walking", analyzed_data$main_mode)))))
}
}

long_analyzed_data <- analyzed_data %>% # ggplot works with long_data
gather(key = "Scenario", value = "Value", -mode_col)
Expand Down Expand Up @@ -708,7 +702,7 @@ walking_distance_distribution_by_mode <- function(trips_list, legs_list, output_

if(plot_creation == 1){
output_filename_pdf <- paste0(output_filename_prefix,".",mode)
plot_bar_chart(mode_data_wide, "Number of trips by walking distance interval for car mode", "Distance class", "Number of trips", "main_mode" , output_filename_pdf )
plot_bar_chart(mode_data_wide, "Number of trips by walking distance interval for car mode", "Distance class", "Number of trips", "interval" , output_filename_pdf )
}
}
}
Expand Down

0 comments on commit d631560

Please sign in to comment.