Skip to content

Commit

Permalink
Merge pull request #180 from rwoldford/master
Browse files Browse the repository at this point in the history
Minor update ... l_hist for factors/char vector labels
  • Loading branch information
rwoldford authored Mar 13, 2022
2 parents 809b9de + ede9b8d commit a8d07ef
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 37 deletions.
4 changes: 2 additions & 2 deletions R/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: loon
Type: Package
Title: Interactive Statistical Data Visualization
Version: 1.3.9.9000
Date: 2022-03-08
Version: 1.4.0
Date: 2022-03-12
Authors@R: c(person(given = "Adrian", family = "Waddell",
email = "[email protected]",
role = c("aut")),
Expand Down
13 changes: 11 additions & 2 deletions R/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# loon 1.3.9.9000
# loon 1.4.0

Beginning changes before the next release on CRAN

* Added "loon.shiny" to l_web() now that it appears as part of diveR package
* Added `"loon.shiny"` to `l_web()` now that it appears as part of diveR package

* Added arguments to `l_hist()` for character vectors and factors.

- Now factors ALWAYS created and placed in a layer.
- Factor layer is simply hidden if `showFactors = FALSE`
- More flexibility given to user in terms of factor text size,
angle of rotation, and colour
- Changed default y positions to 0 so that labels do not disappear with
switch to `yshows = "density"`.

# loon 1.3.9

Expand Down
125 changes: 109 additions & 16 deletions R/R/l_hist.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,28 @@ l_hist.default <- function(x,
}

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

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

if (!is.null(dotArgs$yshows)) {
if(dotArgs$yshows == "density"){
dotArgs$yshows <- "frequency"
warning("For character or factor data, `yshows` cannot be `density`.",
"Switched `yshows` to ", dotArgs$yshows)
}
}

x <- as.factor(x)

levelNames <- levels(x)
nlevels <- length(levelNames)
if(missing(factorLabelAngle)){
if(nlevels >= 10) {
factorLabelAngle <- 90
} else {
factorLabelAngle <- 0
}
}
if(!is.numeric(factorLabelY) | (length(factorLabelY) == 0)) {
warning("factorLabelY must be numeric; using default -1")
factorLabelY <- rep(-1, nlevels)
} else {
if(length(factorLabelY) != nlevels) {
factorLabelY <- rep(factorLabelY,
length.out = nlevels)
}
}


x <- unclass(x) # Get the level numbers as numeric values
dotArgs$x <- x

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

# Add level names to plot
## Adjust text coords
## Adjust text coordinates
## The reason to do so is to make sure that
## `labels` always lay down the corresponding bins no matter how origin shifts
## `labels` always lay down the corresponding bins
## no matter how origin shifts

if(!showFactors) return(hist)

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

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

text_adjust <- text_adjust - 0.5

l_layer_texts(h, x = seq(nlevels) + text_adjust, y = rep(-1, nlevels),
text = levelNames, label = "Factor levels",
angle = 0,
size = 12, color = l_getOption("foreground"))
text_layer <- l_layer_texts(h,
x = seq(nlevels) + text_adjust,
y = factorLabelY,
text = levelNames,
label = "Factor levels",
angle = factorLabelAngle,
size = factorLabelSize,
color = factorLabelColor)

if(!showFactors) l_layer_hide(h, text_layer)
})

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

text_adjust <- text_adjust - 0.5

l_layer_texts(hist, x = seq(nlevels) + text_adjust, y = rep(-1, nlevels),
text = levelNames, label = "Factor levels",
angle = 0,
size = 12, color = l_getOption("foreground"))
text_layer <- l_layer_texts(hist,
x = seq(nlevels) + text_adjust,
y = factorLabelY,
text = levelNames,
label = "Factor levels",
angle = factorLabelAngle,
size = factorLabelSize,
color = factorLabelColor)

if(!showFactors) l_layer_hide(hist, text_layer)
}

hist
}

#' @rdname l_hist
#' @param showFactors whether to show the factor labels (unique strings in \code{x})
#' as a layer on the plot.
#' If \code{FALSE}, the factor labels are hidden and can be turned on
#' from the "layers" tab on the inspector.
#' @param factorLabelAngle is the angle of rotation (in degrees) for the factor labels.
#' If not specified, an angle of 0 is chosen if there are fewer than 10 labels; labels are
#' rotated 90 degrees if there are 10 or more. This can also be a numeric vector of length
#' equal to the number of factor labels.
#' @param factorLabelSize is the font size for the factor labels (default 12).
#' @param factorLabelColor is the colour to be used for the factor labels.
#' (default is \code{l_getOption("foreground")}). Can also be a vector
#' equal to that of the number of factor labels.
#' @param factorLabelY either a single number, or a numeric vector of length
#' equal to the number of factor labels, determining the
#' y coordinate(s) for the factor labels.
#' @export
l_hist.character <- function(x, showFactors = length(unique(x)) < 25L, ...) {
l_hist.character <- function(x,
showFactors = length(unique(x)) < 25L,
factorLabelAngle,
factorLabelSize = 12,
factorLabelColor = l_getOption("foreground"),
factorLabelY = 0,
...) {

if(missing(x))
return(
l_hist.default(x, ...)
)

l_hist.factor(x, showFactors = showFactors, ...)
nlevels <- length(unique(x))
if(missing(factorLabelAngle)){
if(nlevels >= 10) {
factorLabelAngle <- 90
} else {
factorLabelAngle <- 0
}
}

l_hist.factor(x,
showFactors = showFactors,
factorLabelAngle = factorLabelAngle,
factorLabelSize = factorLabelSize,
factorLabelColor = factorLabelColor,
factorLabelY = factorLabelY,
...)
}

#' @rdname l_hist
Expand Down
2 changes: 1 addition & 1 deletion R/R/l_layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ l_layer_text <- function(widget, x, y, text, color="gray60", size=6, angle=0,
#' @param size font size
#' @param angle text rotation
#' @param anchor specifies how the information in a text is to be displayed in the widget.
#' Must be one of the values c("n", "ne", "e", "se", "s", "sw", "w", "nw", "center).
#' Must be one of the values c("n", "ne", "e", "se", "s", "sw", "w", "nw", "center").
#' For example, "nw" means display the information such that its top-left corner is at the
#' top-left corner of the widget.
#' @param justify when there are multiple lines of text displayed in a widget,
Expand Down
40 changes: 37 additions & 3 deletions R/man/l_hist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion R/man/l_layer_texts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion R/vignettes/SavingLoonPlots.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Saving loon plots"
author: "R.W. Oldford"
date: "20/04/2020"
date: "April 20, 2020"
output:
html_vignette:
toc: true
Expand Down
2 changes: 1 addition & 1 deletion R/vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Introduction to loon"
author: "R.W. Oldford"
date: "`r Sys.Date()`"
date: "March 14, 2021"
output:
html_vignette:
toc: true
Expand Down
2 changes: 1 addition & 1 deletion R/vignettes/logicalQueries.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Logical queries"
author: "R.W. Oldford"
date: "`r Sys.Date()`"
date: "September 5, 2021"
output:
html_vignette:
toc: true
Expand Down
2 changes: 1 addition & 1 deletion docs/articles/introduction.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/logicalQueries.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/teaching-example-smoothing.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ articles:
loonPlotsAndGridGraphics: loonPlotsAndGridGraphics.html
savingLoonPlots: savingLoonPlots.html
teaching-example-smoothing: teaching-example-smoothing.html
last_built: 2022-03-09T01:06Z
last_built: 2022-03-12T22:36Z

Loading

0 comments on commit a8d07ef

Please sign in to comment.