diff --git a/R/AnnotationFunction-function.R b/R/AnnotationFunction-function.R index 3158e51..30536b0 100644 --- a/R/AnnotationFunction-function.R +++ b/R/AnnotationFunction-function.R @@ -1370,24 +1370,31 @@ anno_barplot = function(x, baseline = 0, which = c("column", "row"), border = TR value = x attr(value, "labels_format") = labels_format + number_extension = unit(0, "mm") + axis_param = validate_axis_param(axis_param, which) if(ncol(value) == 1) { if(add_numbers) { if(which == "column") { if(numbers_rot == 0) { - extend = convertHeight(max_text_height(value, gp = numbers_gp) + numbers_offset + unit(2, "mm"), "mm", valueOnly = TRUE)/convertHeight(anno_size$height, "mm", valueOnly = TRUE)*(data_scale[2] - data_scale[1]) + number_extension = max_text_height(value, gp = numbers_gp) + numbers_offset + unit(2, "mm") } else { - extend = convertHeight(sin(numbers_rot/180*pi)*max_text_width(value, gp = numbers_gp) + numbers_offset + unit(4, "mm"), "mm", valueOnly = TRUE)/convertHeight(anno_size$height, "mm", valueOnly = TRUE)*(data_scale[2] - data_scale[1]) + number_extension = sin(numbers_rot/180*pi)*max_text_width(value, gp = numbers_gp) + numbers_offset + unit(4, "mm") } - data_scale[2] = data_scale[2] + extend } else if(which == "row") { - extend = convertWidth(cos(numbers_rot/180*pi)*max_text_width(value, gp = numbers_gp) + numbers_offset + unit(4, "mm"), "mm", valueOnly = TRUE)/convertWidth(anno_size$width, "mm", valueOnly = TRUE)*(data_scale[2] - data_scale[1]) + number_extension = cos(numbers_rot/180*pi)*max_text_width(value, gp = numbers_gp) + numbers_offset + unit(4, "mm") + } + if(is.null(ylim) && axis_param$direction == "normal") { + if(which == "column") { + extend = convertHeight(number_extension, "mm", valueOnly = TRUE)/convertHeight(anno_size$height, "mm", valueOnly = TRUE)*(data_scale[2] - data_scale[1]) + } else if(which == "row") { + extend = convertWidth(number_extension, "mm", valueOnly = TRUE)/convertWidth(anno_size$width, "mm", valueOnly = TRUE)*(data_scale[2] - data_scale[1]) + } data_scale[2] = data_scale[2] + extend } } } - axis_param = validate_axis_param(axis_param, which) axis_grob = if(axis) construct_axis_grob(axis_param, which, data_scale, format = labels_format) else NULL row_fun = function(index, k = 1, N = 1) { @@ -1499,7 +1506,11 @@ anno_barplot = function(x, baseline = 0, which = c("column", "row"), border = TR if(add_numbers) { txt = value_origin[index] if(!is.null(attr(value, "labels_format"))) { - txt = attr(value, "labels_format")(value[index]) + if(axis_param$direction == "normal") { + txt = attr(value, "labels_format")(value[index]) + } else { + txt = attr(value, "labels_format")(value_origin[index]) + } } numbers_rot = numbers_rot %% 360 if(axis_param$direction == "normal") { @@ -1588,10 +1599,27 @@ anno_barplot = function(x, baseline = 0, which = c("column", "row"), border = TR if(ncol(value) == 1) { anno@subset_rule$gp = subset_gp } - + anno@subsettable = TRUE anno@extended = update_anno_extend(anno, axis_grob, axis_param) + if(add_numbers && ncol(value) == 1 && (!is.null(ylim) || axis_param$direction == "reverse")) { + if(which == "column") { + number_extension = convertHeight(number_extension, "mm") + if(axis_param$direction == "normal") { + anno@extended[3] = anno@extended[3] + number_extension + } else { + anno@extended[1] = anno@extended[1] + number_extension + } + } else { + number_extension = convertWidth(number_extension, "mm") + if(axis_param$direction == "normal") { + anno@extended[4] = anno@extended[4] + number_extension + } else { + anno@extended[2] = anno@extended[2] + number_extension + } + } + } return(anno) } @@ -4548,4 +4576,3 @@ anno_numeric = function(x, rg = range(x), labels_gp = gpar(), x_convert = NULL, width = width ) } - diff --git a/tests/testthat/testthat-AnnotationFunction-barplot-explicit-ylim.R b/tests/testthat/testthat-AnnotationFunction-barplot-explicit-ylim.R new file mode 100644 index 0000000..7aec6df --- /dev/null +++ b/tests/testthat/testthat-AnnotationFunction-barplot-explicit-ylim.R @@ -0,0 +1,33 @@ +test_that("anno_barplot keeps explicit ylim when numbers are added", { + anno_normal_without_numbers = anno_barplot(1:10, + add_numbers = FALSE, + ylim = c(0, 8), + height = unit(2, "cm")) + anno_normal_with_numbers = anno_barplot(1:10, + add_numbers = TRUE, + ylim = c(0, 8), + height = unit(2, "cm")) + + expect_equal(anno_normal_with_numbers@data_scale, anno_normal_without_numbers@data_scale) + expect_gt( + convertHeight(anno_normal_with_numbers@extended[3], "mm", valueOnly = TRUE), + convertHeight(anno_normal_without_numbers@extended[3], "mm", valueOnly = TRUE) + ) + + anno_reverse_without_numbers = anno_barplot(1:10, + add_numbers = FALSE, + ylim = c(0, 8), + height = unit(2, "cm"), + axis_param = list(direction = "reverse")) + anno_reverse_with_numbers = anno_barplot(1:10, + add_numbers = TRUE, + ylim = c(0, 8), + height = unit(2, "cm"), + axis_param = list(direction = "reverse")) + + expect_equal(anno_reverse_with_numbers@data_scale, anno_reverse_without_numbers@data_scale) + expect_gt( + convertHeight(anno_reverse_with_numbers@extended[1], "mm", valueOnly = TRUE), + convertHeight(anno_reverse_without_numbers@extended[1], "mm", valueOnly = TRUE) + ) +}) diff --git a/tests/testthat/testthat-AnnotationFunction-barplot-reverse.R b/tests/testthat/testthat-AnnotationFunction-barplot-reverse.R new file mode 100644 index 0000000..2065ca5 --- /dev/null +++ b/tests/testthat/testthat-AnnotationFunction-barplot-reverse.R @@ -0,0 +1,16 @@ +test_that("anno_barplot reverse numbers do not expand scale", { + anno_reverse_without_numbers = anno_barplot(1:10, + add_numbers = FALSE, + height = unit(2, "cm"), + axis_param = list(direction = "reverse")) + anno_reverse_with_numbers = anno_barplot(1:10, + add_numbers = TRUE, + height = unit(2, "cm"), + axis_param = list(direction = "reverse")) + + expect_equal(anno_reverse_with_numbers@data_scale, anno_reverse_without_numbers@data_scale) + expect_gt( + convertHeight(anno_reverse_with_numbers@extended[1], "mm", valueOnly = TRUE), + convertHeight(anno_reverse_without_numbers@extended[1], "mm", valueOnly = TRUE) + ) +})