From 13780c9f39bc87b62fe445df8c42bd65d2c36981 Mon Sep 17 00:00:00 2001 From: antux18 Date: Wed, 14 Aug 2024 12:51:52 +0200 Subject: [PATCH] Finished bar plots. Merged bar and line plots into a single plot script. --- plot/bar_plot.r | 22 ++++++---------------- plot/line_plot.r | 2 +- plot/plot.r | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 17 deletions(-) create mode 100755 plot/plot.r diff --git a/plot/bar_plot.r b/plot/bar_plot.r index 649c014..f622203 100755 --- a/plot/bar_plot.r +++ b/plot/bar_plot.r @@ -1,6 +1,7 @@ #!/usr/bin/env Rscript # Libraries: +library(reshape2) library(tidyverse) # Parsing command line arguments: @@ -8,19 +9,8 @@ options = commandArgs(trailingOnly = TRUE) filename = options[1] table_header = options[-1] -# Loading files: -table = read.csv(filename, col_names = table_header, row.names = length(table_header)) # The last column of the table gives the timestamp, thus the row names - -# Setting up the table so it can be plotted: -# Transposing for bar plotting: -table = t(as.matrix(table)) - -# Plotting: -barplot(table) -# legend("topright", legend = c("Level 1", "Level 2"), fill = c("red", "darkblue")) - -# read.csv(filename, col_names = table_header, row.names = length(table_header) + 1) %>% # The last column of the table gives the timestamp, thus the row names -# mutate(timestamp = as_date(as_datetime(timestamp))) %>% # Formatting the date -# as.matrix() %>% # Converting to matrix to transpose -# t() %>% # Transposing for bar plotting -# barplot() \ No newline at end of file +read_csv(filename, col_names = table_header) %>% + mutate(timestamp = as_date(as_datetime(timestamp))) %>% # Formatting the date + melt(id.vars = "timestamp", variable.name = "category", value.name = "amount") %>% # Formatting the table to plot each category + ggplot(aes(x = timestamp, y = amount, fill = category)) + + geom_bar(stat = "identity") \ No newline at end of file diff --git a/plot/line_plot.r b/plot/line_plot.r index a17975d..e905d73 100755 --- a/plot/line_plot.r +++ b/plot/line_plot.r @@ -9,7 +9,7 @@ options = commandArgs(trailingOnly = TRUE) filename = options[1] table_header = options[-1] -read_csv(filename, col_names=table_header) %>% +read_csv(filename, col_names = table_header) %>% mutate(timestamp = as_date(as_datetime(timestamp))) %>% # Formatting the date melt(id.vars = "timestamp", variable.name = "category", value.name = "amount") %>% # Formatting the table to plot each category ggplot(aes(x = timestamp, y = amount)) + diff --git a/plot/plot.r b/plot/plot.r new file mode 100755 index 0000000..752ded4 --- /dev/null +++ b/plot/plot.r @@ -0,0 +1,21 @@ +#!/usr/bin/env Rscript + +# Libraries: +library(reshape2) +library(tidyverse) + +# Parsing command line arguments: +options = commandArgs(trailingOnly = TRUE) +filename = options[1] +plot_type = options[2] +table_header = options[-1:-2] + +read_csv(filename, col_names = table_header) %>% + mutate(timestamp = as_date(as_datetime(timestamp))) %>% # Formatting the date + melt(id.vars = "timestamp", variable.name = "category", value.name = "amount") %>% # Formatting the table to plot each category + { + if (plot_type == "bar") + ggplot(data = ., aes(x = timestamp, y = amount, fill = category)) + geom_bar(stat = "identity") + else if (plot_type == "line") + ggplot(data = ., aes(x = timestamp, y = amount)) + geom_line(aes(color = category)) + }