Skip to content

Commit

Permalink
upset plot non-specific items
Browse files Browse the repository at this point in the history
  • Loading branch information
gaospecial committed Feb 7, 2024
1 parent eaf9df9 commit 3fe7e84
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 20 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ggVennDiagram
Type: Package
Title: A 'ggplot2' Implement of Venn Diagram
Version: 1.5.1
Version: 1.5.2
Authors@R: c(
person("Chun-Hui","Gao", email="[email protected]", role=c("aut","cre"), comment=c(ORCID = "0000-0002-1445-7939")),
person("Guangchuang", "Yu", email = "[email protected]", role = c("ctb"), comment = c(ORCID = "0000-0002-6485-8781")),
Expand Down Expand Up @@ -41,6 +41,7 @@ Suggests:
plotly,
RColorBrewer,
shiny,
rmarkdown
rmarkdown,
tidyr
VignetteBuilder: knitr
LazyData: true
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog of ggVennDiagram

## ggVennDiagram 1.5.2

* Add a switch to show non-specific items in `plot_upset`. See #64.

## ggVennDiagram 1.5.1

* Add more param to `plot_upset()` function
Expand Down
18 changes: 13 additions & 5 deletions R/process_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ venn_region = function(obj){
#' @export
#'
plotData_add_venn = function(plotData, venn){
if (!all(c("setLabel","setEdge","regionLabel", "regionEdge") %in% names(plotData))){
if (!all(c("setLabel","setEdge","regionLabel", "regionEdge") %in% names(plotData))) {
stop("Invalid shape data.")
}
if (!inherits(venn, "Venn")) stop("venn should be a S4 Venn object.")
Expand Down Expand Up @@ -142,7 +142,7 @@ plotData_add_venn = function(plotData, venn){
#' process_set_data(venn)
#' process_region_data(venn)
process_set_data = function(venn){
if(!inherits(venn, "Venn")) stop("venn is not a S4 class 'Venn' object.")
if (!inherits(venn, "Venn")) stop("venn is not a S4 class 'Venn' object.")
tibble::tibble(
id = as.character(seq_along(venn@sets)),
name = venn@names,
Expand All @@ -152,10 +152,18 @@ process_set_data = function(venn){
}

#' @rdname venn_data
#' @param specific whether return ONLY specific items for a subset, default is TRUE
#' @details
#' ggVennDiagram, by default, only return the specific subsets of a region.
#' However, sometimes, we want to show all the overlapping items for two or more sets.
#' For example: https://github.com/gaospecial/ggVennDiagram/issues/64
#' Therefore, we add a 'specific' switch to this function. While 'specific = FALSE',
#' the seperator will be changed from "/" to "~", and all the overlapping items
#' will be returned. This feature is useful in plotting upset plot.
#' @export
process_region_data = function(venn, sep = "/"){
if(!inherits(venn, "Venn")) stop("venn is not a S4 class 'Venn' object.")
region_items = get_subset_items(venn)
process_region_data = function(venn, sep = "/", specific = TRUE) {
if (!inherits(venn, "Venn")) stop("venn is not a S4 class 'Venn' object.")
region_items = get_subset_items(venn, specific = specific)
counts = sapply(region_items, length)
region_ids = get_subset_ids(venn, sep = sep)
region_names = get_subset_names(venn, sep = sep)
Expand Down
5 changes: 3 additions & 2 deletions R/regions.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ matrix2list <- function(matrix){
lapply(seq_len(ncol(matrix)), function(i) matrix[,i])
}

get_subset_items <- function(venn){
get_subset_items <- function(venn, specific = TRUE){
n = length(venn@sets)
c = combinations(n)
lapply(c, function(i) discern_overlap(venn,i))
fun = ifelse(specific, "discern_overlap", "overlap")
lapply(c, fun, venn = venn)
}

get_subset_names <- function(venn, sep = "/"){
Expand Down
29 changes: 21 additions & 8 deletions R/upset_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#' @param sets.bar.show.numbers default is FALSE
#' @param sets.bar.x.label default is "Set Size"
#' @param intersection.matrix.color default is "grey30"
#' @param specific whether only include specific items in subsets, default is TRUE.
#' @param ... useless
#' @return an upset plot
#'
Expand Down Expand Up @@ -62,6 +63,7 @@ plot_upset = function(venn,
sets.bar.show.numbers = FALSE,
sets.bar.x.label = "Set Size",
intersection.matrix.color = "grey30",
specific = TRUE,
...){
# process arguments
order.intersect.by = match.arg(order.intersect.by)
Expand All @@ -71,7 +73,8 @@ plot_upset = function(venn,
data = process_upset_data(venn,
nintersects = nintersects,
order.intersect.by = order.intersect.by,
order.set.by = order.set.by)
order.set.by = order.set.by,
specific = specific)
p_main = upsetplot_main(data$main_data,
intersection.matrix.color = intersection.matrix.color)

Expand Down Expand Up @@ -183,21 +186,31 @@ theme_upset_left = function(){
#' process upset data
#'
#' @inheritParams upset-plot
#' @param name_separator will be used to assign subset names
#' @param specific whether return ONLY specific items for a subset, default is TRUE
#' @details
#' ggVennDiagram, by default, only return the specific subsets of a region.
#' However, sometimes, we want to show all the overlapping items for two or more sets.
#' For example: https://github.com/gaospecial/ggVennDiagram/issues/64
#' Therefore, we add a 'specific' switch to this function. While 'specific = FALSE',
#' the seperator will be changed from "/" to "~", and all the overlapping items
#' will be returned. This feature is useful in plotting upset plot.
#'
#' @return a upsetPlotData object
process_upset_data = function(venn,
nintersects = 30,
order.intersect.by = "size",
order.set.by = "name",
name_separator = "/"){
data = process_region_data(venn, sep = name_separator)
data$size = data$count
specific = TRUE){
set_name = venn@names
name_separator = ifelse(specific, "/", "~")

# region data
data = process_region_data(venn, sep = name_separator, specific = specific)
data$size = data$count

# top data
top_data = data |> dplyr::select(c('id', 'name', 'item', 'size'))
if (order.intersect.by %in% colnames(top_data)){
if (order.intersect.by %in% colnames(top_data)) {
top_data = dplyr::mutate(top_data,
id = forcats::fct_reorder(.data$id, .data[[order.intersect.by]], .desc = TRUE))
} else {
Expand All @@ -208,7 +221,7 @@ process_upset_data = function(venn,
left_data = dplyr::tibble(set = set_name,
name = set_name,
size = lengths(venn@sets))
if (order.set.by %in% colnames(left_data)){
if (order.set.by %in% colnames(left_data)) {
left_data = dplyr::mutate(left_data,
set = forcats::fct_reorder(.data$set, .data[[order.set.by]], .desc = TRUE))
} else {
Expand All @@ -224,7 +237,7 @@ process_upset_data = function(venn,
main_data$id = factor(main_data$id, levels = levels(top_data$id))

# filter intersections
if (is.numeric(nintersects)){
if (is.numeric(nintersects)) {
keep_id = utils::head(levels(top_data$id), nintersects)
main_data = main_data |> dplyr::filter(.data$id %in% keep_id)
top_data = top_data |> dplyr::filter(.data$id %in% keep_id)
Expand Down
12 changes: 10 additions & 2 deletions man/process_upset_data.Rd

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

3 changes: 3 additions & 0 deletions man/upset-plot.Rd

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

12 changes: 11 additions & 1 deletion man/venn_data.Rd

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

0 comments on commit 3fe7e84

Please sign in to comment.