Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 committed Mar 22, 2024
2 parents 55dbb8a + 458e6b8 commit dd8be16
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/main/R/KaiMT/plotFoodResults.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
##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 CSV file
df <- read.csv2("TimeDistance_perVehicleType.csv")
df_tours <- read.csv2("TimeDistance_perVehicle.csv")

# Specify the desired order of vehicleTypeId
desired_order <- c("7.5t", "7.5t_electro","18t", "18t_electro","26t", "26t_electro","40t", "40t_electro")

#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)

#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)

# # 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$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$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)
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 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'))
117 changes: 117 additions & 0 deletions src/main/R/KaiMT/plotFoodResults_comparison.R
Original file line number Diff line number Diff line change
@@ -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'))
File renamed without changes.
50 changes: 50 additions & 0 deletions src/main/R/Ricardo/plotFoodResults.R
Original file line number Diff line number Diff line change
@@ -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'))
Loading

0 comments on commit dd8be16

Please sign in to comment.