Skip to content

Commit

Permalink
add a new argument 'tick_length' in Legend()
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Feb 8, 2023
1 parent 7f95b79 commit 4ce0e3b
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 46 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CHANGES in VERSION 2.15.1
* `Legend()`: allows `NA` in `pch`.
* `SingleAnnotation()`: correctly calculate the max width/height of a vector of texts.
* `to_unit()`: fixed a bug when the unit is negative.
* `Legend()`: add `tick_length` argument.

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

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('prepare', function(object, ...) standardGeneric('prepare'))
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('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
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('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('draw', function(object, ...) standardGeneric('draw'))
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'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
8 changes: 5 additions & 3 deletions R/ColorMapping-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ setMethod(f = "map_to_colors",
# -title_position Position of the title. See `Legend` for all possible values.
# -grid_height Height of each legend grid. Pass to `Legend`.
# -grid_width Width of each legend grid. Pass to `Legend`.
# -tick_length Length of the ticks on the continuous legends. Value should be a `grid::unit` object.
# -border Color for legend grid borders. Pass to `Legend`.
# -at Break values of the legend. By default it is the levels in the `ColorMapping-class` object.
# -labels Labels corresponding to break values.
Expand Down Expand Up @@ -290,6 +291,7 @@ setMethod(f = "color_mapping_legend",
title_position = "topleft",
grid_height = unit(4, "mm"),
grid_width = unit(4, "mm"),
tick_length = unit(0.8, "mm"),
border = NULL,
at = object@levels,
labels = at,
Expand Down Expand Up @@ -355,17 +357,17 @@ setMethod(f = "color_mapping_legend",
}
if(length(at) == 0) {
gf = Legend(at = "NA", labels = "NA", name = object@name, title = title, title_gp = title_gp, grid_height = grid_height,
grid_width = grid_width, border = border, labels_gp = labels_gp, direction = legend_direction, nrow = nrow, ncol = ncol,
grid_width = grid_width, tick_length = tick_length, border = border, labels_gp = labels_gp, direction = legend_direction, nrow = nrow, ncol = ncol,
legend_gp = gpar(fill = object@na_col), title_position = title_position, by_row = by_row, graphics = graphics, break_dist = break_dist)
} else {
gf = Legend(at = at, labels = labels, name = object@name, title = title, title_gp = title_gp, grid_height = grid_height,
grid_width = grid_width, border = border, labels_gp = labels_gp, direction = legend_direction, nrow = nrow, ncol = ncol,
grid_width = grid_width, tick_length = tick_length, border = border, labels_gp = labels_gp, direction = legend_direction, nrow = nrow, ncol = ncol,
legend_gp = gpar(fill = map_to_colors(object, at)), title_position = title_position, by_row = by_row, graphics = graphics, break_dist = break_dist)
}

} else {
gf = Legend(at = at, labels = labels, name = object@name, col_fun = object@col_fun, title = title, title_gp = title_gp, grid_height = grid_height,
grid_width = grid_width, border = border, labels_gp = labels_gp, labels_rot = labels_rot, direction = legend_direction,
grid_width = grid_width, tick_length = tick_length, border = border, labels_gp = labels_gp, labels_rot = labels_rot, direction = legend_direction,
legend_width = legend_width, legend_height = legend_height, title_position = title_position, by_row = by_row, break_dist = break_dist)

}
Expand Down
20 changes: 11 additions & 9 deletions R/grid.Legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Legends = function(...) {
# -by_row Are the legend grids arranged by rows or by columns?
# -grid_height The height of legend grid. It can also control the height of the continuous legend if it is horizontal.
# -grid_width The width of legend grid. It can also control the width of the continuous legend if it is vertical.
# -tick_length Length of the ticks on the continuous legends. Value should be a `grid::unit` object.
# -gap If legend grids are put into multiple rows or columns, this controls the gap between neighbouring rows or columns, measured as a `grid::unit` object.
# -column_gap The same as ``gap``.
# -row_gap Space between legend rows.
Expand Down Expand Up @@ -114,7 +115,7 @@ Legends = function(...) {
Legend = function(at, labels = at, col_fun, name = NULL, grob = NULL,
break_dist = NULL, nrow = NULL, ncol = 1, by_row = FALSE,
grid_height = unit(4, "mm"),
grid_width = unit(4, "mm"),
grid_width = unit(4, "mm"), tick_length = unit(0.8, "mm"),
gap = unit(2, "mm"), column_gap = gap, row_gap = unit(0, "mm"),
labels_gp = gpar(fontsize = 10), labels_rot = 0,
border = NULL, background = "#EEEEEE",
Expand Down Expand Up @@ -174,7 +175,7 @@ Legend = function(at, labels = at, col_fun, name = NULL, grob = NULL,

if(direction == "vertical") {
legend_body = vertical_continuous_legend_body(at = at, labels = labels, col_fun = col_fun, break_dist = break_dist,
grid_height = grid_height, grid_width = grid_width, legend_height = legend_height,
grid_height = grid_height, grid_width = grid_width, tick_length = tick_length, legend_height = legend_height,
labels_gp = labels_gp, border = border)
} else {
legend_extension = unit(0, "mm")
Expand All @@ -186,7 +187,7 @@ Legend = function(at, labels = at, col_fun, name = NULL, grob = NULL,
}
}
legend_body = horizontal_continuous_legend_body(at = at, labels = labels, col_fun = col_fun, break_dist = break_dist,
grid_height = grid_height, grid_width = grid_width, legend_width = legend_width,
grid_height = grid_height, grid_width = grid_width, tick_length = tick_length, legend_width = legend_width,
labels_gp = labels_gp, labels_rot = labels_rot, border = border, legend_extension = legend_extension)
}
}
Expand Down Expand Up @@ -605,7 +606,7 @@ discrete_legend_body = function(at, labels = at, nrow = NULL, ncol = 1, by_row =

vertical_continuous_legend_body = function(at, labels = at, col_fun,
break_dist = NULL, grid_height = unit(4, "mm"), grid_width = unit(4, "mm"),
legend_height = NULL,
legend_height = NULL, tick_length = unit(0.8, "mm"),
labels_gp = gpar(fontsize = 10),
border = NULL) {

Expand Down Expand Up @@ -769,11 +770,12 @@ vertical_continuous_legend_body = function(at, labels = at, col_fun,
y2 = unit.c(offset*0.5, y2, legend_body_height - offset*0.5)
hh = unit.c(offset, rep(hh, length(y2)-2), offset)
colors = c(colors[1], colors, colors[length(colors)])

gl = c(gl, list(
rectGrob(x2, rev(y2), width = grid_width, height = hh, just = c("left", "center"),
gp = gpar(col = rev(colors), fill = rev(colors))),
segmentsGrob(unit(0, "npc"), y, unit(0.8, "mm"), y, gp = gpar(col = ifelse(is.null(border), "white", border))),
segmentsGrob(grid_width, y, grid_width - unit(0.8, "mm"), y, gp = gpar(col = ifelse(is.null(border), "white", border)))
segmentsGrob(unit(0, "npc"), y, tick_length, y, gp = gpar(col = ifelse(is.null(border), "white", border))),
segmentsGrob(grid_width, y, grid_width - tick_length, y, gp = gpar(col = ifelse(is.null(border), "white", border)))
))

if(adjust_text_pos) {
Expand Down Expand Up @@ -815,7 +817,7 @@ vertical_continuous_legend_body = function(at, labels = at, col_fun,

horizontal_continuous_legend_body = function(at, labels = at, col_fun,
break_dist = NULL, grid_height = unit(4, "mm"), grid_width = unit(4, "mm"),
legend_width = NULL,
legend_width = NULL, tick_length = unit(0.8, "mm"),
labels_gp = gpar(fontsize = 10), labels_rot = 0,
border = NULL, legend_extension = unit(0, "mm")) {

Expand Down Expand Up @@ -978,8 +980,8 @@ horizontal_continuous_legend_body = function(at, labels = at, col_fun,
gl = c(gl, list(
rectGrob(x2, y2, height = grid_height, width = ww, just = c("left", "top"),
gp = gpar(col = colors, fill = colors)),
segmentsGrob(x, legend_body_height - grid_height, x, legend_body_height - grid_height + unit(0.8, "mm"), gp = gpar(col = ifelse(is.null(border), "white", border))),
segmentsGrob(x, legend_body_height - unit(0.8, "mm"), x, legend_body_height, gp = gpar(col = ifelse(is.null(border), "white", border)))
segmentsGrob(x, legend_body_height - grid_height, x, legend_body_height - grid_height + tick_length, gp = gpar(col = ifelse(is.null(border), "white", border))),
segmentsGrob(x, legend_body_height - tick_length, x, legend_body_height, gp = gpar(col = ifelse(is.null(border), "white", border)))
))

if(adjust_text_pos) {
Expand Down
3 changes: 2 additions & 1 deletion man/Legend.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Make a Single Legend
Legend(at, labels = at, col_fun, name = NULL, grob = NULL,
break_dist = NULL, nrow = NULL, ncol = 1, by_row = FALSE,
grid_height = unit(4, "mm"),
grid_width = unit(4, "mm"),
grid_width = unit(4, "mm"), tick_length = unit(0.8, "mm"),
gap = unit(2, "mm"), column_gap = gap, row_gap = unit(0, "mm"),
labels_gp = gpar(fontsize = 10), labels_rot = 0,
border = NULL, background = "#EEEEEE",
Expand All @@ -35,6 +35,7 @@ Legend(at, labels = at, col_fun, name = NULL, grob = NULL,
\item{by_row}{Are the legend grids arranged by rows or by columns?}
\item{grid_height}{The height of legend grid. It can also control the height of the continuous legend if it is horizontal.}
\item{grid_width}{The width of legend grid. It can also control the width of the continuous legend if it is vertical.}
\item{tick_length}{Length of the ticks on the continuous legends. Value should be a \code{\link[grid]{unit}} object.}
\item{gap}{If legend grids are put into multiple rows or columns, this controls the gap between neighbouring rows or columns, measured as a \code{\link[grid]{unit}} object.}
\item{column_gap}{The same as \code{gap}.}
\item{row_gap}{Space between legend rows.}
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 @@ -11,8 +11,8 @@ Method dispatch page for \code{add_heatmap}.

\itemize{
\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
\item \code{\link{add_heatmap,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down
2 changes: 2 additions & 0 deletions man/color_mapping_legend-ColorMapping-method.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Draw Legend Based on Color Mapping
title_position = "topleft",
grid_height = unit(4, "mm"),
grid_width = unit(4, "mm"),
tick_length = unit(0.8, "mm"),
border = NULL,
at = object@levels,
labels = at,
Expand Down Expand Up @@ -45,6 +46,7 @@ Draw Legend Based on Color Mapping
\item{title_position}{Position of the title. See \code{\link{Legend}} for all possible values.}
\item{grid_height}{Height of each legend grid. Pass to \code{\link{Legend}}.}
\item{grid_width}{Width of each legend grid. Pass to \code{\link{Legend}}.}
\item{tick_length}{Length of the ticks on the continuous legends. Value should be a \code{\link[grid]{unit}} object.}
\item{border}{Color for legend grid borders. Pass to \code{\link{Legend}}.}
\item{at}{Break values of the legend. By default it is the levels in the \code{\link{ColorMapping-class}} object.}
\item{labels}{Labels corresponding to break values.}
Expand Down
2 changes: 1 addition & 1 deletion man/column_order-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{column_order}.
\code{column_order} can be dispatched on following classes:

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

\itemize{
\item \code{\link{component_width,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{component_width,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{component_width,Heatmap-method}}, \code{\link{Heatmap-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,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\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
}
}
\examples{
Expand Down
4 changes: 2 additions & 2 deletions man/draw-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Method dispatch page for \code{draw}.
\code{draw} can be dispatched on following classes:

\itemize{
\item \code{\link{draw,Legends-method}}, \code{\link{Legends-class}} class method
\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-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,Legends-method}}, \code{\link{Legends-class}} class method
\item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
Expand Down
6 changes: 3 additions & 3 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,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-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,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down

0 comments on commit 4ce0e3b

Please sign in to comment.