Skip to content

Commit a8d07ef

Browse files
authored
Merge pull request #180 from rwoldford/master
Minor update ... l_hist for factors/char vector labels
2 parents 809b9de + ede9b8d commit a8d07ef

16 files changed

+224
-37
lines changed

R/DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: loon
22
Type: Package
33
Title: Interactive Statistical Data Visualization
4-
Version: 1.3.9.9000
5-
Date: 2022-03-08
4+
Version: 1.4.0
5+
Date: 2022-03-12
66
Authors@R: c(person(given = "Adrian", family = "Waddell",
77
email = "adrian@waddell.ch",
88
role = c("aut")),

R/NEWS.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
# loon 1.3.9.9000
1+
# loon 1.4.0
22

33
Beginning changes before the next release on CRAN
44

5-
* Added "loon.shiny" to l_web() now that it appears as part of diveR package
5+
* Added `"loon.shiny"` to `l_web()` now that it appears as part of diveR package
6+
7+
* Added arguments to `l_hist()` for character vectors and factors.
8+
9+
- Now factors ALWAYS created and placed in a layer.
10+
- Factor layer is simply hidden if `showFactors = FALSE`
11+
- More flexibility given to user in terms of factor text size,
12+
angle of rotation, and colour
13+
- Changed default y positions to 0 so that labels do not disappear with
14+
switch to `yshows = "density"`.
615

716
# loon 1.3.9
817

R/R/l_hist.R

+109-16
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,28 @@ l_hist.default <- function(x,
308308
}
309309

310310
#' @rdname l_hist
311-
#' @param showFactors whether to draw the factor names
311+
#' @param showFactors whether to show the factor levels as factor labels layered on the plot.
312+
#' If \code{FALSE}, the factor labels are hidden and can be turned on
313+
#' from the "layers" tab on the inspector.
314+
#' @param factorLabelAngle is the angle of rotation (in degrees) for the factor labels.
315+
#' If not specified, an angle of 0 is chosen if there are fewer than 10 labels; labels are
316+
#' rotated 90 degrees if there are 10 or more. This can also be a numeric vector of length
317+
#' equal to the number of factor levels in \code{x}.
318+
#' @param factorLabelSize is the font size for the factor labels (default 12).
319+
#' @param factorLabelColor is the colour to be used for the factor labels.
320+
#' (default is \code{l_getOption("foreground")}). Can be a vector of length
321+
#' equal to that of the number of factor levels in \code{x}.
322+
#' @param factorLabelY either a single number (default 0), or a numeric vector of length
323+
#' equal to that of the number of factor levels, determining the
324+
#' y coordinate(s) for the factor labels.
312325
#' @export
313-
l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
326+
l_hist.factor <- function(x,
327+
showFactors = length(unique(x)) < 25L,
328+
factorLabelAngle,
329+
factorLabelSize = 12,
330+
factorLabelColor = l_getOption("foreground"),
331+
factorLabelY = 0,
332+
...) {
314333

315334
if(missing(x))
316335
return(
@@ -323,10 +342,36 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
323342
dotArgs$xlabel <- gsub("\"", "", deparse(substitute(x)))
324343
}
325344

345+
if (!is.null(dotArgs$yshows)) {
346+
if(dotArgs$yshows == "density"){
347+
dotArgs$yshows <- "frequency"
348+
warning("For character or factor data, `yshows` cannot be `density`.",
349+
"Switched `yshows` to ", dotArgs$yshows)
350+
}
351+
}
352+
326353
x <- as.factor(x)
327354

328355
levelNames <- levels(x)
329356
nlevels <- length(levelNames)
357+
if(missing(factorLabelAngle)){
358+
if(nlevels >= 10) {
359+
factorLabelAngle <- 90
360+
} else {
361+
factorLabelAngle <- 0
362+
}
363+
}
364+
if(!is.numeric(factorLabelY) | (length(factorLabelY) == 0)) {
365+
warning("factorLabelY must be numeric; using default -1")
366+
factorLabelY <- rep(-1, nlevels)
367+
} else {
368+
if(length(factorLabelY) != nlevels) {
369+
factorLabelY <- rep(factorLabelY,
370+
length.out = nlevels)
371+
}
372+
}
373+
374+
330375
x <- unclass(x) # Get the level numbers as numeric values
331376
dotArgs$x <- x
332377

@@ -342,7 +387,7 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
342387
uni_x <- unique(x)
343388
binwidth <- if(length(uni_x) == 1) {
344389
# This is a single bin histogram
345-
# the binwidth can be set as any non-negtive value
390+
# the bin width can be set as any non-negative value
346391
0.1
347392
} else {
348393
min(diff(sort(uni_x)))
@@ -354,11 +399,11 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
354399
hist <- do.call(l_hist.default, dotArgs)
355400

356401
# Add level names to plot
357-
## Adjust text coords
402+
## Adjust text coordinates
358403
## The reason to do so is to make sure that
359-
## `labels` always lay down the corresponding bins no matter how origin shifts
404+
## `labels` always lay down the corresponding bins
405+
## no matter how origin shifts
360406

361-
if(!showFactors) return(hist)
362407

363408
if(inherits(hist, "l_compound")) {
364409

@@ -372,10 +417,16 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
372417

373418
text_adjust <- text_adjust - 0.5
374419

375-
l_layer_texts(h, x = seq(nlevels) + text_adjust, y = rep(-1, nlevels),
376-
text = levelNames, label = "Factor levels",
377-
angle = 0,
378-
size = 12, color = l_getOption("foreground"))
420+
text_layer <- l_layer_texts(h,
421+
x = seq(nlevels) + text_adjust,
422+
y = factorLabelY,
423+
text = levelNames,
424+
label = "Factor levels",
425+
angle = factorLabelAngle,
426+
size = factorLabelSize,
427+
color = factorLabelColor)
428+
429+
if(!showFactors) l_layer_hide(h, text_layer)
379430
})
380431

381432
} else {
@@ -387,25 +438,67 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
387438

388439
text_adjust <- text_adjust - 0.5
389440

390-
l_layer_texts(hist, x = seq(nlevels) + text_adjust, y = rep(-1, nlevels),
391-
text = levelNames, label = "Factor levels",
392-
angle = 0,
393-
size = 12, color = l_getOption("foreground"))
441+
text_layer <- l_layer_texts(hist,
442+
x = seq(nlevels) + text_adjust,
443+
y = factorLabelY,
444+
text = levelNames,
445+
label = "Factor levels",
446+
angle = factorLabelAngle,
447+
size = factorLabelSize,
448+
color = factorLabelColor)
449+
450+
if(!showFactors) l_layer_hide(hist, text_layer)
394451
}
395452

396453
hist
397454
}
398455

