55# ' @param data A \link[base]{data.frame} or \link[data.table]{data.table} computed from \link{diversity}.
66# ' @param values A column name of a continuous variable.
77# ' @param col_name A column name of a categorical variable.
8- # ' @param group_by A column name to perform grouped statistical test (default: NULL).
8+ # ' @param group_by A column name to perform grouped statistical test (default: \code{ NULL} ).
99# ' @param palette An object with names and hexcode or color names, see \link{colormap}.
10- # ' @param method A character variable indicating what method is used to compute the diversity.
11- # ' @param paired A boolean value to perform paired analysis in \link[stats]{wilcox.test}.
10+ # ' @param method A character variable indicating what method is used to compute the diversity (default: \code{"custom"}) .
11+ # ' @param paired A boolean value to perform paired analysis in \link[stats]{wilcox.test} (default: \code{FALSE}) .
1212# ' @param p.adjust.method A character variable to specify the p.adjust.method to be used (Default: fdr).
1313# ' @return A \link[ggplot2]{ggplot2} object to be further modified
1414# '
@@ -76,7 +76,7 @@ diversity_plot <- function(
7676 col_name ,
7777 group_by = NULL ,
7878 palette ,
79- method ,
79+ method = " custom " ,
8080 paired = FALSE ,
8181 p.adjust.method = " fdr"
8282 ) {
@@ -85,45 +85,49 @@ diversity_plot <- function(
8585 # --------------------------------------------------------------------#
8686
8787 if (! inherits(data , " data.frame" ) && ! inherits(data , " data.table" ))
88- cli :: cli_abort(" Data must be a {.cls data.frame} or {.cls data.table}." )
89-
90- if (! is.character(palette ))
91- cli :: cli_abort(" {.val {palette}} needs to contain characters." )
92-
93- if (! is.character(method )) {
94- cli :: cli_abort(" {.val {method}} needs to be a character {.cls vector}." )
95- }
96-
97- if (! is.character(values ) && length(values ) != 1 ) {
98- cli :: cli_abort(" {.val {values}} needs to contain characters with length of 1." )
88+ cli :: cli_abort(" {.val data} must be a {.cls data.frame} or {.cls data.table}." )
89+
90+ if (! is.character(values ) || length(values ) != 1 ) {
91+ cli :: cli_abort(" {.val values} needs to contain characters with length of 1." )
9992 } else if (! column_exists(values , data )) {
100- cli :: cli_abort(" The {.val values}} column does not exist in the provided {.arg data}." )
93+ cli :: cli_abort(" The {.val values} column does not exist in the provided {.arg data}." )
10194 }
10295
103- if (! is.character(col_name ) && length(col_name ) != 1 ) {
104- cli :: cli_abort(" {.val { col_name} } needs to contain characters with length of 1." )
96+ if (! is.character(col_name ) || length(col_name ) != 1 ) {
97+ cli :: cli_abort(" {.val col_name} needs to contain characters with length of 1." )
10598 } else if (! column_exists(col_name , data )) {
106- cli :: cli_abort(" The {.val { col_name} } column does not exist in the provided {.arg data}." )
99+ cli :: cli_abort(" The {.val col_name} column does not exist in the provided {.arg data}." )
107100 }
108101
109102 if (! is.null(group_by )) {
110- if (! is.character(group_by ) && length(group_by ) != 1 ) {
111- cli :: cli_abort(" {.val { group_by} } needs to contain characters with length of 1." )
103+ if (! is.character(group_by ) || length(group_by ) != 1 ) {
104+ cli :: cli_abort(" {.val group_by} needs to contain characters with length of 1." )
112105 } else if (! column_exists(group_by , data )) {
113- cli :: cli_abort(" The {.val { group_by} } column does not exist in the provided {.arg data}." )
106+ cli :: cli_abort(" The {.val group_by} column does not exist in the provided {.arg data}." )
114107 }
115108 }
109+
110+ if (! is.character(palette ))
111+ cli :: cli_abort(" {.val palette} needs to contain characters." )
116112
113+ if (! is.character(method )) {
114+ cli :: cli_abort(" {.val method} needs to be a character {.cls vector}." )
115+ }
116+
117+ if (! is.logical(paired ))
118+ cli :: cli_abort(" {.val paired} needs to be either `TRUE` or `FALSE`." )
119+
117120 if (! c(p.adjust.method %in% stats :: p.adjust.methods ))
118121 cli :: cli_abort(" {.val {p.adjust.method}} is not a valid option. \n Valid options: {.val {p.adjust.methods}}" )
119122
120123 # # MAIN
121124 # --------------------------------------------------------------------#
122125
126+ data_tmp <- data.table :: copy(data )
123127 result <- list ()
124128
125129 if (! is.null(group_by )) {
126- pvalues_adjusted <- data [, {
130+ pvalues_adjusted <- data_tmp [, {
127131 tmp <- rstatix :: pairwise_wilcox_test(
128132 data = .SD ,
129133 formula = stats :: reformulate(col_name , response = values ),
@@ -136,17 +140,17 @@ diversity_plot <- function(
136140 }, by = group_by ]
137141
138142 # Creates box_stats for half geom_box
139- data.table :: setnames(data , old = group_by , new = " group_col" )
143+ data.table :: setnames(data_tmp , old = group_by , new = " group_col" )
140144 group_by <- " group_col"
141- box_stats <- data [, .(
145+ box_stats <- data_tmp [, .(
142146 ymin = base :: min(base :: get(values )),
143147 ymax = base :: max(base :: get(values )),
144148 lower = stats :: quantile(base :: get(values ), 0.25 ),
145149 middle = stats :: median(base :: get(values )),
146150 upper = stats :: quantile(base :: get(values ), 0.75 )
147151 ), by = .(group_numeric = as.numeric(as.factor(base :: get(col_name ))), group_col )]
148152 } else {
149- pvalues_adjusted <- data [, {
153+ pvalues_adjusted <- data_tmp [, {
150154 tmp <- rstatix :: pairwise_wilcox_test(
151155 data = .SD ,
152156 formula = stats :: reformulate(col_name , response = values ),
@@ -158,7 +162,7 @@ diversity_plot <- function(
158162 }]
159163
160164 # Creates box_stats for half geom_box
161- box_stats <- data [, .(
165+ box_stats <- data_tmp [, .(
162166 ymin = base :: min(base :: get(values )),
163167 ymax = base :: max(base :: get(values )),
164168 lower = stats :: quantile(base :: get(values ), 0.25 ),
@@ -169,7 +173,7 @@ diversity_plot <- function(
169173 pvalues_adjusted.filtered <- pvalues_adjusted [grepl(" \\ *" , pvalues_adjusted $ p.adj.signif ) ,]
170174
171175 plt <- ggplot2 :: ggplot(
172- data = data ,
176+ data = data_tmp ,
173177 mapping = ggplot2 :: aes(
174178 x = as.numeric(as.factor(.data [[col_name ]])),
175179 y = .data [[values ]]
@@ -276,8 +280,8 @@ diversity_plot <- function(
276280 ) +
277281 # Restore proper x-axis labels
278282 ggplot2 :: scale_x_continuous(
279- breaks = seq_along(unique(data [[col_name ]])),
280- labels = levels(as.factor(data [[col_name ]]))
283+ breaks = seq_along(unique(data_tmp [[col_name ]])),
284+ labels = levels(as.factor(data_tmp [[col_name ]]))
281285 ) +
282286 ggplot2 :: scale_colour_manual(
283287 name = " groups" ,
0 commit comments