Skip to content

Commit

Permalink
Fix johnson_neyman failure with non-syntactic modx, add axis label opts
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
jacob-long committed Apr 12, 2022
1 parent c99249d commit 149f88f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ Suggests:
Enhances:
brms,
rstanarm
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
35 changes: 30 additions & 5 deletions R/johnson_neyman.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down Expand Up @@ -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
#'
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
16 changes: 14 additions & 2 deletions man/johnson_neyman.Rd

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

0 comments on commit 149f88f

Please sign in to comment.