From e289f7d356fb9acf6a303eb15ea6a58ac6438f68 Mon Sep 17 00:00:00 2001 From: "mael.pretet" Date: Fri, 4 Apr 2025 16:19:20 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20ajout=20d'une=20fonction=20pour=20visua?= =?UTF-8?q?liser=20une=20s=C3=A9rie=20temporelle=20avec=20un=20barplot=20+?= =?UTF-8?q?=20options=20esth=C3=A9tiques?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fonctions/fct_time_series.R | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 fonctions/fct_time_series.R diff --git a/fonctions/fct_time_series.R b/fonctions/fct_time_series.R new file mode 100644 index 0000000..acc93f8 --- /dev/null +++ b/fonctions/fct_time_series.R @@ -0,0 +1,58 @@ +#' Graphique +#' +#' @param df A dataframe +#' @param x A character +#' @param y A character +#' @param fill A character +#' @param xlab A character +#' @param ylab A character +#' +#' @return A ggplot object +#' +#' @examples +#' df_test = data.frame(x1 = c(2012, 2012, 2013, 2013, 2014, 2014), +#' y1 = c(30, 40, 42, 67, 19, 21), +#' fill1 = c("new", "old", "new", "new", "old", "new")) +#' barplot_time_series(df = df_test, x = "x1", y = "y1", fill = "fill1") +#' barplot_time_series(df = df_test_2, x = "x1", fill = "fill1", stat = "count") +#' barplot_time_series(df = df_test, x = "x1", y = "y1", fill = "fill1", modif_x_axis = T, position = "dodge") +#' barplot_time_series(df = df_test, x = "x1", y = "y1", fill = "fill1", +#' fix_ratio = TRUE, c_fix = 0.05) +#' barplot_time_series(df = df_test, x = "x1", y = "y1", fill = "fill1", +#' fix_ratio = TRUE, c_fix = 0.5) +#' +barplot_time_series <- function(df, x = "year", y = NA, color_bar = NA, + fill = "column_for_colour", + lab_title = "lab_for_colour", + xlab = "Years", ylab = "y_title", + position = "stack", + scale_fill = c('#009ef8', '#76ea02'), + modif_x_axis = FALSE, + fix_ratio = FALSE, c_fix = 0.15){ + if (is.na(y)) { + gg <- ggplot(df, aes(x = !!sym(x), fill = !!sym(fill))) + stat = "count" + }else{ + gg <- ggplot(df, aes(x = !!sym(x), y = !!sym(y), fill = !!sym(fill))) + stat = "identity" + } + + gg <- gg + + geom_bar(stat = stat, position = position, color = color_bar)+ + scale_fill_manual(values=scale_fill)+ + ylab(ylab) + + xlab(xlab) + + labs(fill = lab_title) + + theme_cowplot() + + if (modif_x_axis) { + gg <- gg + + theme(axis.text.x = element_text(angle = 45, size = 10, hjust = 1)) + } + if (fix_ratio) { + gg <- gg + + coord_fixed(ratio = c_fix) + } + + return(gg) +} -- GitLab