399456
#' @rdname l_hist
457+
#' @param showFactors whether to show the factor labels (unique strings in \code{x})
458+
#' as a layer on the plot.
459+
#' If \code{FALSE}, the factor labels are hidden and can be turned on
460+
#' from the "layers" tab on the inspector.
461+
#' @param factorLabelAngle is the angle of rotation (in degrees) for the factor labels.
462+
#' If not specified, an angle of 0 is chosen if there are fewer than 10 labels; labels are
463+
#' rotated 90 degrees if there are 10 or more. This can also be a numeric vector of length
464+
#' equal to the number of factor labels.
465+
#' @param factorLabelSize is the font size for the factor labels (default 12).
466+
#' @param factorLabelColor is the colour to be used for the factor labels.
467+
#' (default is \code{l_getOption("foreground")}). Can also be a vector
468+
#' equal to that of the number of factor labels.
469+
#' @param factorLabelY either a single number, or a numeric vector of length
470+
#' equal to the number of factor labels, determining the
471+
#' y coordinate(s) for the factor labels.
400472
#' @export
401-
l_hist.character <- function(x, showFactors = length(unique(x)) < 25L, ...) {
473+
l_hist.character <- function(x,
474+
showFactors = length(unique(x)) < 25L,
475+
factorLabelAngle,
476+
factorLabelSize = 12,
477+
factorLabelColor = l_getOption("foreground"),
478+
factorLabelY = 0,
479+
...) {
402480

403481
if(missing(x))
404482
return(
405483
l_hist.default(x, ...)
406484
)
407485

408-
l_hist.factor(x, showFactors = showFactors, ...)
486+
nlevels <- length(unique(x))
487+
if(missing(factorLabelAngle)){
488+
if(nlevels >= 10) {
489+
factorLabelAngle <- 90
490+
} else {
491+
factorLabelAngle <- 0
492+
}
493+
}
494+
495+
l_hist.factor(x,
496+
showFactors = showFactors,
497+
factorLabelAngle = factorLabelAngle,
498+
factorLabelSize = factorLabelSize,
499+
factorLabelColor = factorLabelColor,
500+
factorLabelY = factorLabelY,
501+
...)
409502
}
410503

411504
#' @rdname l_hist

R/R/l_layer.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ l_layer_text <- function(widget, x, y, text, color="gray60", size=6, angle=0,
876876
#' @param size font size
877877
#' @param angle text rotation
878878
#' @param anchor specifies how the information in a text is to be displayed in the widget.
879-
#' Must be one of the values c("n", "ne", "e", "se", "s", "sw", "w", "nw", "center).
879+
#' Must be one of the values c("n", "ne", "e", "se", "s", "sw", "w", "nw", "center").
880880
#' For example, "nw" means display the information such that its top-left corner is at the
881881
#' top-left corner of the widget.
882882
#' @param justify when there are multiple lines of text displayed in a widget,

R/man/l_hist.Rd

+37-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

R/man/l_layer_texts.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

R/vignettes/SavingLoonPlots.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Saving loon plots"
33
author: "R.W. Oldford"
4-
date: "20/04/2020"
4+
date: "April 20, 2020"
55
output:
66
html_vignette:
77
toc: true

R/vignettes/introduction.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Introduction to loon"
33
author: "R.W. Oldford"
4-
date: "`r Sys.Date()`"
4+
date: "March 14, 2021"
55
output:
66
html_vignette:
77
toc: true

R/vignettes/logicalQueries.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Logical queries"
33
author: "R.W. Oldford"
4-
date: "`r Sys.Date()`"
4+
date: "September 5, 2021"
55
output:
66
html_vignette:
77
toc: true

docs/articles/introduction.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/logicalQueries.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/teaching-example-smoothing.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/news/index.html

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/pkgdown.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ articles:
77
loonPlotsAndGridGraphics: loonPlotsAndGridGraphics.html
88
savingLoonPlots: savingLoonPlots.html
99
teaching-example-smoothing: teaching-example-smoothing.html
10-
last_built: 2022-03-09T01:06Z
10+
last_built: 2022-03-12T22:36Z
1111

0 commit comments

Comments
 (0)