diff --git a/NEWS.md b/NEWS.md index 9281cc89..a4a3d1a2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -47,6 +47,10 @@ where the formatting is also better._ ### Bug fixes +- `flip = TRUE` now flips the drawn geometry of the single-letter line types, + not just the axes: `type = "h"` draws horizontal segments to the baseline, + and the step types `"s"` and `"S"` swap which coordinate moves first. + (#675 @haomeng797-ship-it) - Fixed several bugs specific to plots with free facets (i.e., `facet.args = list(free = TRUE)`): - Single-valued discrete axes no longer trigger invalid `par(usr)` values. diff --git a/R/type_lines.R b/R/type_lines.R index a76155be..7caa91fa 100644 --- a/R/type_lines.R +++ b/R/type_lines.R @@ -77,12 +77,37 @@ data_lines = function(dodge = 0, fixed.dodge = FALSE) { draw_lines = function(type = "l") { - fun = function(ix, iy, icol, ipch, ibg, ilty, ilwd, icex = 1, ...) { + fun = function(ix, iy, icol, ipch, ibg, ilty, ilwd, icex = 1, flip = FALSE, ...) { + ltype = type + if (isTRUE(flip)) { + # flip_datapoints() has already swapped the coordinates, but the + # base engine still draws these types in fixed orientations: + # type = "h" always drops vertically, and the step types commit + # to which coordinate moves first. So draw "h" as explicit + # horizontal segments to the baseline, and mirror the step order. + if (ltype == "h") { + x0 = if (par("xlog")) 10^par("usr")[1] else 0 + segments( + x0 = x0, + y0 = iy, + x1 = ix, + y1 = iy, + col = icol, + lty = ilty, + lwd = ilwd + ) + return(invisible(NULL)) + } else if (ltype == "s") { + ltype = "S" + } else if (ltype == "S") { + ltype = "s" + } + } lines( x = ix, y = iy, col = icol, - type = type, + type = ltype, pch = ipch, bg = ibg, lty = ilty, diff --git a/inst/tinytest/_tinysnapshot/flip_type_h.svg b/inst/tinytest/_tinysnapshot/flip_type_h.svg new file mode 100644 index 00000000..1690072b --- /dev/null +++ b/inst/tinytest/_tinysnapshot/flip_type_h.svg @@ -0,0 +1,76 @@ + + + + + + + + + + + + + +Flipped type = "h" +(1:10)^2 +1:10 + + + + + + + +0 +20 +40 +60 +80 +100 + + + + + + +2 +4 +6 +8 +10 + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/_tinysnapshot/flip_type_h_grouped.svg b/inst/tinytest/_tinysnapshot/flip_type_h_grouped.svg new file mode 100644 index 00000000..6c810108 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/flip_type_h_grouped.svg @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + +Tree +3 +1 +5 +2 +4 + + + + + + + +Flipped grouped type = "h" +circumference +age + + + + + + + +50 +100 +150 +200 + + + + +500 +1000 +1500 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/_tinysnapshot/flip_type_step.svg b/inst/tinytest/_tinysnapshot/flip_type_step.svg new file mode 100644 index 00000000..5a19e579 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/flip_type_step.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + +Flipped type = "s" +(1:10)^2 +1:10 + + + + + + + +0 +20 +40 +60 +80 +100 + + + + + + +2 +4 +6 +8 +10 + + + + + + + + + + + + diff --git a/inst/tinytest/_tinysnapshot/flip_type_step_rev.svg b/inst/tinytest/_tinysnapshot/flip_type_step_rev.svg new file mode 100644 index 00000000..359633f7 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/flip_type_step_rev.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + +Flipped type = "S" +(1:10)^2 +1:10 + + + + + + + +0 +20 +40 +60 +80 +100 + + + + + + +2 +4 +6 +8 +10 + + + + + + + + + + + + diff --git a/inst/tinytest/test-flip.R b/inst/tinytest/test-flip.R index 35bb3604..c2e9537d 100644 --- a/inst/tinytest/test-flip.R +++ b/inst/tinytest/test-flip.R @@ -113,3 +113,30 @@ f = function() { tinyplot(x_dt, y, grid = TRUE, flip = TRUE) } expect_snapshot_plot(f, label = "flip_date") + + +# flipped single-letter line types (#675) + +f = function() { + tinyplot(1:10, (1:10)^2, type = "h", flip = TRUE, main = "Flipped type = \"h\"") +} +expect_snapshot_plot(f, label = "flip_type_h") + +f = function() { + tinyplot(1:10, (1:10)^2, type = "s", flip = TRUE, main = "Flipped type = \"s\"") +} +expect_snapshot_plot(f, label = "flip_type_step") + +f = function() { + tinyplot(1:10, (1:10)^2, type = "S", flip = TRUE, main = "Flipped type = \"S\"") +} +expect_snapshot_plot(f, label = "flip_type_step_rev") + +f = function() { + tinyplot( + circumference ~ age | Tree, data = Orange, + type = "h", flip = TRUE, grid = TRUE, + main = "Flipped grouped type = \"h\"" + ) +} +expect_snapshot_plot(f, label = "flip_type_h_grouped")