@@ -308,9 +308,28 @@ l_hist.default <- function(x,
308
308
}
309
309
310
310
# ' @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.
312
325
# ' @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
+ ... ) {
314
333
315
334
if (missing(x ))
316
335
return (
@@ -323,10 +342,36 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
323
342
dotArgs $ xlabel <- gsub(" \" " , " " , deparse(substitute(x )))
324
343
}
325
344
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
+
326
353
x <- as.factor(x )
327
354
328
355
levelNames <- levels(x )
329
356
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
+
330
375
x <- unclass(x ) # Get the level numbers as numeric values
331
376
dotArgs $ x <- x
332
377
@@ -342,7 +387,7 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
342
387
uni_x <- unique(x )
343
388
binwidth <- if (length(uni_x ) == 1 ) {
344
389
# 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
346
391
0.1
347
392
} else {
348
393
min(diff(sort(uni_x )))
@@ -354,11 +399,11 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
354
399
hist <- do.call(l_hist.default , dotArgs )
355
400
356
401
# Add level names to plot
357
- # # Adjust text coords
402
+ # # Adjust text coordinates
358
403
# # 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
360
406
361
- if (! showFactors ) return (hist )
362
407
363
408
if (inherits(hist , " l_compound" )) {
364
409
@@ -372,10 +417,16 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
372
417
373
418
text_adjust <- text_adjust - 0.5
374
419
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 )
379
430
})
380
431
381
432
} else {
@@ -387,25 +438,67 @@ l_hist.factor <- function(x, showFactors = length(unique(x)) < 25L, ...) {
387
438
388
439
text_adjust <- text_adjust - 0.5
389
440
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 )
394
451
}
395
452
396
453
hist
397
454
}
398
455
399
456
# ' @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.
400
472
# ' @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
+ ... ) {
402
480
403
481
if (missing(x ))
404
482
return (
405
483
l_hist.default(x , ... )
406
484
)
407
485
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
+ ... )
409
502
}
410
503
411
504
# ' @rdname l_hist
0 commit comments