Skip to content

Commit

Permalink
add a new add_points argument in anno_boxplot()
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Oct 27, 2022
1 parent 15a7586 commit 8e8f4ee
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 46 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CHANGES in VERSION 2.13.4

* `anno_barplot()`: fixed a bug when split is set, the bars are wrongly plotted under besides = TRUE.
* `anno_boxplot()`: add two new argumetn: `add_points` and `pt_gp`.

========================

Expand Down
46 changes: 23 additions & 23 deletions R/00_S4_generic_methods.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
setGeneric('draw', function(object, ...) standardGeneric('draw'))
setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('draw', function(object, ...) standardGeneric('draw'))
setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
39 changes: 30 additions & 9 deletions R/AnnotationFunction-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -1561,8 +1561,10 @@ anno_barplot = function(x, baseline = 0, which = c("column", "row"), border = TR
# -extend The extension to both side of ``ylim``. The value is a percent value corresponding to ``ylim[2] - ylim[1]``.
# -outline Whether draw outline of boxplots?
# -box_width Relative width of boxes. The value should be smaller than one.
# -add_points Whether add points on top of the boxes?
# -pch Point style.
# -size Point size.
# -pt_gp Graphics parameters for points.
# -axis Whether to add axis?
# -axis_param parameters for controlling axis. See `default_axis_param` for all possible settings and default parameters.
# -width Width of the annotation. The value should be an absolute unit. Width is not allowed to be set for column annotation.
Expand All @@ -1584,8 +1586,8 @@ anno_barplot = function(x, baseline = 0, which = c("column", "row"), border = TR
# draw(anno, test = "anno_boxplot with gp")
anno_boxplot = function(x, which = c("column", "row"), border = TRUE,
gp = gpar(fill = "#CCCCCC"), ylim = NULL, extend = 0.05, outline = TRUE, box_width = 0.6,
pch = 1, size = unit(2, "mm"), axis = TRUE, axis_param = default_axis_param(which),
width = NULL, height = NULL, ...) {
add_points = FALSE, pch = 16, size = unit(4, "pt"), pt_gp = gpar(), axis = TRUE,
axis_param = default_axis_param(which), width = NULL, height = NULL, ...) {

other_args = list(...)
if(length(other_args)) {
Expand Down Expand Up @@ -1639,6 +1641,7 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,

n = length(value)
gp = recycle_gp(gp, n)
pt_gp = recycle_gp(pt_gp, n)
if(length(pch) == 1) pch = rep(pch, n)
if(length(size) == 1) size = rep(size, n)

Expand All @@ -1657,6 +1660,7 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,

n = length(index)
gp = subset_gp(gp, index)
pt_gp = subset_gp(pt_gp, index)
pch = pch[index]
size = size[index]
pushViewport(viewport(xscale = data_scale, yscale = c(0.5, n+0.5)))
Expand All @@ -1680,18 +1684,25 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,
grid.segments(boxplot_stats[3, ], n - seq_along(index) + 1 - 0.5*box_width,
boxplot_stats[3, ], n - seq_along(index) + 1 + 0.5*box_width,
default.units = "native", gp = gp)
if(outline) {
if(!add_points && outline) {
for(i in seq_along(value)) {
l1 = value[[i]] > boxplot_stats[5,i]
l1[is.na(l1)] = FALSE
if(sum(l1)) grid.points(y = rep(n - i + 1, sum(l1)), x = value[[i]][l1],
default.units = "native", gp = subset_gp(gp, i), pch = pch[i], size = size[i])
default.units = "native", gp = subset_gp(pt_gp, i), pch = pch[i], size = size[i])
l2 = value[[i]] < boxplot_stats[1,i]
l2[is.na(l2)] = FALSE
if(sum(l2)) grid.points(y = rep(n - i + 1, sum(l2)), x = value[[i]][l2],
default.units = "native", gp = subset_gp(gp, i), pch = pch[i], size = size[i])
default.units = "native", gp = subset_gp(pt_gp, i), pch = pch[i], size = size[i])
}
}
if(add_points && outline) {
for(i in seq_along(value)) {
grid.points(y = n - runif(length(value[[i]]), min = i - 0.5*0.5*box_width, max = i + 0.5*0.5*box_width) + 1,
x = value[[i]], default.units = "native", gp = subset_gp(pt_gp, i), pch = pch[i], size = size[i])
}
}

if(axis_param$side == "top") {
if(k > 1) axis = FALSE
} else if(axis_param$side == "bottom") {
Expand All @@ -1712,6 +1723,7 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,

n = length(index)
gp = subset_gp(gp, index)
pt_gp = subset_gp(pt_gp, index)
pch = pch[index]
size = size[index]
pushViewport(viewport(xscale = c(0.5, n+0.5), yscale = data_scale))
Expand All @@ -1734,16 +1746,24 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,
grid.segments(seq_along(index) - 0.5*box_width, boxplot_stats[3, ],
seq_along(index) + 0.5*box_width, boxplot_stats[3, ],
default.units = "native", gp = gp)
if(outline) {

if(!add_points && outline) {
for(i in seq_along(value)) {
l1 = value[[i]] > boxplot_stats[5,i]
l1[is.na(l1)] = FALSE
if(sum(l1)) grid.points(x = rep(i, sum(l1)), y = value[[i]][l1],
default.units = "native", gp = subset_gp(gp, i), pch = pch[i], size = size[i])
default.units = "native", gp = subset_gp(pt_gp, i), pch = pch[i], size = size[i])
l2 = value[[i]] < boxplot_stats[1,i]
l2[is.na(l2)] = FALSE
if(sum(l2)) grid.points(x = rep(i, sum(l2)), y = value[[i]][l2],
default.units = "native", gp = subset_gp(gp, i), pch = pch[i], size = size[i])
default.units = "native", gp = subset_gp(pt_gp, i), pch = pch[i], size = size[i])
}
}

if(add_points && outline) {
for(i in seq_along(value)) {
grid.points(x = runif(length(value[[i]]), min = i - 0.5*0.5*box_width, max = i + 0.5*0.5*box_width),
y = value[[i]], default.units = "native", gp = subset_gp(pt_gp, i), pch = pch[i], size = size[i])
}
}
if(axis_param$side == "left") {
Expand All @@ -1770,11 +1790,12 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,
width = anno_size$width,
height = anno_size$height,
data_scale = data_scale,
var_import = list(value, gp, border, box_width, axis, axis_param, axis_grob, data_scale, pch, size, outline)
var_import = list(value, gp, border, box_width, axis, axis_param, axis_grob, data_scale, add_points, pch, pt_gp, size, outline)
)

anno@subset_rule$value = subset_vector
anno@subset_rule$gp = subset_gp
anno@subset_rule$pt_gp = subset_gp
anno@subset_rule$pch = subset_vector
anno@subset_rule$size = subset_vector

Expand Down
2 changes: 1 addition & 1 deletion man/add_heatmap-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Method dispatch page for \code{add_heatmap}.
\code{add_heatmap} can be dispatched on following classes:

\itemize{
\item \code{\link{add_heatmap,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{add_heatmap,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{add_heatmap,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{add_heatmap,Heatmap-method}}, \code{\link{Heatmap-class}} class method
}
}
\examples{
Expand Down
6 changes: 4 additions & 2 deletions man/anno_boxplot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Boxplot Annotation
\usage{
anno_boxplot(x, which = c("column", "row"), border = TRUE,
gp = gpar(fill = "#CCCCCC"), ylim = NULL, extend = 0.05, outline = TRUE, box_width = 0.6,
pch = 1, size = unit(2, "mm"), axis = TRUE, axis_param = default_axis_param(which),
width = NULL, height = NULL, ...)
add_points = FALSE, pch = 16, size = unit(4, "pt"), pt_gp = gpar(), axis = TRUE,
axis_param = default_axis_param(which), width = NULL, height = NULL, ...)
}
\arguments{

Expand All @@ -22,8 +22,10 @@ anno_boxplot(x, which = c("column", "row"), border = TRUE,
\item{extend}{The extension to both side of \code{ylim}. The value is a percent value corresponding to \code{ylim[2] - ylim[1]}.}
\item{outline}{Whether draw outline of boxplots?}
\item{box_width}{Relative width of boxes. The value should be smaller than one.}
\item{add_points}{Whether add points on top of the boxes?}
\item{pch}{Point style.}
\item{size}{Point size.}
\item{pt_gp}{Graphics parameters for points.}
\item{axis}{Whether to add axis?}
\item{axis_param}{parameters for controlling axis. See \code{\link{default_axis_param}} for all possible settings and default parameters.}
\item{width}{Width of the annotation. The value should be an absolute unit. Width is not allowed to be set for column annotation.}
Expand Down
2 changes: 1 addition & 1 deletion man/column_dend-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{column_dend}.
\code{column_dend} can be dispatched on following classes:

\itemize{
\item \code{\link{column_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{column_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{column_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/component_height-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{component_height}.
\code{component_height} can be dispatched on following classes:

\itemize{
\item \code{\link{component_height,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{component_height,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{component_height,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/copy_all-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{copy_all}.
\code{copy_all} can be dispatched on following classes:

\itemize{
\item \code{\link{copy_all,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{copy_all,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{copy_all,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
}
}
\examples{
Expand Down
6 changes: 3 additions & 3 deletions man/draw-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Method dispatch page for \code{draw}.
\code{draw} can be dispatched on following classes:

\itemize{
\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{draw,Legends-method}}, \code{\link{Legends-class}} class method
\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{draw,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{draw,Legends-method}}, \code{\link{Legends-class}} class method
\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/draw_title-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{draw_title}.
\code{draw_title} can be dispatched on following classes:

\itemize{
\item \code{\link{draw_title,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{draw_title,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{draw_title,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/make_layout-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{make_layout}.
\code{make_layout} can be dispatched on following classes:

\itemize{
\item \code{\link{make_layout,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{make_layout,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{make_layout,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
4 changes: 2 additions & 2 deletions man/show-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Method dispatch page for \code{show}.
\code{show} can be dispatched on following classes:

\itemize{
\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{show,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{show,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{show,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{show,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
}
}
\examples{
Expand Down
1 change: 0 additions & 1 deletion tests/test-AnnotationFunction.R
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ anno = anno_barplot(matrix(nc = 2, c(1:10, 10:1)), beside = TRUE, gp = gpar(fill
draw(anno, test = "a matrix with fill")



##### test anno_boxplot #####
set.seed(123)
m = matrix(rnorm(100), 10)
Expand Down

0 comments on commit 8e8f4ee

Please sign in to comment.