I have a problem with add_xy_position()
after using tukey_hsd()
from the rstatix package inside a function. This is what my function looks like:
make_tukey_test <- function (data,variable,grouping_variable){
data %>%
tukey_hsd(variable ~ grouping_variable)
}
When I call the function using the following code, it works perfectly fine and the test results are being saved in the data.frame:
test <- make_tukey_test(data = dat, variable = dat$num_var, grouping_variable = dat$factor_var)
However, when I try to add the x and y coordinates of dat$factor_var
using add_xy_position()
like this:
test <- test %>%
add_xy_position(x = “factor_var”)
I get the following error message:
Error: Must group by variables found in `.data`. * Column `grouping_variable` is not found.
However, when I use tukey_hsd()
outside of my function, the code works perfectly fine and the coordinates are being added to the data.frame.
I would greatly appreciate some helpful suggestions, as I have no clue why the code doesn't work when I´m using it inside my function.
CodePudding user response:
I would suggest to pass column names as strings. Since tukey_hsd
accepts a formula object you can use reformulate
to create that and pass the grouping_variable
as it is in add_xy_position
function.
Here is an example with mtcars
dataset.
library(rstatix)
library(dplyr)
dat <- mtcars %>% mutate(cyl = factor(cyl))
make_tukey_test <- function (data,variable,grouping_variable){
data %>%
tukey_hsd(reformulate(grouping_variable, variable)) %>%
add_xy_position(x = grouping_variable)
}
test <- make_tukey_test(data = dat, variable = "mpg", grouping_variable = "cyl")