diff --git a/NEWS.md b/NEWS.md index 56a92978..fb22ec71 100644 --- a/NEWS.md +++ b/NEWS.md @@ -56,6 +56,8 @@ where the formatting is also better._ x-axis order of the original plot layer. This should ensure that we don't end up with misaligned layers. For example, when ribbon is added on top of an errorbar plot. (#517, #520, #523, #526 @grantmcdermott) +- Custom axis titles work properly for one-sided (formula) bar plots. Thanks to + @lbelzile for the report in #423. (#527 @grantmcdermott) ### Documentation diff --git a/R/tinyplot.R b/R/tinyplot.R index 8d5c1904..f60d1ab8 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -1463,17 +1463,23 @@ tinyplot.formula = function( } ## nice axis and legend labels - dens_type = (is.atomic(type) && identical(type, "density")) || (!is.atomic(type) && identical(type$name, "density")) - hist_type = (is.atomic(type) && type %in% c("hist", "histogram")) || (!is.atomic(type) && identical(type$name, "histogram")) - if (!is.null(type) && dens_type) { + dens_type = !is.null(type) && (is.atomic(type) && identical(type, "density")) || (!is.atomic(type) && identical(type$name, "density")) + hist_type = !is.null(type) && (is.atomic(type) && type %in% c("hist", "histogram")) || (!is.atomic(type) && identical(type$name, "histogram")) + barp_type = !is.null(type) && (is.atomic(type) && identical(type, "barplot")) || (!is.atomic(type) && identical(type$name, "barplot")) + if (dens_type) { # if (is.null(ylab)) ylab = "Density" ## rather assign ylab as part of internal type_density() logic if (is.null(xlab)) xlab = xnam - } else if (!is.null(type) && hist_type) { + } else if (hist_type) { # if (is.null(ylab)) ylab = "Frequency" ## rather assign ylab as part of internal type_histogram() logic if (is.null(xlab)) xlab = xnam } else if (is.null(y)) { - if (is.null(ylab)) ylab = xnam - if (is.null(xlab)) xlab = "Index" + if (!barp_type) { + if (is.null(ylab)) ylab = xnam + if (is.null(xlab)) xlab = "Index" + } else { + if (is.null(ylab)) ylab = "Count" + if (is.null(xlab)) xlab = xnam + } } else { if (is.null(ylab)) ylab = ynam if (is.null(xlab)) xlab = xnam diff --git a/R/type_barplot.R b/R/type_barplot.R index df8de8a0..2d3dd4d4 100644 --- a/R/type_barplot.R +++ b/R/type_barplot.R @@ -91,9 +91,8 @@ data_barplot = function(width = 5/6, beside = FALSE, center = FALSE, FUN = NULL, ## tabulate/aggregate datapoints if (is.null(datapoints$y)) { - xlab = ylab - ylab = "Count" - + if (is.null(xlab) || xlab == "Index") xlab = ylab + if (is.null(settings$y_dep) && is.null(ylab)) ylab = "Count" datapoints$y = numeric(nrow(datapoints)) if (!is.null(FUN)) warning("without 'y' variable 'FUN' specification is ignored") FUN = length diff --git a/inst/tinytest/_tinysnapshot/barplot_custom_xtitle.svg b/inst/tinytest/_tinysnapshot/barplot_custom_xtitle.svg new file mode 100644 index 00000000..f83f9133 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/barplot_custom_xtitle.svg @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + +grp +0 +1 + + + + + + + +Custom x title +Count + + +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 + + + + + + + + + +0 +2 +4 +6 +8 +10 +12 +14 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/_tinysnapshot/barplot_custom_ytitle.svg b/inst/tinytest/_tinysnapshot/barplot_custom_ytitle.svg new file mode 100644 index 00000000..0fe2f886 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/barplot_custom_ytitle.svg @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + +grp +0 +1 + + + + + + + +x +Custom y title + + +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 + + + + + + + + + +0 +2 +4 +6 +8 +10 +12 +14 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/test-type_barplot.R b/inst/tinytest/test-type_barplot.R index 8b551865..4a973b7e 100644 --- a/inst/tinytest/test-type_barplot.R +++ b/inst/tinytest/test-type_barplot.R @@ -53,3 +53,24 @@ f = function() { } expect_snapshot_plot(f, label = "barplot_text_issue469") +# +## Custom axis titles for one-sided barplots (issue #423) + +f = function() { + set.seed(2025) + n = 100L + grp = factor(sample(0:1, size = n, replace = TRUE)) + x = rpois(n, 5) + plt(~ x | grp, type = "barplot", beside = TRUE, xlab = "Custom x title") +} +expect_snapshot_plot(f, label = "barplot_custom_xtitle") + +# issue #423 +f = function() { + set.seed(2025) + n = 100L + grp = factor(sample(0:1, size = n, replace = TRUE)) + x = rpois(n, 5) + plt(~ x | grp, type = "barplot", beside = TRUE, ylab = "Custom y title") +} +expect_snapshot_plot(f, label = "barplot_custom_ytitle") \ No newline at end of file