diff --git a/R/DESCRIPTION b/R/DESCRIPTION index f8ece4d2..c7ebf9e0 100755 --- a/R/DESCRIPTION +++ b/R/DESCRIPTION @@ -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 = "adrian@waddell.ch", role = c("aut")), diff --git a/R/NEWS.md b/R/NEWS.md index ff7061a6..879cf616 100755 --- a/R/NEWS.md +++ b/R/NEWS.md @@ -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 diff --git a/R/R/l_hist.R b/R/R/l_hist.R index bb66e759..a709751e 100755 --- a/R/R/l_hist.R +++ b/R/R/l_hist.R @@ -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( @@ -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 @@ -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))) @@ -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")) { @@ -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 { @@ -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 diff --git a/R/R/l_layer.R b/R/R/l_layer.R index b69f97c8..adf1327e 100755 --- a/R/R/l_layer.R +++ b/R/R/l_layer.R @@ -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, diff --git a/R/man/l_hist.Rd b/R/man/l_hist.Rd index b727832f..005b9eb5 100644 --- a/R/man/l_hist.Rd +++ b/R/man/l_hist.Rd @@ -36,9 +36,25 @@ l_hist(x, ...) ... ) -\method{l_hist}{factor}(x, showFactors = length(unique(x)) < 25L, ...) +\method{l_hist}{factor}( + x, + showFactors = length(unique(x)) < 25L, + factorLabelAngle, + factorLabelSize = 12, + factorLabelColor = l_getOption("foreground"), + factorLabelY = 0, + ... +) -\method{l_hist}{character}(x, showFactors = length(unique(x)) < 25L, ...) +\method{l_hist}{character}( + x, + showFactors = length(unique(x)) < 25L, + factorLabelAngle, + factorLabelSize = 12, + factorLabelColor = l_getOption("foreground"), + factorLabelY = 0, + ... +) \method{l_hist}{data.frame}(x, ...) @@ -140,7 +156,25 @@ specified (i.e. not \code{NULL}) then the plot widget needs to be placed using some geometry manager like \code{\link{tkpack}} or \code{\link{tkplace}} in order to be displayed. See the examples below.} -\item{showFactors}{whether to draw the factor names} +\item{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.} + +\item{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.} + +\item{factorLabelSize}{is the font size for the factor labels (default 12).} + +\item{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.} + +\item{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.} } \value{ if the argument \code{by} is not set, a \code{loon} widget will be returned; diff --git a/R/man/l_layer_texts.Rd b/R/man/l_layer_texts.Rd index db4ce9e7..e3beafb9 100755 --- a/R/man/l_layer_texts.Rd +++ b/R/man/l_layer_texts.Rd @@ -37,7 +37,7 @@ l_layer_texts( \item{angle}{text rotation} \item{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.} diff --git a/R/vignettes/SavingLoonPlots.Rmd b/R/vignettes/SavingLoonPlots.Rmd index 0a1e2b9c..9f17c802 100644 --- a/R/vignettes/SavingLoonPlots.Rmd +++ b/R/vignettes/SavingLoonPlots.Rmd @@ -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 diff --git a/R/vignettes/introduction.Rmd b/R/vignettes/introduction.Rmd index b7c49f14..e6a44f5a 100755 --- a/R/vignettes/introduction.Rmd +++ b/R/vignettes/introduction.Rmd @@ -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 diff --git a/R/vignettes/logicalQueries.Rmd b/R/vignettes/logicalQueries.Rmd index 0874d718..b037ebba 100755 --- a/R/vignettes/logicalQueries.Rmd +++ b/R/vignettes/logicalQueries.Rmd @@ -1,7 +1,7 @@ --- title: "Logical queries" author: "R.W. Oldford" -date: "`r Sys.Date()`" +date: "September 5, 2021" output: html_vignette: toc: true diff --git a/docs/articles/introduction.html b/docs/articles/introduction.html index ad9cd926..24a1d493 100755 --- a/docs/articles/introduction.html +++ b/docs/articles/introduction.html @@ -116,7 +116,7 @@
vignettes/introduction.Rmd
introduction.Rmd
vignettes/logicalQueries.Rmd
logicalQueries.Rmd
vignettes/teaching-example-smoothing.Rmd
teaching-example-smoothing.Rmd
Beginning changes before the next release on CRAN
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.
showFactors = FALSE
+whether to draw the factor names
whether to show the factor labels (unique strings in x
)
+as a layer on the plot.
+If FALSE
, the factor labels are hidden and can be turned on
+from the "layers" tab on the inspector.
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.
is the font size for the factor labels (default 12).
is the colour to be used for the factor labels.
+(default is l_getOption("foreground")
). Can also be a vector
+equal to that of the number of factor labels.
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.
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.