From 572be88053c40adbae7b4aff8549cefb7e5b72e0 Mon Sep 17 00:00:00 2001 From: Jake Conway Date: Wed, 8 Aug 2018 15:55:36 -0400 Subject: [PATCH] Fix size bar number positioning and give leverage over set size scale maximum --- R/SizeBar.R | 36 +++++++++++++++++++++++++++++------- R/upset.R | 10 +++++++--- man/upset.Rd | 38 ++++++++++++++++++++++---------------- 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/R/SizeBar.R b/R/SizeBar.R index 77f7ea0..62c8a5b 100644 --- a/R/SizeBar.R +++ b/R/SizeBar.R @@ -32,7 +32,8 @@ log2_reverse_trans <- function(){ } ## Generate set size plot -Make_size_plot <- function(Set_size_data, sbar_color, ratios, ylabel, scale_sets, text_scale, set_size_angle,set_size.show){ +Make_size_plot <- function(Set_size_data, sbar_color, ratios, ylabel, scale_sets, text_scale, set_size_angle, set_size.show, set_size.scale_max, + set_size.number_size){ # if(ratios[1] < 0.4){ # m <- (-0.05) # } @@ -62,16 +63,22 @@ Make_size_plot <- function(Set_size_data, sbar_color, ratios, ylabel, scale_sets } } + if(!is.null(set_size.number_size)) { + num.size <- (set_size.number_size/ggplot2:::.pt)*x_axis_tick_label_scale + } else { + num.size <- (7/ggplot2:::.pt)*x_axis_tick_label_scale + } + Size_plot <- (ggplot(data = Set_size_data, aes_string(x ="x", y = "y")) + geom_bar(stat = "identity",colour = sbar_color, width = 0.4, fill = sbar_color, position = "identity") - + scale_x_continuous(limits = c(0.5, (nrow(Set_size_data)+0.5)), + + scale_x_continuous(limits = c(0.5, (nrow(Set_size_data) + 0.5)), breaks = c(0, max(Set_size_data)), expand = c(0,0)) + theme(panel.background = element_rect(fill = "white"), plot.margin=unit(c(-0.11,-1.3,0.5,0.5), "lines"), axis.title.x = element_text(size = 8.3*x_axis_title_scale), - axis.text.x = element_text(size = 7*x_axis_tick_label_scale, angle = set_size_angle, + axis.text.x = element_text(size = 7*x_axis_tick_label_scale, vjust = 1, hjust = 0.5), axis.line = element_line(colour = "gray0"), axis.line.y = element_blank(), @@ -84,17 +91,32 @@ Make_size_plot <- function(Set_size_data, sbar_color, ratios, ylabel, scale_sets + coord_flip()) if(set_size.show == TRUE){ - Size_plot <- (Size_plot + geom_text(aes(label=y,vjust=0.5,hjust=0.1),size=(7/ggplot2:::.pt)*x_axis_tick_label_scale)) + Size_plot <- (Size_plot + geom_text(aes(label=y,vjust=0.5,hjust=1.2, angle = set_size_angle), size=num.size)) } if(scale_sets == "log10"){ - Size_plot <- (Size_plot + scale_y_continuous(trans = log10_reverse_trans())) + if(!is.null(set_size.scale_max)) { + Size_plot <- (Size_plot + scale_y_continuous(limits = c(set_size.scale_max, 0), + trans = log10_reverse_trans())) + } else { + Size_plot <- (Size_plot + scale_y_continuous(trans = log10_reverse_trans())) + } } else if (scale_sets == "log2"){ - Size_plot <- (Size_plot + scale_y_continuous(trans = log2_reverse_trans())) + if(!is.null(set_size.scale_max)) { + Size_plot <- (Size_plot + scale_y_continuous(limits = c(set_size.scale_max, 0), + trans = log2_reverse_trans())) + } else { + Size_plot <- (Size_plot + scale_y_continuous(trans = log2_reverse_trans())) + } } else{ - Size_plot <- (Size_plot + scale_y_continuous(trans = "reverse")) + if(!is.null(set_size.scale_max)) { + Size_plot <- (Size_plot + scale_y_continuous(limits = c(set_size.scale_max, 0), + trans = "reverse")) + } else { + Size_plot <- (Size_plot + scale_y_continuous(trans = "reverse")) + } } Size_plot <- ggplot_gtable(ggplot_build(Size_plot)) diff --git a/R/upset.R b/R/upset.R index a8d373d..40cba8d 100644 --- a/R/upset.R +++ b/R/upset.R @@ -53,6 +53,8 @@ #' in the following format: c(intersection size title, intersection size tick labels, set size title, set size tick labels, set names, numbers above bars) #' @param set_size.angles Numeric, angle to rotate the set size plot x-axis text #' @param set_size.show Logical, display the set sizes on the set size bar chart +#' @param set_size.numbers_size If set_size.show is TRUE, adjust the size of the numbers +#' @param set_size.scale_max Increase the maximum of set size scale #' @details Visualization of set data in the layout described by Lex and Gehlenborg in \url{http://www.nature.com/nmeth/journal/v11/n8/abs/nmeth.3033.html}. #' UpSet also allows for visualization of queries on intersections and elements, along with custom queries queries implemented using #' Hadley Wickham's apply function. To further analyze the data contained in the intersections, the user may select additional attribute plots @@ -120,7 +122,7 @@ upset <- function(data, nsets = 5, nintersects = 40, sets = NULL, keep.order = F decreasing = c(T, F), show.numbers = "yes", number.angles = 0, group.by = "degree",cutoff = NULL, queries = NULL, query.legend = "none", shade.color = "gray88", shade.alpha = 0.25, matrix.dot.alpha =0.5, empty.intersections = NULL, color.pal = 1, boxplot.summary = NULL, attribute.plots = NULL, scale.intersections = "identity", - scale.sets = "identity", text.scale = 1, set_size.angles = 0 , set_size.show = FALSE ){ + scale.sets = "identity", text.scale = 1, set_size.angles = 0 , set_size.show = FALSE, set_size.numbers_size = NULL, set_size.scale_max = NULL){ startend <-FindStartEnd(data) first.col <- startend[1] @@ -257,8 +259,9 @@ upset <- function(data, nsets = 5, nintersects = 40, sets = NULL, keep.order = F mainbar.y.max, scale.intersections, text.scale, attribute.plots)) Matrix <- Make_matrix_plot(Matrix_layout, Set_sizes, All_Freqs, point.size, line.size, text.scale, labels, ShadingData, shade.alpha) - Sizes <- Make_size_plot(Set_sizes, sets.bar.color, mb.ratio, sets.x.label, scale.sets, text.scale, set_size.angles,set_size.show) - + Sizes <- Make_size_plot(Set_sizes, sets.bar.color, mb.ratio, sets.x.label, scale.sets, text.scale, set_size.angles,set_size.show, + set_size.scale_max, set_size.numbers_size) + # Make_base_plot(Main_bar, Matrix, Sizes, labels, mb.ratio, att.x, att.y, New_data, # expression, att.pos, first.col, att.color, AllQueryData, attribute.plots, # legend, query.legend, BoxPlots, Set_names, set.metadata, set.metadata.plots) @@ -287,6 +290,7 @@ upset <- function(data, nsets = 5, nintersects = 40, sets = NULL, keep.order = F set.metadata.plots = set.metadata.plots) ) } + #' @export print.upset <- function(x, newpage = TRUE) { Make_base_plot( diff --git a/man/upset.Rd b/man/upset.Rd index 6a781e3..34b8598 100644 --- a/man/upset.Rd +++ b/man/upset.Rd @@ -4,20 +4,22 @@ \alias{upset} \title{UpSetR Plot} \usage{ -upset(data, nsets = 5, nintersects = 40, sets = NULL, keep.order = F, - set.metadata = NULL, intersections = NULL, matrix.color = "gray23", - main.bar.color = "gray23", mainbar.y.label = "Intersection Size", - mainbar.y.max = NULL, sets.bar.color = "gray23", - sets.x.label = "Set Size", point.size = 2.2, line.size = 0.7, - mb.ratio = c(0.7, 0.3), expression = NULL, att.pos = NULL, - att.color = main.bar.color, order.by = c("freq", "degree"), - decreasing = c(T, F), show.numbers = "yes", number.angles = 0, - group.by = "degree", cutoff = NULL, queries = NULL, - query.legend = "none", shade.color = "gray88", shade.alpha = 0.25, - matrix.dot.alpha = 0.5, empty.intersections = NULL, color.pal = 1, - boxplot.summary = NULL, attribute.plots = NULL, - scale.intersections = "identity", scale.sets = "identity", - text.scale = 1, set_size.angles = 0, set_size.show = FALSE) +upset(data, nsets = 5, nintersects = 40, sets = NULL, + keep.order = F, set.metadata = NULL, intersections = NULL, + matrix.color = "gray23", main.bar.color = "gray23", + mainbar.y.label = "Intersection Size", mainbar.y.max = NULL, + sets.bar.color = "gray23", sets.x.label = "Set Size", + point.size = 2.2, line.size = 0.7, mb.ratio = c(0.7, 0.3), + expression = NULL, att.pos = NULL, att.color = main.bar.color, + order.by = c("freq", "degree"), decreasing = c(T, F), + show.numbers = "yes", number.angles = 0, group.by = "degree", + cutoff = NULL, queries = NULL, query.legend = "none", + shade.color = "gray88", shade.alpha = 0.25, matrix.dot.alpha = 0.5, + empty.intersections = NULL, color.pal = 1, boxplot.summary = NULL, + attribute.plots = NULL, scale.intersections = "identity", + scale.sets = "identity", text.scale = 1, set_size.angles = 0, + set_size.show = FALSE, set_size.numbers_size = NULL, + set_size.scale_max = NULL) } \arguments{ \item{data}{Data set} @@ -30,7 +32,7 @@ upset(data, nsets = 5, nintersects = 40, sets = NULL, keep.order = F, \item{keep.order}{Keep sets in the order entered using the sets parameter. The default is FALSE, which orders the sets by their sizes.} -\item{set.metadata}{Metadata that offers insight to an attribute of the sets. Input should be a data frame where the first column is set names, and the +\item{set.metadata}{Metadata that offers insight to an attribute of the sets. Input should be a data frame where the first column is set names, and the remaining columns are attributes of those sets. To learn how to use this parameter it is highly suggested to view the set metadata vignette. The link can be found on the package's GitHub page.} @@ -110,6 +112,10 @@ in the following format: c(intersection size title, intersection size tick label \item{set_size.angles}{Numeric, angle to rotate the set size plot x-axis text} \item{set_size.show}{Logical, display the set sizes on the set size bar chart} + +\item{set_size.numbers_size}{If set_size.show is TRUE, adjust the size of the numbers} + +\item{set_size.scale_max}{Increase the maximum of set size scale} } \description{ Visualization of set intersections using novel UpSet matrix design. @@ -165,7 +171,7 @@ upset(movies, attribute.plots = attributeplots, list(query = intersects, params = list("Drama"), color= "red"), list(query = elements, params = list("ReleaseDate", 1990, 1991, 1992))), main.bar.color = "yellow") - + } \references{ Lex et al. (2014). UpSet: Visualization of Intersecting Sets