I have a bunch of separate code chunks to run normality tests in R, and I would like to be able to combine them so that I can test specific variables without copying the code each time. So far, all the individual code chunks are working (using the iris dataset as an example):
library(datasets)
library(tidyverse)
library(skimr)
data(iris)
iris$Species <- NULL
# descriptive statistics and normality tests
skim(iris$Sepal.Length)
round(stat.desc(iris$Sepal.Length, basic = FALSE, norm = TRUE), digits = 3)
# histogram with normality curve
hist_sepal_length <- ggplot(iris, aes(Sepal.Length)) +
geom_histogram(aes(y = ..density..), bins = 10, colour = "black", fill = "white") +
labs(x = "Sepal.Length", y = "Density") +
stat_function(fun = dnorm, args = list(mean = mean(iris$Sepal.Length), sd = sd(iris$Sepal.Length)), colour = "black", size = 1)
hist_sepal_length
# qqplot
qqplot_sepal_length <- qplot(sample = iris$Sepal.Length)
qqplot_sepal_length
I can do the first step of the descriptive statistics using sapply
round(sapply(iris, stat.desc, basic = FALSE, norm = TRUE), digits = 3)
However, I am not sure how to use any of the apply functions with ggplot2. I looked at the following questions:
How to use lapply with ggplot2 while indexing variables
using an apply function with ggplot2 to create bar plots for more than one variable in a data.frame
Using apply functions with ggplot to plot a subset of dataframe columns
Using lapply to make boxplots of a variable list
However, none of them quite cover what I want, since my ggplot also includes a stat_function which references the variable. I also would like the output in separate graphs. Is there a way to write the ggplot code so that it will run through all of the variables at once (so sepal length, sepal width, petal length, petal width)? I have the variables that I want to run the normality tests on already saved to a separate dataframe, so there's no need to subset.
Finally, is there a way that I could package the 3 steps together (normality tests, histogram, and qq plot) into one function?