From 41a1e251a1f40ae5b5d9c2b6b19fd6dd9dcc1d33 Mon Sep 17 00:00:00 2001 From: buddekai Date: Tue, 21 Feb 2023 12:38:06 +0100 Subject: [PATCH] Better plotting --- R/plotMeasurements.R | 16 ++++++++----- R/plotWaveforms.R | 53 +++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/R/plotMeasurements.R b/R/plotMeasurements.R index 7efb907..86f52e8 100644 --- a/R/plotMeasurements.R +++ b/R/plotMeasurements.R @@ -15,7 +15,7 @@ #' @return 0 # Created: 2022/04/20 -# Last changed: 2022/08/25 +# Last changed: 2023/02/21 plotMeasurements <- function(input_data = NULL, output_dir = NULL, vavg_min = NULL, vavg_max = NULL, @@ -43,10 +43,16 @@ plotMeasurements <- function(input_data = NULL, output_dir = NULL, # Set plot title if(is.null(plot_title)){ plot_title <- "" + + # Get date of experiment + date_of_experiment <- unique(as.Date(input_data$date_time))[1] + + name_for_saving <- date_of_experiment + }else{ + name_for_saving <- gsub(pattern = " ", replacement = "_", x = plot_title) } - # Get date of experiment - date_of_experiment <- unique(as.Date(input_data$date_time))[1] + # Determine Vavg minimum and maximum if not given if(is.null(vavg_min)){ @@ -137,7 +143,7 @@ plotMeasurements <- function(input_data = NULL, output_dir = NULL, # } # Save files - file_path <- file.path(output_dir, paste0(date_of_experiment, "_vavg_measurement.png")) + file_path <- file.path(output_dir, paste0(name_for_saving, "_vavg_measurement.png")) ggsave(plot = plot_vavg, filename = file_path, device = "png", width = 19.2, height = 10.8, units = "cm") # Find VPP ################################################################# @@ -158,7 +164,7 @@ plotMeasurements <- function(input_data = NULL, output_dir = NULL, # scale_x_datetime(date_breaks = timedate_breaks) # } - file_path <- file.path(output_dir, paste0(date_of_experiment, "_vp2p_measurement.png")) + file_path <- file.path(output_dir, paste0(name_for_saving, "_vp2p_measurement.png")) ggsave(plot = plot_vpp, filename = file_path, device = "png", width = 19.2, height = 10.8, units = "cm") return(invisible(NULL)) diff --git a/R/plotWaveforms.R b/R/plotWaveforms.R index 5d78d50..2cbcc13 100644 --- a/R/plotWaveforms.R +++ b/R/plotWaveforms.R @@ -23,7 +23,7 @@ #' @return 0 # Created: 2022/04/21 -# Last changed: 2022/04/21 +# Last changed: 2023/02/21 plotWaveforms <- function(input_data = NULL, output_dir = NULL, @@ -57,10 +57,22 @@ plotWaveforms <- function(input_data = NULL, # Set plot title if(is.null(plot_title)){ plot_title <- "" + plot_title_without_date <- "" + + # Get date of experiment + date_of_experiment <- unique(as.Date(input_data$date_time))[1] + + name_for_saving <- date_of_experiment + name_for_saving_without_date <- "" + + }else{ + name_for_saving <- gsub(pattern = " ", replacement = "_", x = plot_title) + + plot_title_without_date <- substr(x = plot_title, start = 12, stop = nchar(plot_title)) + name_for_saving_without_date <- gsub(pattern = " ", replacement = "_", x = plot_title_without_date) } - # Get date of experiment - date_of_experiment <- unique(as.Date(input_data$date_time))[1] + # Convert time to \mu s ################################################## if(show_time_in_us){ @@ -158,10 +170,10 @@ plotWaveforms <- function(input_data = NULL, max_value <- as.numeric(quantile(input_data_filtered$U[ input_data_filtered$Channel == channel_stimulation_pulse & input_data_filtered$U > lower_bound], - 0.95, na.rm = TRUE)) + 0.99, na.rm = TRUE)) min_value <- as.numeric(quantile(input_data_filtered$U[ input_data_filtered$Channel == channel_stimulation_pulse & input_data_filtered$U < upper_bound], - 0.05, na.rm = TRUE)) + 0.01, na.rm = TRUE)) p2p_value <- abs(max_value) + abs(min_value) if(is.na(p2p_value)){ @@ -199,7 +211,7 @@ plotWaveforms <- function(input_data = NULL, geom_hline(yintercept=mean_value, linetype="dotdash", color = "darkgray", size=1) + annotate("text", x=plot_annotation_x, y=(voltage_limits_of_plot-2), label=p2p_value) + coord_cartesian(ylim = c(-voltage_limits_of_plot, voltage_limits_of_plot)) + - labs(title=paste(plot_title, input_data_filtered$date_time[1], sep=" "), + labs(title=paste(plot_title_without_date, input_data_filtered$date_time[1], sep=" "), x = xaxis_lab, y = "U/V") + theme_bw() + theme(axis.text.x = element_text(angle=90, vjust = 0.5), @@ -211,7 +223,7 @@ plotWaveforms <- function(input_data = NULL, date_time_measurement <- gsub(pattern = ":", replacement = "", x = date_time_measurement) # Save files - file_path <- file.path(output_dir, paste0(date_time_measurement, ".png")) + file_path <- file.path(output_dir, paste0(date_time_measurement, "_", name_for_saving_without_date, ".png")) ggsave(plot = plot_waveform, filename = file_path, device = "png", width = 19.2, height = 10.8, units = "cm") } @@ -229,10 +241,10 @@ plotWaveforms <- function(input_data = NULL, max_value <- as.numeric(quantile(input_data$U[ input_data$Channel == channel_stimulation_pulse & input_data$U > lower_bound], - 0.95, na.rm = TRUE)) + 0.99, na.rm = TRUE)) min_value <- as.numeric(quantile(input_data$U[ input_data$Channel == channel_stimulation_pulse & input_data$U < upper_bound], - 0.05, na.rm = TRUE)) + 0.01, na.rm = TRUE)) p2p_value <- abs(max_value) + abs(min_value) @@ -243,12 +255,23 @@ plotWaveforms <- function(input_data = NULL, p2p_value <- paste("V_p2p=", p2p_value, "V", sep="") # Calculate mean - df_dummy <- input_data %>% - dplyr::filter(Channel == channel_stimulation_pulse) %>% - dplyr::filter(time >= start_point_function_generator_pulse & - time < end_point_function_generator_pulse) + if(start_point_function_generator_pulse == end_point_function_generator_pulse){ + df_dummy <- input_data %>% + dplyr::filter(Channel == channel_stimulation_pulse) + + mean_value <- mean(df_dummy$U) + }else{ + df_dummy <- input_data %>% + dplyr::filter(Channel == channel_stimulation_pulse) %>% + dplyr::filter(time >= start_point_function_generator_pulse & + time < end_point_function_generator_pulse) + + mean_value <- mean(df_dummy$U) + } + + + - mean_value <- mean(df_dummy$U) # Plot complete data from YAML file #################################### plot_annotation_x <- 0.05*max(input_data$time) @@ -269,7 +292,7 @@ plotWaveforms <- function(input_data = NULL, plot.title = element_text(hjust = 0.5)) # Save files - file_path <- file.path(output_dir, paste0(date_of_experiment, "_all_in_one_", plot_title, ".png")) + file_path <- file.path(output_dir, paste0(name_for_saving, "_all_in_one.png")) ggsave(plot = plot_waveform, filename = file_path, device = "png", width = 19.2, height = 10.8, units = "cm") return(invisible(NULL))