From b7a84c5a78d186c45b2360973d6c2b5e8f7280b0 Mon Sep 17 00:00:00 2001 From: antux18 Date: Fri, 9 Aug 2024 18:31:17 +0200 Subject: [PATCH] Improved the formatting of dates in R plots. Switched to a functionnal approach in the line plot. Broke the bar plot while trying to improve the formatting of dates :( Added the tidyverse R package to the Nix flake. --- flake.nix | 1 + plot/bar_plot.r | 15 ++++++++++++--- plot/line_plot.r | 16 ++++++---------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/flake.nix b/flake.nix index 7c200c2..5dca042 100644 --- a/flake.nix +++ b/flake.nix @@ -28,6 +28,7 @@ requests kapkgs.execo ])) + (rWrapper.override { packages = with rPackages; [ tidyverse reshape2 ]; }) ]; }; latex = pkgs.mkShell { diff --git a/plot/bar_plot.r b/plot/bar_plot.r index bf83260..649c014 100755 --- a/plot/bar_plot.r +++ b/plot/bar_plot.r @@ -1,17 +1,26 @@ #!/usr/bin/env Rscript +# Libraries: +library(tidyverse) + # Parsing command line arguments: options = commandArgs(trailingOnly = TRUE) filename = options[1] table_header = options[-1] # Loading files: -table = read.csv(filename, header = FALSE, row.names = length(table_header) + 1) # The last column of the table gives the timestamp, thus the row names +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: -colnames(table) = table_header # Transposing for bar plotting: table = t(as.matrix(table)) # Plotting: -barplot(table) \ No newline at end of file +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 diff --git a/plot/line_plot.r b/plot/line_plot.r index 38c8ca5..a17975d 100755 --- a/plot/line_plot.r +++ b/plot/line_plot.r @@ -1,20 +1,16 @@ #!/usr/bin/env Rscript # Libraries: -library(ggplot2) library(reshape2) +library(tidyverse) # Parsing command line arguments: options = commandArgs(trailingOnly = TRUE) filename = options[1] table_header = options[-1] -# Loading files: -table = read.csv(filename, header = FALSE) - -# Setting up the table so it can be plotted: -colnames(table) = table_header -melted_table = melt(table, id.vars = "timestamp", variable.name = "category") - -# Plotting: -ggplot(melted_table, aes(timestamp, value)) + geom_line(aes(colour = category)) \ 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)) + + geom_line(aes(color = category))