From 999d771f85fb902155334fca6bf47ced2a9f8201 Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Wed, 6 Mar 2024 16:07:27 +0100 Subject: [PATCH 1/4] add R scripts to analyze food --- src/main/R/plotFoodResults.R | 50 +++++++++++ src/main/R/plotFoodResults_comparison.R | 115 ++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 src/main/R/plotFoodResults.R create mode 100644 src/main/R/plotFoodResults_comparison.R diff --git a/src/main/R/plotFoodResults.R b/src/main/R/plotFoodResults.R new file mode 100644 index 0000000..15e571f --- /dev/null +++ b/src/main/R/plotFoodResults.R @@ -0,0 +1,50 @@ +setwd("C:/Users/Ricardo/git/shared-svn/projects/freight/studies/WP51_EmissionsFood/output/BaseCase_Food2024/Analysis") + +# Install and load necessary packages +if (!requireNamespace("tidyverse", quietly = TRUE)) { + install.packages("tidyverse") +} +if (!requireNamespace("plotly", quietly = TRUE)) { + install.packages("plotly") +} +if (!requireNamespace("gridExtra", quietly = TRUE)) { + install.packages("gridExtra") +} +library(tidyverse) +library(plotly) +library(gridExtra) + +# Read the TSV file +df <- read.delim("TimeDistance_perVehicleType.tsv") +df_tours <- read.delim("TimeDistance_perVehicle.tsv") + +# Specify the desired order of vehicleTypeId +desired_order <- c("light8t", "light8t_frozen","medium18t", "medium18t_frozen","heavy26t", "heavy26t_frozen","heavy40t", "heavy40t_frozen") + +# Convert 'vehicleTypeId' to a factor with the desired order +df$vehicleTypeId <- factor(df$vehicleTypeId, levels = desired_order) +df_tours$vehicleTypeId <- factor(df_tours$vehicleTypeId, levels = desired_order) + +# Ensure both plots have the same levels +common_levels <- intersect(levels(df$vehicleTypeId), levels(df$vehicleTypeId)) + +# Filter the data frame to include only common levels +df <- df %>% filter(vehicleTypeId %in% common_levels) + +# 1. Bar Plot for Number of Vehicles by Vehicle Type (Interactive) +bar_plot <- plot_ly(x = ~df$vehicleTypeId, y = df$nuOfVehicles, type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Number of Vehicles')) + +# 3. Bar Plot for Total Costs by Vehicle Type (Interactive) +bar_plot_costs <- plot_ly(x = df$vehicleTypeId, y = df$totalCosts.EUR., type = 'bar', name = 'Total Costs') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Total Costs (EUR)')) + +# 4. Box Plot for Traveled Distances by Vehicle Type (Interactive) +box_plot_distances <- plot_ly(data = df_tours, x = ~vehicleTypeId, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) + +# Display the plots separately +print(bar_plot %>% layout(title = 'Number of Vehicles by Vehicle Type')) +print(bar_plot_costs %>% layout(title = 'Total Costs by Vehicle Type')) +print(box_plot_distances %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type')) diff --git a/src/main/R/plotFoodResults_comparison.R b/src/main/R/plotFoodResults_comparison.R new file mode 100644 index 0000000..8f63e41 --- /dev/null +++ b/src/main/R/plotFoodResults_comparison.R @@ -0,0 +1,115 @@ +# Set working directory and load necessary packages (assuming it hasn't changed) +setwd("C:/Users/Ricardo/git/shared-svn/projects/freight/studies/WP51_EmissionsFood/output") + +if (!requireNamespace("tidyverse", quietly = TRUE)) { + install.packages("tidyverse") +} +if (!requireNamespace("plotly", quietly = TRUE)) { + install.packages("plotly") +} +if (!requireNamespace("gridExtra", quietly = TRUE)) { + install.packages("gridExtra") +} +library(tidyverse) +library(plotly) +library(gridExtra) + +# Function to create a bar plot for vehicles +create_bar_plot_vehicles <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$nuOfVehicles, name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$nuOfVehicles, name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Number of Vehicles')) %>% + layout(title = title) +} + +# Function to create a bar plot for costs +create_bar_plot_costs <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$totalCosts.EUR., name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$totalCosts.EUR., name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Total Costs (EUR)')) %>% + layout(title = title) +} + +# Function to create a bar plot for distances +create_bar_plot_distances <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$SumOfTravelDistances.km, name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$SumOfTravelDistances.km, name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveles Distances (km)')) %>% + layout(title = title) +} +#function to create categories +create_vehicle_categories <- function(data) { + data <- data %>% + mutate(vehicleCategory = ifelse(vehicleTypeId %in% c("light8t", "light8t_frozen"), yes = "light8t", + no = ifelse(vehicleTypeId %in% c("light8t_electro_Mitsubishi", "light8t_electro_Quantron", "light8t_frozen_electro_Mitsubishi", "light8t_frozen_electro_Quantron"), yes = "light8t_electro", + no = ifelse(vehicleTypeId %in% c("medium18t", "medium18t_frozen"), yes = "medium18t", + no = ifelse(vehicleTypeId %in% c("medium18t_electro_Renault", "medium18t_electro_Volvo"), yes = "medium18t_electro", + no = ifelse(vehicleTypeId %in% c("heavy26t", "heavy26t_frozen"), yes = "heavy26t", + no = ifelse(vehicleTypeId %in% c("heavy26t_electro_Daimler", "heavy26t_electro_Renault", "heavy26t_frozen_electro_Daimler", "heavy26t_frozen_electro_Renault"), yes = "heavy26t_electro", + no = ifelse(vehicleTypeId %in% c("heavy40t_electro_Daimler", "heavy40t_electro_Scania", "heavy40t_frozen_electro_Daimler", "heavy40t_frozen_electro_Scania"), yes = "heavy40t_electro", + no = ifelse(vehicleTypeId %in% c("heavy40t", "heavy40t_frozen"), yes = "heavy40t", + no = as.character(vehicleTypeId)))))))))) + # Add a category for the sum of all vehicle types + data <- data %>% + mutate(sum = ifelse(vehicleCategory %in% c("light8t", "light8t_electro","medium18t", "medium18t_electro", + "heavy26t", "heavy26t_electro", "heavy40t", "heavy40t_electro"), + yes = "Sum", no = as.character(vehicleCategory))) + +} +# Read data for the base case +df <- read.delim("run3_Base_100it/Analysis/TimeDistance_perVehicleType.tsv") +df_tours <- read.delim("run3_Base_100it/Analysis/TimeDistance_perVehicle.tsv") + +# Read data for the policy case +df_policy <- read.delim("run2_EV_100it/Analysis/TimeDistance_perVehicleType.tsv") +df_tours_policy <- read.delim("run2_EV_100it/Analysis/TimeDistance_perVehicle.tsv") + +#add categories +df <- create_vehicle_categories(df) +df_tours <- create_vehicle_categories(df_tours) +df_policy <- create_vehicle_categories(df_policy) +df_tours_policy <- create_vehicle_categories(df_tours_policy) + +# Specify the desired order of vehicleTypeId +desired_order <- c("light8t", "light8t_electro","medium18t", "medium18t_electro", + "heavy26t", "heavy26t_electro", "heavy40t", "heavy40t_electro", "Sum") + +# Convert 'vehicleTypeId' to a factor with the desired order for both base and policy cases +df$vehicleCategory <- factor(df$vehicleCategory, levels = desired_order) +df_tours$vehicleCategory <- factor(df_tours$vehicleCategory, levels = desired_order) +df_policy$vehicleCategory <- factor(df_policy$vehicleCategory, levels = desired_order) +df_tours_policy$vehicleCategory <- factor(df_tours_policy$vehicleCategory, levels = desired_order) + +# Ensure both plots have the same levels +common_levels <- intersect(levels(df$vehicleCategory), levels(df$vehicleCategory)) + +# Filter the data frame to include only common levels +#df <- df %>% filter(vehicleTypeId %in% common_levels) + +# 1. Bar Plot for Number of Vehicles by Vehicle Type (Interactive) +combined_bar_plot_vehicles <- create_bar_plot_vehicles(df, df_policy, 'Number of Vehicles by Vehicle Type') + + +# 2. Bar Plot for Total Costs by Vehicle Type (Interactive) +combined_bar_plot_costs <- create_bar_plot_costs(df, df_policy, 'Total Costs by Vehicle Type') + +# 3. Bar Plot for Travel Distacnes by Vehicle Type (Interactive) +combined_bar_plot_distances <- create_bar_plot_distances(df, df_policy, 'Total Traveled Distances by Vehicle Type') + +# 4. Box Plot for Traveled Distances by Vehicle Type (Interactive) +box_plot_distances <- plot_ly(data = df_tours, x = ~vehicleCategory, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0, colors = ) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) +box_plot_distances_policy <- plot_ly(data = df_tours_policy, x = ~vehicleCategory, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) + +# Display the combined bar plot +print(combined_bar_plot_vehicles) +print(combined_bar_plot_costs) +print(combined_bar_plot_distances) +print(box_plot_distances %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type - Base')) +print(box_plot_distances_policy %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type - EV')) From 025ab56b0651c9d1cc7c43a452ffc0b0ea731841 Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Fri, 22 Mar 2024 12:14:16 +0100 Subject: [PATCH 2/4] R Skripte aufgeteilt nach Anwender --- src/main/R/{ => KaiMT}/plotFoodResults.R | 0 .../{ => KaiMT}/plotFoodResults_comparison.R | 0 src/main/R/{ => Ricardo}/KiDdata.R | 0 src/main/R/Ricardo/plotFoodResults.R | 52 ++++++++ .../R/Ricardo/plotFoodResults_comparison.R | 117 ++++++++++++++++++ 5 files changed, 169 insertions(+) rename src/main/R/{ => KaiMT}/plotFoodResults.R (100%) rename src/main/R/{ => KaiMT}/plotFoodResults_comparison.R (100%) rename src/main/R/{ => Ricardo}/KiDdata.R (100%) create mode 100644 src/main/R/Ricardo/plotFoodResults.R create mode 100644 src/main/R/Ricardo/plotFoodResults_comparison.R diff --git a/src/main/R/plotFoodResults.R b/src/main/R/KaiMT/plotFoodResults.R similarity index 100% rename from src/main/R/plotFoodResults.R rename to src/main/R/KaiMT/plotFoodResults.R diff --git a/src/main/R/plotFoodResults_comparison.R b/src/main/R/KaiMT/plotFoodResults_comparison.R similarity index 100% rename from src/main/R/plotFoodResults_comparison.R rename to src/main/R/KaiMT/plotFoodResults_comparison.R diff --git a/src/main/R/KiDdata.R b/src/main/R/Ricardo/KiDdata.R similarity index 100% rename from src/main/R/KiDdata.R rename to src/main/R/Ricardo/KiDdata.R diff --git a/src/main/R/Ricardo/plotFoodResults.R b/src/main/R/Ricardo/plotFoodResults.R new file mode 100644 index 0000000..58e5a86 --- /dev/null +++ b/src/main/R/Ricardo/plotFoodResults.R @@ -0,0 +1,52 @@ +##Kopie von Ricardo. Muss dann noch angepasst werden. KMT März 24 + +setwd("/Users/kturner/git-and-svn/shared-svn/projects/freight/studies/UpdateEventsfromEarlierStudies/foodRetailing_wo_rangeConstraint/71_ICEVBEV_NwCE_BVWP_10000it_DCoff_noTax/analysis") + +# Install and load necessary packages +if (!requireNamespace("tidyverse", quietly = TRUE)) { + install.packages("tidyverse") +} +if (!requireNamespace("plotly", quietly = TRUE)) { + install.packages("plotly") +} +if (!requireNamespace("gridExtra", quietly = TRUE)) { + install.packages("gridExtra") +} +library(tidyverse) +library(plotly) +library(gridExtra) + +# Read the TSV file +df <- read.delim("TimeDistance_perVehicleType.tsv") +df_tours <- read.delim("TimeDistance_perVehicle.tsv") + +# Specify the desired order of vehicleTypeId +desired_order <- c("light8t", "light8t_frozen","medium18t", "medium18t_frozen","heavy26t", "heavy26t_frozen","heavy40t", "heavy40t_frozen") + +# Convert 'vehicleTypeId' to a factor with the desired order +df$vehicleTypeId <- factor(df$vehicleTypeId, levels = desired_order) +df_tours$vehicleTypeId <- factor(df_tours$vehicleTypeId, levels = desired_order) + +# Ensure both plots have the same levels +common_levels <- intersect(levels(df$vehicleTypeId), levels(df$vehicleTypeId)) + +# Filter the data frame to include only common levels +df <- df %>% filter(vehicleTypeId %in% common_levels) + +# 1. Bar Plot for Number of Vehicles by Vehicle Type (Interactive) +bar_plot <- plot_ly(x = ~df$vehicleTypeId, y = df$nuOfVehicles, type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Number of Vehicles')) + +# 3. Bar Plot for Total Costs by Vehicle Type (Interactive) +bar_plot_costs <- plot_ly(x = df$vehicleTypeId, y = df$totalCosts.EUR., type = 'bar', name = 'Total Costs') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Total Costs (EUR)')) + +# 4. Box Plot for Traveled Distances by Vehicle Type (Interactive) +box_plot_distances <- plot_ly(data = df_tours, x = ~vehicleTypeId, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) + +# Display the plots separately +print(bar_plot %>% layout(title = 'Number of Vehicles by Vehicle Type')) +print(bar_plot_costs %>% layout(title = 'Total Costs by Vehicle Type')) +print(box_plot_distances %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type')) diff --git a/src/main/R/Ricardo/plotFoodResults_comparison.R b/src/main/R/Ricardo/plotFoodResults_comparison.R new file mode 100644 index 0000000..dc7c47e --- /dev/null +++ b/src/main/R/Ricardo/plotFoodResults_comparison.R @@ -0,0 +1,117 @@ +##Kopie von Ricardo. Muss dann noch angepasst werden. + +# Set working directory and load necessary packages (assuming it hasn't changed) +setwd("C:/Users/Ricardo/git/shared-svn/projects/freight/studies/WP51_EmissionsFood/output") + +if (!requireNamespace("tidyverse", quietly = TRUE)) { + install.packages("tidyverse") +} +if (!requireNamespace("plotly", quietly = TRUE)) { + install.packages("plotly") +} +if (!requireNamespace("gridExtra", quietly = TRUE)) { + install.packages("gridExtra") +} +library(tidyverse) +library(plotly) +library(gridExtra) + +# Function to create a bar plot for vehicles +create_bar_plot_vehicles <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$nuOfVehicles, name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$nuOfVehicles, name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Number of Vehicles')) %>% + layout(title = title) +} + +# Function to create a bar plot for costs +create_bar_plot_costs <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$totalCosts.EUR., name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$totalCosts.EUR., name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Total Costs (EUR)')) %>% + layout(title = title) +} + +# Function to create a bar plot for distances +create_bar_plot_distances <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$SumOfTravelDistances.km, name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$SumOfTravelDistances.km, name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveles Distances (km)')) %>% + layout(title = title) +} +#function to create categories +create_vehicle_categories <- function(data) { + data <- data %>% + mutate(vehicleCategory = ifelse(vehicleTypeId %in% c("light8t", "light8t_frozen"), yes = "light8t", + no = ifelse(vehicleTypeId %in% c("light8t_electro_Mitsubishi", "light8t_electro_Quantron", "light8t_frozen_electro_Mitsubishi", "light8t_frozen_electro_Quantron"), yes = "light8t_electro", + no = ifelse(vehicleTypeId %in% c("medium18t", "medium18t_frozen"), yes = "medium18t", + no = ifelse(vehicleTypeId %in% c("medium18t_electro_Renault", "medium18t_electro_Volvo"), yes = "medium18t_electro", + no = ifelse(vehicleTypeId %in% c("heavy26t", "heavy26t_frozen"), yes = "heavy26t", + no = ifelse(vehicleTypeId %in% c("heavy26t_electro_Daimler", "heavy26t_electro_Renault", "heavy26t_frozen_electro_Daimler", "heavy26t_frozen_electro_Renault"), yes = "heavy26t_electro", + no = ifelse(vehicleTypeId %in% c("heavy40t_electro_Daimler", "heavy40t_electro_Scania", "heavy40t_frozen_electro_Daimler", "heavy40t_frozen_electro_Scania"), yes = "heavy40t_electro", + no = ifelse(vehicleTypeId %in% c("heavy40t", "heavy40t_frozen"), yes = "heavy40t", + no = as.character(vehicleTypeId)))))))))) + # Add a category for the sum of all vehicle types + data <- data %>% + mutate(sum = ifelse(vehicleCategory %in% c("light8t", "light8t_electro","medium18t", "medium18t_electro", + "heavy26t", "heavy26t_electro", "heavy40t", "heavy40t_electro"), + yes = "Sum", no = as.character(vehicleCategory))) + +} +# Read data for the base case +df <- read.delim("run3_Base_100it/Analysis/TimeDistance_perVehicleType.tsv") +df_tours <- read.delim("run3_Base_100it/Analysis/TimeDistance_perVehicle.tsv") + +# Read data for the policy case +df_policy <- read.delim("run2_EV_100it/Analysis/TimeDistance_perVehicleType.tsv") +df_tours_policy <- read.delim("run2_EV_100it/Analysis/TimeDistance_perVehicle.tsv") + +#add categories +df <- create_vehicle_categories(df) +df_tours <- create_vehicle_categories(df_tours) +df_policy <- create_vehicle_categories(df_policy) +df_tours_policy <- create_vehicle_categories(df_tours_policy) + +# Specify the desired order of vehicleTypeId +desired_order <- c("light8t", "light8t_electro","medium18t", "medium18t_electro", + "heavy26t", "heavy26t_electro", "heavy40t", "heavy40t_electro", "Sum") + +# Convert 'vehicleTypeId' to a factor with the desired order for both base and policy cases +df$vehicleCategory <- factor(df$vehicleCategory, levels = desired_order) +df_tours$vehicleCategory <- factor(df_tours$vehicleCategory, levels = desired_order) +df_policy$vehicleCategory <- factor(df_policy$vehicleCategory, levels = desired_order) +df_tours_policy$vehicleCategory <- factor(df_tours_policy$vehicleCategory, levels = desired_order) + +# Ensure both plots have the same levels +common_levels <- intersect(levels(df$vehicleCategory), levels(df$vehicleCategory)) + +# Filter the data frame to include only common levels +#df <- df %>% filter(vehicleTypeId %in% common_levels) + +# 1. Bar Plot for Number of Vehicles by Vehicle Type (Interactive) +combined_bar_plot_vehicles <- create_bar_plot_vehicles(df, df_policy, 'Number of Vehicles by Vehicle Type') + + +# 2. Bar Plot for Total Costs by Vehicle Type (Interactive) +combined_bar_plot_costs <- create_bar_plot_costs(df, df_policy, 'Total Costs by Vehicle Type') + +# 3. Bar Plot for Travel Distacnes by Vehicle Type (Interactive) +combined_bar_plot_distances <- create_bar_plot_distances(df, df_policy, 'Total Traveled Distances by Vehicle Type') + +# 4. Box Plot for Traveled Distances by Vehicle Type (Interactive) +box_plot_distances <- plot_ly(data = df_tours, x = ~vehicleCategory, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0, colors = ) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) +box_plot_distances_policy <- plot_ly(data = df_tours_policy, x = ~vehicleCategory, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) + +# Display the combined bar plot +print(combined_bar_plot_vehicles) +print(combined_bar_plot_costs) +print(combined_bar_plot_distances) +print(box_plot_distances %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type - Base')) +print(box_plot_distances_policy %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type - EV')) From 4dfb9789ce0b679e361eaaa30c8e586b5e273644 Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Fri, 22 Mar 2024 12:14:16 +0100 Subject: [PATCH 3/4] R Skripte aufgeteilt nach Anwender --- src/main/R/KaiMT/plotFoodResults.R | 52 ++++++++ src/main/R/KaiMT/plotFoodResults_comparison.R | 117 ++++++++++++++++++ src/main/R/{ => Ricardo}/KiDdata.R | 0 src/main/R/{ => Ricardo}/plotFoodResults.R | 0 .../plotFoodResults_comparison.R | 0 5 files changed, 169 insertions(+) create mode 100644 src/main/R/KaiMT/plotFoodResults.R create mode 100644 src/main/R/KaiMT/plotFoodResults_comparison.R rename src/main/R/{ => Ricardo}/KiDdata.R (100%) rename src/main/R/{ => Ricardo}/plotFoodResults.R (100%) rename src/main/R/{ => Ricardo}/plotFoodResults_comparison.R (100%) diff --git a/src/main/R/KaiMT/plotFoodResults.R b/src/main/R/KaiMT/plotFoodResults.R new file mode 100644 index 0000000..58e5a86 --- /dev/null +++ b/src/main/R/KaiMT/plotFoodResults.R @@ -0,0 +1,52 @@ +##Kopie von Ricardo. Muss dann noch angepasst werden. KMT März 24 + +setwd("/Users/kturner/git-and-svn/shared-svn/projects/freight/studies/UpdateEventsfromEarlierStudies/foodRetailing_wo_rangeConstraint/71_ICEVBEV_NwCE_BVWP_10000it_DCoff_noTax/analysis") + +# Install and load necessary packages +if (!requireNamespace("tidyverse", quietly = TRUE)) { + install.packages("tidyverse") +} +if (!requireNamespace("plotly", quietly = TRUE)) { + install.packages("plotly") +} +if (!requireNamespace("gridExtra", quietly = TRUE)) { + install.packages("gridExtra") +} +library(tidyverse) +library(plotly) +library(gridExtra) + +# Read the TSV file +df <- read.delim("TimeDistance_perVehicleType.tsv") +df_tours <- read.delim("TimeDistance_perVehicle.tsv") + +# Specify the desired order of vehicleTypeId +desired_order <- c("light8t", "light8t_frozen","medium18t", "medium18t_frozen","heavy26t", "heavy26t_frozen","heavy40t", "heavy40t_frozen") + +# Convert 'vehicleTypeId' to a factor with the desired order +df$vehicleTypeId <- factor(df$vehicleTypeId, levels = desired_order) +df_tours$vehicleTypeId <- factor(df_tours$vehicleTypeId, levels = desired_order) + +# Ensure both plots have the same levels +common_levels <- intersect(levels(df$vehicleTypeId), levels(df$vehicleTypeId)) + +# Filter the data frame to include only common levels +df <- df %>% filter(vehicleTypeId %in% common_levels) + +# 1. Bar Plot for Number of Vehicles by Vehicle Type (Interactive) +bar_plot <- plot_ly(x = ~df$vehicleTypeId, y = df$nuOfVehicles, type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Number of Vehicles')) + +# 3. Bar Plot for Total Costs by Vehicle Type (Interactive) +bar_plot_costs <- plot_ly(x = df$vehicleTypeId, y = df$totalCosts.EUR., type = 'bar', name = 'Total Costs') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Total Costs (EUR)')) + +# 4. Box Plot for Traveled Distances by Vehicle Type (Interactive) +box_plot_distances <- plot_ly(data = df_tours, x = ~vehicleTypeId, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) + +# Display the plots separately +print(bar_plot %>% layout(title = 'Number of Vehicles by Vehicle Type')) +print(bar_plot_costs %>% layout(title = 'Total Costs by Vehicle Type')) +print(box_plot_distances %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type')) diff --git a/src/main/R/KaiMT/plotFoodResults_comparison.R b/src/main/R/KaiMT/plotFoodResults_comparison.R new file mode 100644 index 0000000..dc7c47e --- /dev/null +++ b/src/main/R/KaiMT/plotFoodResults_comparison.R @@ -0,0 +1,117 @@ +##Kopie von Ricardo. Muss dann noch angepasst werden. + +# Set working directory and load necessary packages (assuming it hasn't changed) +setwd("C:/Users/Ricardo/git/shared-svn/projects/freight/studies/WP51_EmissionsFood/output") + +if (!requireNamespace("tidyverse", quietly = TRUE)) { + install.packages("tidyverse") +} +if (!requireNamespace("plotly", quietly = TRUE)) { + install.packages("plotly") +} +if (!requireNamespace("gridExtra", quietly = TRUE)) { + install.packages("gridExtra") +} +library(tidyverse) +library(plotly) +library(gridExtra) + +# Function to create a bar plot for vehicles +create_bar_plot_vehicles <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$nuOfVehicles, name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$nuOfVehicles, name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Number of Vehicles')) %>% + layout(title = title) +} + +# Function to create a bar plot for costs +create_bar_plot_costs <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$totalCosts.EUR., name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$totalCosts.EUR., name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Total Costs (EUR)')) %>% + layout(title = title) +} + +# Function to create a bar plot for distances +create_bar_plot_distances <- function(df_base, df_policy, title) { + plot_ly() %>% + add_bars(x = ~df_base$vehicleCategory, y = df_base$SumOfTravelDistances.km, name = 'Base Case', type = 'bar') %>% + add_bars(x = ~df_policy$vehicleCategory, y = df_policy$SumOfTravelDistances.km, name = 'Policy Case', type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveles Distances (km)')) %>% + layout(title = title) +} +#function to create categories +create_vehicle_categories <- function(data) { + data <- data %>% + mutate(vehicleCategory = ifelse(vehicleTypeId %in% c("light8t", "light8t_frozen"), yes = "light8t", + no = ifelse(vehicleTypeId %in% c("light8t_electro_Mitsubishi", "light8t_electro_Quantron", "light8t_frozen_electro_Mitsubishi", "light8t_frozen_electro_Quantron"), yes = "light8t_electro", + no = ifelse(vehicleTypeId %in% c("medium18t", "medium18t_frozen"), yes = "medium18t", + no = ifelse(vehicleTypeId %in% c("medium18t_electro_Renault", "medium18t_electro_Volvo"), yes = "medium18t_electro", + no = ifelse(vehicleTypeId %in% c("heavy26t", "heavy26t_frozen"), yes = "heavy26t", + no = ifelse(vehicleTypeId %in% c("heavy26t_electro_Daimler", "heavy26t_electro_Renault", "heavy26t_frozen_electro_Daimler", "heavy26t_frozen_electro_Renault"), yes = "heavy26t_electro", + no = ifelse(vehicleTypeId %in% c("heavy40t_electro_Daimler", "heavy40t_electro_Scania", "heavy40t_frozen_electro_Daimler", "heavy40t_frozen_electro_Scania"), yes = "heavy40t_electro", + no = ifelse(vehicleTypeId %in% c("heavy40t", "heavy40t_frozen"), yes = "heavy40t", + no = as.character(vehicleTypeId)))))))))) + # Add a category for the sum of all vehicle types + data <- data %>% + mutate(sum = ifelse(vehicleCategory %in% c("light8t", "light8t_electro","medium18t", "medium18t_electro", + "heavy26t", "heavy26t_electro", "heavy40t", "heavy40t_electro"), + yes = "Sum", no = as.character(vehicleCategory))) + +} +# Read data for the base case +df <- read.delim("run3_Base_100it/Analysis/TimeDistance_perVehicleType.tsv") +df_tours <- read.delim("run3_Base_100it/Analysis/TimeDistance_perVehicle.tsv") + +# Read data for the policy case +df_policy <- read.delim("run2_EV_100it/Analysis/TimeDistance_perVehicleType.tsv") +df_tours_policy <- read.delim("run2_EV_100it/Analysis/TimeDistance_perVehicle.tsv") + +#add categories +df <- create_vehicle_categories(df) +df_tours <- create_vehicle_categories(df_tours) +df_policy <- create_vehicle_categories(df_policy) +df_tours_policy <- create_vehicle_categories(df_tours_policy) + +# Specify the desired order of vehicleTypeId +desired_order <- c("light8t", "light8t_electro","medium18t", "medium18t_electro", + "heavy26t", "heavy26t_electro", "heavy40t", "heavy40t_electro", "Sum") + +# Convert 'vehicleTypeId' to a factor with the desired order for both base and policy cases +df$vehicleCategory <- factor(df$vehicleCategory, levels = desired_order) +df_tours$vehicleCategory <- factor(df_tours$vehicleCategory, levels = desired_order) +df_policy$vehicleCategory <- factor(df_policy$vehicleCategory, levels = desired_order) +df_tours_policy$vehicleCategory <- factor(df_tours_policy$vehicleCategory, levels = desired_order) + +# Ensure both plots have the same levels +common_levels <- intersect(levels(df$vehicleCategory), levels(df$vehicleCategory)) + +# Filter the data frame to include only common levels +#df <- df %>% filter(vehicleTypeId %in% common_levels) + +# 1. Bar Plot for Number of Vehicles by Vehicle Type (Interactive) +combined_bar_plot_vehicles <- create_bar_plot_vehicles(df, df_policy, 'Number of Vehicles by Vehicle Type') + + +# 2. Bar Plot for Total Costs by Vehicle Type (Interactive) +combined_bar_plot_costs <- create_bar_plot_costs(df, df_policy, 'Total Costs by Vehicle Type') + +# 3. Bar Plot for Travel Distacnes by Vehicle Type (Interactive) +combined_bar_plot_distances <- create_bar_plot_distances(df, df_policy, 'Total Traveled Distances by Vehicle Type') + +# 4. Box Plot for Traveled Distances by Vehicle Type (Interactive) +box_plot_distances <- plot_ly(data = df_tours, x = ~vehicleCategory, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0, colors = ) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) +box_plot_distances_policy <- plot_ly(data = df_tours_policy, x = ~vehicleCategory, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0) %>% + layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) + +# Display the combined bar plot +print(combined_bar_plot_vehicles) +print(combined_bar_plot_costs) +print(combined_bar_plot_distances) +print(box_plot_distances %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type - Base')) +print(box_plot_distances_policy %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type - EV')) diff --git a/src/main/R/KiDdata.R b/src/main/R/Ricardo/KiDdata.R similarity index 100% rename from src/main/R/KiDdata.R rename to src/main/R/Ricardo/KiDdata.R diff --git a/src/main/R/plotFoodResults.R b/src/main/R/Ricardo/plotFoodResults.R similarity index 100% rename from src/main/R/plotFoodResults.R rename to src/main/R/Ricardo/plotFoodResults.R diff --git a/src/main/R/plotFoodResults_comparison.R b/src/main/R/Ricardo/plotFoodResults_comparison.R similarity index 100% rename from src/main/R/plotFoodResults_comparison.R rename to src/main/R/Ricardo/plotFoodResults_comparison.R From 99e3d0fb2a4e0784adca43a4566c3f7ca7dbe697 Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Fri, 22 Mar 2024 14:27:53 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Violine=20Plots=20km=20f=C3=BCr=20KMTs=20ru?= =?UTF-8?q?ns.=20Noch=20nicht=20perfekt,=20aber=20auf=20dem=20Weg...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/R/KaiMT/plotFoodResults.R | 78 ++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/src/main/R/KaiMT/plotFoodResults.R b/src/main/R/KaiMT/plotFoodResults.R index 58e5a86..8d65c53 100644 --- a/src/main/R/KaiMT/plotFoodResults.R +++ b/src/main/R/KaiMT/plotFoodResults.R @@ -16,37 +16,73 @@ library(tidyverse) library(plotly) library(gridExtra) -# Read the TSV file -df <- read.delim("TimeDistance_perVehicleType.tsv") -df_tours <- read.delim("TimeDistance_perVehicle.tsv") +# Read the CSV file +df <- read.csv2("TimeDistance_perVehicleType.csv") +df_tours <- read.csv2("TimeDistance_perVehicle.csv") # Specify the desired order of vehicleTypeId -desired_order <- c("light8t", "light8t_frozen","medium18t", "medium18t_frozen","heavy26t", "heavy26t_frozen","heavy40t", "heavy40t_frozen") +desired_order <- c("7.5t", "7.5t_electro","18t", "18t_electro","26t", "26t_electro","40t", "40t_electro") -# Convert 'vehicleTypeId' to a factor with the desired order -df$vehicleTypeId <- factor(df$vehicleTypeId, levels = desired_order) -df_tours$vehicleTypeId <- factor(df_tours$vehicleTypeId, levels = desired_order) +#function to create categories +create_vehicle_categories <- function(data) { + data <- data %>% + mutate(vehicleCategory = ifelse(vehicleTypeId %in% c("light8t", "light8t_frozen"), yes = "7.5t", + no = ifelse(vehicleTypeId %in% c("light8t_electro", "light8t_frozen_electro"), yes = "7.5t_electro", + no = ifelse(vehicleTypeId %in% c("medium18t", "medium18t_frozen"), yes = "18t", + no = ifelse(vehicleTypeId %in% c("medium18t_electro", "medium18t_electro"), yes = "18t_electro", + no = ifelse(vehicleTypeId %in% c("heavy26t", "heavy26t_frozen"), yes = "26t", + no = ifelse(vehicleTypeId %in% c("heavy26t_electro", "heavy26t_frozen_electro"), yes = "26t_electro", + no = ifelse(vehicleTypeId %in% c("heavy40t", "heavy40t_frozen"), yes = "40t", + no = ifelse(vehicleTypeId %in% c("heavy40t_electro", "heavy40t_frozen_electro"), yes = "40t_electro", + no = as.character(vehicleTypeId)))))))))) +} + +df <- create_vehicle_categories(df) +df_tours <- create_vehicle_categories(df_tours) + + +# Convert 'vehicleCategory' to a factor with the desired order +df$vehicleCategory <- factor(df$vehicleCategory, levels = desired_order) +df_tours$vehicleCategory <- factor(df_tours$vehicleCategory, levels = desired_order) -# Ensure both plots have the same levels -common_levels <- intersect(levels(df$vehicleTypeId), levels(df$vehicleTypeId)) +#Runde Werte auf 2 Nachkommastellen +df_tours$travelDistance.km. <- as.numeric(df_tours$travelDistance.km.) +df_tours$travelDistance.km. <- round(df_tours$travelDistance.km.,2) -# Filter the data frame to include only common levels -df <- df %>% filter(vehicleTypeId %in% common_levels) +# # Ensure both plots have the same levels +# common_levels <- intersect(levels(df$vehicleCategory), levels(df$vehicleCategory)) +# +# # Filter the data frame to include only common levels +# df <- df %>% filter(vehicleCategory %in% common_levels) # 1. Bar Plot for Number of Vehicles by Vehicle Type (Interactive) -bar_plot <- plot_ly(x = ~df$vehicleTypeId, y = df$nuOfVehicles, type = 'bar') %>% - layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Number of Vehicles')) +bar_plot <- plot_ly(x = ~df$vehicleCategory, y = df$nuOfVehicles, type = 'bar') %>% + layout(xaxis = list(title = 'Vehicle Category'), yaxis = list(title = 'Number of Vehicles')) # 3. Bar Plot for Total Costs by Vehicle Type (Interactive) -bar_plot_costs <- plot_ly(x = df$vehicleTypeId, y = df$totalCosts.EUR., type = 'bar', name = 'Total Costs') %>% - layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Total Costs (EUR)')) +bar_plot_costs <- plot_ly(x = df$vehicleCategory, y = df$totalCosts.EUR., type = 'bar', name = 'Total Costs') %>% + layout(xaxis = list(title = 'Category'), yaxis = list(title = 'Total Costs (EUR)')) # 4. Box Plot for Traveled Distances by Vehicle Type (Interactive) -box_plot_distances <- plot_ly(data = df_tours, x = ~vehicleTypeId, y = ~travelDistance.km., - type = 'box', boxpoints = "all", jitter = 0.0, pointpos = -0.0) %>% - layout(xaxis = list(title = 'Vehicle Type'), yaxis = list(title = 'Traveled Distances (km)')) +max_y_km <- round(max(df_tours$travelDistance.km.),-2) + + +box_plot_distances <- plot_ly(data = df_tours, x = ~vehicleCategory, y = ~travelDistance.km., + type = 'box', boxpoints = "all", jitter = 0.5, pointpos = -1.0) %>% + layout(xaxis = list(title = 'Category'), yaxis = list(title = 'Traveled Distances (km)', range = list(0.,max_y_km))) + +# 4b Violin- Plot Distances by Vehicle Type (Interactive) +violin_plot_distances <- plot_ly(data = df_tours, + x = ~vehicleCategory, + y = ~travelDistance.km., + split = ~vehicleCategory, + type = 'violin', + box = list(visible = T), + points = "all", jitter = 0.5, pointpos = -1.5) %>% + layout(xaxis = list(title = 'Category'), yaxis = list(title = 'Traveled Distances (km)', range = list(0.,max_y_km))) # Display the plots separately -print(bar_plot %>% layout(title = 'Number of Vehicles by Vehicle Type')) -print(bar_plot_costs %>% layout(title = 'Total Costs by Vehicle Type')) -print(box_plot_distances %>% layout(title = 'Box Plot of Traveled Distances by Vehicle Type')) +print(bar_plot %>% layout(title = 'Number of Vehicles by Vehicle Category')) +print(bar_plot_costs %>% layout(title = 'Total Costs by Vehicle Category')) +print(box_plot_distances %>% layout(title = 'Traveled Distances by Vehicle Category')) +print(violin_plot_distances %>% layout(title = 'Traveled Distances by Vehicle Category'))