Skip to content

Commit

Permalink
scale pies and add legend in emapplot, #304
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangchuangYu committed Dec 12, 2024
1 parent e636259 commit ad6409c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6485-8781")),
person(given = "Chun-Hui", family = "Gao", email = "[email protected]", role = "ctb", comment = c(ORCID = "0000-0002-1445-7939")))
Expand Down
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
14 changes: 10 additions & 4 deletions R/cnetplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
42 changes: 21 additions & 21 deletions R/emapplot_utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# ##'
Expand Down

0 comments on commit ad6409c

Please sign in to comment.