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.

This commit is contained in:
antux18 2024-08-09 18:31:17 +02:00
parent 5aae58d680
commit b7a84c5a78
3 changed files with 19 additions and 13 deletions

View File

@ -28,6 +28,7 @@
requests
kapkgs.execo
]))
(rWrapper.override { packages = with rPackages; [ tidyverse reshape2 ]; })
];
};
latex = pkgs.mkShell {

View File

@ -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)
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()

View File

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