Skip to content

Commit

Permalink
Added parameter labels to plot_expression_partitions and plot_express…
Browse files Browse the repository at this point in the history
…ion_frequencies where users can specify labels for each generation
  • Loading branch information
almeidasilvaf committed Nov 28, 2023
1 parent f6d5859 commit 87e8868
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
24 changes: 18 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ get_deg_summary <- function(deg_list) {
#' @param group_by Character indicating the name of the variable
#' in \strong{partition_table} to use to group genes. One of "Category" or
#' "Class". Default: "Category".
#' @param labels A character vector of length 3 indicating the labels to be
#' given for parent 1, offspring, and parent 2.
#' Default: \code{c("P1", "F1", "P2")}.
#'
#' @return A ggplot object with a scatterplot.
#'
Expand All @@ -190,7 +193,8 @@ get_deg_summary <- function(deg_list) {
#' @noRd
#'
partition_scatterplot <- function(
partition_table, palette, group_by = "Category"
partition_table, palette, group_by = "Category",
labels = c("P1", "F1", "P2")
) {

p_scatter <- ggplot(
Expand All @@ -203,15 +207,15 @@ partition_scatterplot <- function(
theme_bw() +
labs(
subtitle = expression(Log[2] ~ "fold change between generations"),
x = "F1 vs P1",
y = "F1 vs P2"
x = paste0(labels[2], " vs ", labels[1]), # F1 vs P1
y = paste0(labels[2], " vs ", labels[3]) # F1 vs P2
) +
ggplot2::theme(
panel.grid = ggplot2::element_blank(),
axis.text = element_text(size = 11)
) +
geom_vline(xintercept = 0, linetype = "dashed") +
geom_hline(yintercept = 0, linetype = "dashed")
geom_hline(yintercept = 0, linetype = "dashed")

return(p_scatter)
}
Expand All @@ -227,6 +231,9 @@ partition_scatterplot <- function(
#' @param group_by Character indicating the name of the variable
#' in \strong{partition_table} to use to group genes. One of "Category" or
#' "Class". Default: "Category".
#' @param labels A character vector of length 3 indicating the labels to be
#' given for parent 1, offspring, and parent 2.
#' Default: \code{c("P1", "F1", "P2")}.
#'
#' @return A list of ggplot objects with line plots.
#'
Expand All @@ -236,7 +243,8 @@ partition_scatterplot <- function(
#' @noRd
#'
partition_lineplots <- function(
partition_table, palette, add_n = TRUE, group_by = "Category"
partition_table, palette, add_n = TRUE, group_by = "Category",
labels = c("P1", "F1", "P2")
) {

# Data frame of point coordinates for each group
Expand Down Expand Up @@ -286,7 +294,11 @@ partition_lineplots <- function(
axis.text.x = element_text(size = 11, face = "bold"),
axis.text.y = element_blank()
) +
ylim(c(0, 4))
ylim(c(0, 4)) +
scale_x_discrete(
breaks = c("P1", "F1", "P2"),
labels = labels
)

if(group_by == "Class") { p <- p + facet_wrap("Category") }

Expand Down
23 changes: 18 additions & 5 deletions R/visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ plot_expression_triangle <- function(
#' If \strong{group_by = "Category"}, this must be a vector of length 12.
#' If \strong{group_by = "Class"}, this must be a vector of length 5.
#' If NULL, a default color palette will be used.
#' @param labels A character vector of length 3 indicating the labels to be
#' given for parent 1, offspring, and parent 2.
#' Default: \code{c("P1", "F1", "P2")}.
#'
#' @return A ggplot object with a plot showing genes in each expression
#' partition.
Expand All @@ -108,7 +111,8 @@ plot_expression_triangle <- function(
#' partition_table <- expression_partitioning(deg_list)
#' plot_expression_partitions(partition_table)
plot_expression_partitions <- function(
partition_table, group_by = "Category", palette = NULL
partition_table, group_by = "Category", palette = NULL,
labels = c("P1", "F1", "P2")
) {

pdata <- partition_table
Expand All @@ -118,10 +122,14 @@ plot_expression_partitions <- function(
names(pal) <- levels(pdata[[group_by]])

# 1) Create scatterplot
p_scatter <- partition_scatterplot(pdata, pal, group_by = group_by)
p_scatter <- partition_scatterplot(
pdata, pal, group_by = group_by, labels = labels
)

# 2) Create line plots to represent partitions
p_line <- partition_lineplots(pdata, pal, group_by = group_by)
p_line <- partition_lineplots(
pdata, pal, group_by = group_by, labels = labels
)

# Combine figures
if(group_by == "Class") {
Expand Down Expand Up @@ -164,6 +172,9 @@ plot_expression_partitions <- function(
#' If \strong{group_by = "Category"}, this must be a vector of length 12.
#' If \strong{group_by = "Class"}, this must be a vector of length 5.
#' If NULL, a default color palette will be used.
#' @param labels A character vector of length 3 indicating the labels to be
#' given for parent 1, offspring, and parent 2.
#' Default: \code{c("P1", "F1", "P2")}.
#'
#' @return A ggplot object with a barplot showing gene frequencies per
#' partition next to explanatory line plots depicting each partition.
Expand All @@ -177,7 +188,8 @@ plot_expression_partitions <- function(
#' partition_table <- expression_partitioning(deg_list)
#' plot_partition_frequencies(partition_table)
plot_partition_frequencies <- function(
partition_table, group_by = "Category", palette = NULL
partition_table, group_by = "Category", palette = NULL,
labels = c("P1", "F1", "P2")
) {

# Define color palette
Expand Down Expand Up @@ -209,7 +221,8 @@ plot_partition_frequencies <- function(

# Get line plots
p_line <- partition_lineplots(
partition_table, pal, add_n = FALSE, group_by = group_by
partition_table, pal, add_n = FALSE, group_by = group_by,
labels = labels
)

# Combine plots
Expand Down
7 changes: 6 additions & 1 deletion man/plot_expression_partitions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/plot_partition_frequencies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87e8868

Please sign in to comment.