Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1dbe740

Browse files
Make tinytheme() play nicer with ridge and spineplot types (grantmcdermott#298)
* fix dynmar ylab spacing for ridge type * Fix theme col inheritance * similar catches for spineplot type * add tests * docs
1 parent bb02334 commit 1dbe740

File tree

8 files changed

+245
-5
lines changed

8 files changed

+245
-5
lines changed

R/facet.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ draw_facet_window = function(grid, ...) {
183183
# extra whitespace bump on the y axis
184184
# yaxl = axTicks(2)
185185
yaxl = axisTicks(usr = extendrange(ylim, f = 0.04), log = par("ylog"))
186+
## overrides for ridge and spineplot types
187+
if (type == "ridge") yaxl = levels(y)
188+
if (type == "spineplot") yaxl = ylabs
186189
# whtsbp = grconvertX(max(strwidth(yaxl, "figure")), from = "nfc", to = "lines") - 1
187190
whtsbp = grconvertX(max(strwidth(yaxl, "figure")), from = "nfc", to = "lines") - grconvertX(0, from = "nfc", to = "lines") - 1
188191
if (whtsbp > 0) {
@@ -236,10 +239,14 @@ draw_facet_window = function(grid, ...) {
236239
# Dynamic plot margin adjustments
237240
omar = par("mar")
238241
omar = omar - c(0, 0, 1, 0) # reduce top whitespace since no facet (title)
242+
if (type == "spineplot") omar[4] = 2.1 # catch for spineplot RHS axis labs
239243
if (par("las") %in% 1:2) {
240244
# extra whitespace bump on the y axis
241245
# yaxl = axTicks(2)
242246
yaxl = axisTicks(usr = extendrange(ylim, f = 0.04), log = par("ylog"))
247+
## overrides for ridge and spineplot types
248+
if (type == "ridge") yaxl = levels(y)
249+
if (type == "spineplot") yaxl = ylabs
243250
# whtsbp = grconvertX(max(strwidth(yaxl, "figure")), from = "nfc", to = "lines") - 1
244251
whtsbp = grconvertX(max(strwidth(yaxl, "figure")), from = "nfc", to = "lines") - grconvertX(0, from = "nfc", to = "lines") - 1
245252
if (whtsbp > 0) {

R/tinytheme.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#' Known current limitations include:
4545
#'
4646
#' - Themes do not work well when `legend = "top!"`.
47-
#' - Themes do not play nicely with some complex plot types, particularly `"spineplot"` and `"ridge"`.
4847
#' - Dynamic margin spacing does not account for multi-line strings (e.g., axes
4948
#' or main titles that contain "\\n").
5049
#'

R/type_ridge.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ data_ridge = function(bw = "nrd0", adjust = 1, kernel = "gaussian", n = 512,
318318
dotspal = list(...)[["palette"]]
319319
palette = if (!is.null(dotspal)) dotspal else gradient
320320
gradient = TRUE
321-
if (isTRUE(palette)) palette = "viridis"
321+
if (isTRUE(palette)) {
322+
palette = if (!is.null(.tpar[["palette.sequential"]])) .tpar[["palette.sequential"]] else "viridis"
323+
}
322324

323325
if (length(palette) > 1L || !is.character(palette)) {
324326
## color vector already given
@@ -380,7 +382,8 @@ draw_ridge = function() {
380382
d = data.frame(x = ix, y = iy, ymin = iymin, ymax = iymax)
381383
dsplit = split(d, d$y)
382384
if (is.null(ibg)) {
383-
ibg = if (isTRUE(type_info[["fill_by"]])) seq_palette(icol, n = 2)[2] else "gray"
385+
default_bg = if (!is.null(.tpar[["palette.qualitative"]])) seq_palette(by_col(), n = 2)[2] else "gray"
386+
ibg = if (isTRUE(type_info[["fill_by"]])) seq_palette(icol, n = 2)[2] else default_bg
384387
}
385388
if (!is.null(type_info[["alpha"]]) && is.null(type_info[["palette"]])) {
386389
ibg = adjustcolor(ibg, alpha.f = type_info[["alpha"]])

R/type_spineplot.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ data_spineplot = function(off = NULL, breaks = NULL, ylevels = ylevels, xaxlabel
215215
if (isTRUE(y_by)) datapoints$by = rep(yaxlabels, length.out = nrow(datapoints))
216216

217217
## grayscale flag
218-
grayscale = length(unique(datapoints[["by"]])) == 1 && is.null(palette)
218+
grayscale = length(unique(datapoints[["by"]])) == 1 && is.null(palette) && is.null(.tpar[["palette.qualitative"]])
219219

220220
out = list(
221221
x = c(datapoints$xmin, datapoints$xmax),
@@ -235,6 +235,7 @@ data_spineplot = function(off = NULL, breaks = NULL, ylevels = ylevels, xaxlabel
235235
yaxt = "n",
236236
xaxs = "i",
237237
yaxs = "i",
238+
ylabs = yaxlabels,
238239
type_info = list(
239240
off = off,
240241
x.categorical = x.categorical,

inst/tinytest/_tinysnapshot/tinytheme_dynamic_clean_ridge.svg

Lines changed: 83 additions & 0 deletions
Loading
Lines changed: 126 additions & 0 deletions
Loading

inst/tinytest/test-tinytheme.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ tinytheme("dark")
7070
f()
7171
expect_snapshot_plot(f, label = "tinytheme_dynamic_dark_facet")
7272

73+
# ridge and spineplot types (req's extra steps b/c of tinyAxis logic)
74+
75+
tinytheme('clean')
76+
77+
f = function() {
78+
tinyplot(
79+
Species ~ Petal.Length, data = iris, type = "ridge",
80+
main = "Dynamic plot adjustment and whitespace reduction",
81+
sub = "Ridge plot version"
82+
)
83+
}
84+
expect_snapshot_plot(f, label = "tinytheme_dynamic_clean_ridge")
85+
86+
f = function() {
87+
tinyplot(
88+
Species ~ Petal.Length, data = iris, type = "spineplot",
89+
main = "Dynamic plot adjustment and whitespace reduction",
90+
sub = "Spineplot version"
91+
)
92+
}
93+
expect_snapshot_plot(f, label = "tinytheme_dynamic_clean_spineplot")
94+
7395
#
7496
## reset
7597

man/tinytheme.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)