From ad6409cec139fff84a40d81b4ee80582790147ae Mon Sep 17 00:00:00 2001 From: Guangchuang Yu Date: Thu, 12 Dec 2024 17:42:56 +0800 Subject: [PATCH] scale pies and add legend in emapplot, #304 --- DESCRIPTION | 2 +- NEWS.md | 5 +++-- R/cnetplot.R | 14 ++++++++++---- R/emapplot_utilities.R | 42 +++++++++++++++++++++--------------------- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d0b6630..1bdbc66 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: enrichplot Title: Visualization of Functional Enrichment Result -Version: 1.27.2.001 +Version: 1.27.2.002 Authors@R: c( person(given = "Guangchuang", family = "Yu", email = "guangchuangyu@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6485-8781")), person(given = "Chun-Hui", family = "Gao", email = "gaospecial@gmail.com", role = "ctb", comment = c(ORCID = "0000-0002-1445-7939"))) diff --git a/NEWS.md b/NEWS.md index a4b30f8..899d725 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ -# enrichplot 1.27.2.001 +# enrichplot 1.27.2.002 -+ a safe way to extract gene sets in `ridgeplot()` (2024-12-12, Thu) ++ scale pies and add pie legend in `emapplot()` (2024-12-12, Thu, #304) ++ a safe way to extract gene sets in `ridgeplot()` (2024-12-12, Thu, #303) # enrichplot 1.27.2 diff --git a/R/cnetplot.R b/R/cnetplot.R index d76a0f4..00b38c0 100644 --- a/R/cnetplot.R +++ b/R/cnetplot.R @@ -107,14 +107,20 @@ cnetplot.compareClusterResult <- function( #' @importFrom ggplot2 coord_fixed add_node_pie <- function(p, d, pie = "equal", pie_scale = 1) { dd <- d[,c('Cluster', 'Description', 'Count')] + pathway_size <- sapply(split(dd$Count, dd$Description), sum) if (pie == "equal") dd$Count <- 1 - dd <- tidyr::pivot_wider(dd, names_from=.data$Cluster, values_from=.data$Count, values_fill=0) - + dd <- tidyr::pivot_wider(dd, names_from="Cluster", values_from="Count", values_fill=0) + dd$pathway_size <- sqrt(pathway_size[dd$Description]/sum(pathway_size)) + + p <- p %<+% dd + - scatterpie::geom_scatterpie(cols=as.character(unique(d$Cluster)), + scatterpie::geom_scatterpie(aes(x=.data$x, y=.data$y, r=.data$pathway_size), + cols=as.character(unique(d$Cluster)), legend_name = "Cluster", color=NA, pie_scale = pie_scale) + + scatterpie::geom_scatterpie_legend(dd$pathway_size, x=min(p$data$x), y=min(p$data$y), n=3, + labeller=function(x) round(sum(pathway_size) * x^2)) + coord_fixed() + - guides(size = "none") + guides(size = "none") return(p) } diff --git a/R/emapplot_utilities.R b/R/emapplot_utilities.R index 2145235..c29052a 100644 --- a/R/emapplot_utilities.R +++ b/R/emapplot_utilities.R @@ -557,27 +557,27 @@ add_ellipse <- function(p, group_legend, label_style, -##' Get data for pie plot -##' -##' @param enrichDf A data.frame of enrichment result. -##' @param pie proportion of clusters in the pie chart, one of 'equal' (default) and 'Count' -##' @param mergedEnrichDf A data.frame of merged enrichment result. -##' @param cex_pie2axis It is used to adjust the relative size of the pie chart on the coordinate axis. -##' @param p a ggraph object. -##' @param cex_category Number indicating the amount by which plotting category -##' nodes should be scaled relative to the default. -##' @noRd -get_pie_data <- function(enrichDf, pie, mergedEnrichDf, cex_pie2axis, p, cex_category) { - ggData <- p$data - ID_Cluster_mat <- prepare_pie_category(enrichDf = enrichDf, pie=pie) - desc <- mergedEnrichDf$Description[match(rownames(ID_Cluster_mat), - mergedEnrichDf$Description)] - i <- match(desc, ggData$name) - ID_Cluster_mat$x <- ggData$x[i] - ID_Cluster_mat$y <- ggData$y[i] - ID_Cluster_mat$radius <- sqrt(ggData$size[i] / sum(ggData$size) * cex_category * cex_pie2axis) - return(ID_Cluster_mat) -} +# ##' Get data for pie plot +# ##' +# ##' @param enrichDf A data.frame of enrichment result. +# ##' @param pie proportion of clusters in the pie chart, one of 'equal' (default) and 'Count' +# ##' @param mergedEnrichDf A data.frame of merged enrichment result. +# ##' @param cex_pie2axis It is used to adjust the relative size of the pie chart on the coordinate axis. +# ##' @param p a ggraph object. +# ##' @param cex_category Number indicating the amount by which plotting category +# ##' nodes should be scaled relative to the default. +# ##' @noRd +# get_pie_data <- function(enrichDf, pie, mergedEnrichDf, cex_pie2axis=1, p, cex_category=1) { +# ggData <- p$data +# ID_Cluster_mat <- prepare_pie_category(enrichDf = enrichDf, pie=pie) +# desc <- mergedEnrichDf$Description[match(rownames(ID_Cluster_mat), +# mergedEnrichDf$Description)] +# i <- match(desc, ggData$name) +# ID_Cluster_mat$x <- ggData$x[i] +# ID_Cluster_mat$y <- ggData$y[i] +# ID_Cluster_mat$radius <- sqrt(ggData$size[i] / sum(ggData$size) * cex_category * cex_pie2axis) +# return(ID_Cluster_mat) +# } # ##' Add category node(pie plot) # ##'