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 requests
kapkgs.execo kapkgs.execo
])) ]))
(rWrapper.override { packages = with rPackages; [ tidyverse reshape2 ]; })
]; ];
}; };
latex = pkgs.mkShell { latex = pkgs.mkShell {

View File

@ -1,17 +1,26 @@
#!/usr/bin/env Rscript #!/usr/bin/env Rscript
# Libraries:
library(tidyverse)
# Parsing command line arguments: # Parsing command line arguments:
options = commandArgs(trailingOnly = TRUE) options = commandArgs(trailingOnly = TRUE)
filename = options[1] filename = options[1]
table_header = options[-1] table_header = options[-1]
# Loading files: # 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: # Setting up the table so it can be plotted:
colnames(table) = table_header
# Transposing for bar plotting: # Transposing for bar plotting:
table = t(as.matrix(table)) table = t(as.matrix(table))
# Plotting: # 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 #!/usr/bin/env Rscript
# Libraries: # Libraries:
library(ggplot2)
library(reshape2) library(reshape2)
library(tidyverse)
# Parsing command line arguments: # Parsing command line arguments:
options = commandArgs(trailingOnly = TRUE) options = commandArgs(trailingOnly = TRUE)
filename = options[1] filename = options[1]
table_header = options[-1] table_header = options[-1]
# Loading files: read_csv(filename, col_names=table_header) %>%
table = read.csv(filename, header = FALSE) 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
# Setting up the table so it can be plotted: ggplot(aes(x = timestamp, y = amount)) +
colnames(table) = table_header geom_line(aes(color = category))
melted_table = melt(table, id.vars = "timestamp", variable.name = "category")
# Plotting:
ggplot(melted_table, aes(timestamp, value)) + geom_line(aes(colour = category))