diff --git a/NEWS.md b/NEWS.md index f352b80..accc8ce 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,13 @@ # protti 0.9.1.9000 +## New features + +* `calculate_go_enrichment()` received the argument `label_size` that allows the user to specifiy the size of the labels in the plot. + ## Bug fixes * Fixed issue #193. This makes sure that information in retained columns can be propagated to newly created combinations, which were not present in the original data. +* `calculate_go_enrichment()` can now correctly handle groups that are of type factor. ## Additional Changes diff --git a/R/calculate_go_enrichment.R b/R/calculate_go_enrichment.R index 6e37539..f2327c2 100644 --- a/R/calculate_go_enrichment.R +++ b/R/calculate_go_enrichment.R @@ -78,6 +78,8 @@ go_enrichment <- function(...) { #' should be reversed in order. Default is `TRUE`. #' @param label a logical argument indicating whether labels should be added to the plot. #' Default is TRUE. +#' @param label_size a numeric argument that specifies the text size of the labels with the unit "pt". +#' The default is 7. #' @param enrichment_type a character argument that is either "all", "enriched" or "deenriched". This #' determines if the enrichment analysis should be performed in order to check for both enrichemnt and #' deenrichemnt or only one of the two. This affects the statistics performed and therefore also the displayed @@ -223,6 +225,7 @@ calculate_go_enrichment <- function(data, heatmap_fill_colour = protti::mako_colours, heatmap_fill_colour_rev = TRUE, label = TRUE, + label_size = 7, enrichment_type = "all", replace_long_name = TRUE, label_move_frac = 0.2, @@ -408,12 +411,14 @@ if you used the right organism ID.", prefix = "\n", initial = "")) } result_table <- cont_table %>% + dplyr::arrange({{ group }}) %>% { # group argument is missing if (group_missing) { dplyr::left_join(., fisher_test, by = "go_id") } else { # group argument is not missing dplyr::left_join(., fisher_test, by = c("go_id", rlang::as_name(enquo(group)))) %>% + dplyr::mutate({{ group }} := forcats::fct_inorder({{ group }})) %>% dplyr::group_by({{ group }}) } } %>% @@ -481,7 +486,14 @@ if you used the right organism ID.", prefix = "\n", initial = "")) # move label if bar is less than 20% (default) of largest bar plot_input <- plot_input %>% - dplyr::mutate(hjust = ifelse((.data$neg_log_sig / max(.data$neg_log_sig)) < label_move_frac, -0.15, 1.05)) + dplyr::mutate(hjust = ifelse((.data$neg_log_sig / max(.data$neg_log_sig)) < label_move_frac, -0.15, 1.05)) %>% + dplyr::mutate(label = ifelse(!is.na(.data$n_significant_proteins_in_process), paste0( + .data$n_significant_proteins_in_process, "/", + .data$n_detected_proteins_in_process, " (", + round(.data$n_significant_proteins_in_process / .data$n_detected_proteins_in_process * 100, digits = 1), "%)" + ), + NA + )) if (plot_style == "barplot") { # Check if ggforce package is available. If not prompt user to install it. @@ -519,22 +531,19 @@ if you used the right organism ID.", prefix = "\n", initial = "")) geom_text( data = plot_input, aes( - label = paste0( - .data$n_significant_proteins_in_process, - "/", - .data$n_detected_proteins_in_process, - "(", - round(.data$n_significant_proteins_in_process / .data$n_detected_proteins_in_process * 100, digits = 1), - "%)" - ), + label = label, y = .data$neg_log_sig - 0.1, hjust = .data$hjust - ) + ), + size = label_size / .pt ) } } + ggplot2::scale_fill_manual(values = c(Deenriched = barplot_fill_colour[1], Enriched = barplot_fill_colour[2])) + - ggplot2::scale_y_continuous(labels = scales::number_format(accuracy = 1)) + + ggplot2::scale_y_continuous( + labels = scales::number_format(accuracy = 1), + expand = expansion(mult = c(0, 0.05)) + ) + ggplot2::coord_flip() + { if (!missing(group)) { @@ -605,7 +614,11 @@ if you used the right organism ID.", prefix = "\n", initial = "")) if (group_missing) { dplyr::mutate(., grouping = "") } else { - dplyr::mutate(., grouping = forcats::fct_inorder({{ group }})) + if (!is(dplyr::pull(plot_input, {{ group }}), "factor")) { # if the column is already a factor do not reorder + dplyr::mutate(., grouping = forcats::fct_inorder({{ group }})) + } else { + dplyr::mutate(., grouping = {{ group }}) + } } } @@ -631,7 +644,15 @@ if you used the right organism ID.", prefix = "\n", initial = "")) # Add text colours to data plot_input_heatmap <- plot_input_heatmap %>% - dplyr::mutate(text_col = ifelse(hcl[, "l"] > 50, "#000000", "#FFFFFF")) + dplyr::mutate(text_col = ifelse(hcl[, "l"] > 50, "#000000", "#FFFFFF")) %>% + # Add label names + dplyr::mutate(label = ifelse(!is.na(.data$n_significant_proteins_in_process), paste0( + .data$n_significant_proteins_in_process, "/", + .data$n_detected_proteins_in_process, " (", + round(.data$n_significant_proteins_in_process / .data$n_detected_proteins_in_process * 100), "%)" + ), + NA + )) # Heatmap Plot enrichment_plot <- @@ -651,18 +672,15 @@ if you used the right organism ID.", prefix = "\n", initial = "")) aes( y = .data$term, x = .data$grouping, - label = ifelse(!is.na(.data$n_significant_proteins_in_process), paste0( - .data$n_significant_proteins_in_process, "/", - .data$n_detected_proteins_in_process, " (", - round(.data$n_significant_proteins_in_process / .data$n_detected_proteins_in_process * 100), "%)" - ), - NA - ) + label = label ), - colour = plot_input_heatmap$text_col + colour = plot_input_heatmap$text_col, + size = label_size / .pt ) } } + + ggplot2::scale_x_discrete(expand = expansion(mult = c(0, 0))) + + ggplot2::scale_y_discrete(expand = expansion(mult = c(0, 0))) + ggplot2::scale_fill_gradientn(colours = colours) + ggplot2::labs( title = plot_title, diff --git a/man/calculate_go_enrichment.Rd b/man/calculate_go_enrichment.Rd index 884bff1..7102a1e 100644 --- a/man/calculate_go_enrichment.Rd +++ b/man/calculate_go_enrichment.Rd @@ -22,6 +22,7 @@ calculate_go_enrichment( heatmap_fill_colour = protti::mako_colours, heatmap_fill_colour_rev = TRUE, label = TRUE, + label_size = 7, enrichment_type = "all", replace_long_name = TRUE, label_move_frac = 0.2, @@ -96,6 +97,9 @@ should be reversed in order. Default is \code{TRUE}.} \item{label}{a logical argument indicating whether labels should be added to the plot. Default is TRUE.} +\item{label_size}{a numeric argument that specifies the text size of the labels with the unit "pt". +The default is 7.} + \item{enrichment_type}{a character argument that is either "all", "enriched" or "deenriched". This determines if the enrichment analysis should be performed in order to check for both enrichemnt and deenrichemnt or only one of the two. This affects the statistics performed and therefore also the displayed