diff --git a/DESCRIPTION b/DESCRIPTION index 9fa7a5f..b6f399f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,6 +40,6 @@ Suggests: Enhances: brms, rstanarm -RoxygenNote: 7.1.1 +RoxygenNote: 7.1.2 VignetteBuilder: knitr Roxygen: list(markdown = TRUE) diff --git a/NEWS.md b/NEWS.md index b5a3422..74ce2ed 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,10 @@ # interactions 1.1.6.9000 * Website and some documentation have been revamped and upgraded. +* `johnson_neyman()` now handles non-syntactic variable names for `modx` +correctly. (#56) +* Users can now change the axis labels for `johnson_neyman()` plots via the +arguments `y.label` and `modx.label`. (#56) # interactions 1.1.5 diff --git a/R/johnson_neyman.R b/R/johnson_neyman.R index 1d5e639..e38ad9c 100644 --- a/R/johnson_neyman.R +++ b/R/johnson_neyman.R @@ -68,6 +68,16 @@ #' e.g., `c(0, 10)`. #' #' @param title The plot title. `"Johnson-Neyman plot"` by default. +#' +#' @param y.label If you prefer to override the automatic labelling of the +#' y axis, you can specify your own label here. The y axis represents a +#' *slope* so it is recommended that you do not simply give the name of the +#' predictor variable but instead make clear that it is a slope. By default, +#' "Slope of [pred]" is used (with whatever `pred` is). +#' +#' @param modx.label If you prefer to override the automatic labelling of +#' the x axis, you can specify your own label here. By default, the name +#' `modx` is used. #' #' @details #' @@ -111,7 +121,7 @@ #' \item{plot}{The \code{ggplot} object used for plotting. You can tweak the #' plot like you could any other from \code{ggplot}.} #' -#' @author Jacob Long <\email{long.1377@@osu.edu}> +#' @author Jacob Long \email{jacob.long@@sc.edu} #' #' @family interaction tools #' @@ -150,7 +160,8 @@ johnson_neyman <- function(model, pred, modx, vmat = NULL, alpha = 0.05, digits = getOption("jtools-digits", 2), critical.t = NULL, sig.color = "#00BFC4", insig.color = "#F8766D", mod.range = NULL, - title = "Johnson-Neyman plot") { + title = "Johnson-Neyman plot", y.label = NULL, + modx.label = NULL) { # Evaluate the modx, mod2, pred args pred <- quo_name(enexpr(pred)) @@ -213,9 +224,9 @@ johnson_neyman <- function(model, pred, modx, vmat = NULL, alpha = 0.05, intterm <- intterms[which(inttermstf)] # Keep the index that is TRUE # Getting the range of the moderator - modrange <- range(model.frame(model)[,modx]) - modrangeo <- range(model.frame(model)[,modx]) # for use later - modsd <- sd(model.frame(model)[,modx]) # let's expand outside observed range + modrange <- range(model.frame(model)[,un_bt(modx)]) + modrangeo <- range(model.frame(model)[,un_bt(modx)]) # for use later + modsd <- sd(model.frame(model)[,un_bt(modx)]) # let's expand outside observed range if (is.null(mod.range)) { modrange[1] <- modrange[1] - modsd modrange[2] <- modrange[2] + modsd @@ -565,6 +576,20 @@ johnson_neyman <- function(model, pred, modx, vmat = NULL, alpha = 0.05, ggplot2::theme(legend.key.size = ggplot2::unit(1, "lines")) + # Let users relabel the axis without changing source data + if (!is.null(y.label)) { + # If I don't think they realize it's a slope, give a message. + if (!grepl("slope", tolower(y.label))) { + msg_wrap("The y-axis represents a slope. Make sure you choose a label + that makes it clear it is the slope of ", pred, " rather than the + value of ", pred, ".") + } + plot <- plot + ggplot2::ylab(y.label) + } + + if (!is.null(modx.label)) { + plot <- plot + ggplot2::xlab(modx.label) + } out$plot <- plot diff --git a/man/johnson_neyman.Rd b/man/johnson_neyman.Rd index 11ef7b2..b3d9366 100644 --- a/man/johnson_neyman.Rd +++ b/man/johnson_neyman.Rd @@ -19,7 +19,9 @@ johnson_neyman( sig.color = "#00BFC4", insig.color = "#F8766D", mod.range = NULL, - title = "Johnson-Neyman plot" + title = "Johnson-Neyman plot", + y.label = NULL, + modx.label = NULL ) } \arguments{ @@ -87,6 +89,16 @@ observed data is not highlighted in the plot. Provide the range as a vector, e.g., \code{c(0, 10)}.} \item{title}{The plot title. \code{"Johnson-Neyman plot"} by default.} + +\item{y.label}{If you prefer to override the automatic labelling of the +y axis, you can specify your own label here. The y axis represents a +\emph{slope} so it is recommended that you do not simply give the name of the +predictor variable but instead make clear that it is a slope. By default, +"Slope of \link{pred}" is used (with whatever \code{pred} is).} + +\item{modx.label}{If you prefer to override the automatic labelling of +the x axis, you can specify your own label here. By default, the name +\code{modx} is used.} } \value{ \item{bounds}{The two numbers that make up the interval.} @@ -165,6 +177,6 @@ Other interaction tools: \code{\link{sim_slopes}()} } \author{ -Jacob Long <\email{long.1377@osu.edu}> +Jacob Long \email{jacob.long@sc.edu} } \concept{interaction tools}