Finished bar plots. Merged bar and line plots into a single plot script.

This commit is contained in:
antux18 2024-08-14 12:51:52 +02:00
parent b7a84c5a78
commit 13780c9f39
3 changed files with 28 additions and 17 deletions

View File

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

View File

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

21
plot/plot.r Executable file
View File

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