added try catch for error handling
This commit is contained in:
parent
9c141326aa
commit
8033c1785f
3 changed files with 69 additions and 54 deletions
|
@ -5,27 +5,30 @@ getwd()
|
|||
############################################################
|
||||
# TASK: boxplots with stats
|
||||
############################################################
|
||||
|
||||
is.error <- function(x) inherits(x, "try-error")
|
||||
|
||||
doMyPlotsStats <- function(df) {
|
||||
mediators = levels(as.factor(df$mediator))
|
||||
mediators = levels(as.factor(df$mediator))
|
||||
|
||||
plots <- list()
|
||||
|
||||
for (i in mediators) {
|
||||
cat("Creating plot for:", i, "\n")
|
||||
single=df[df$mediator==i,]
|
||||
|
||||
plots <- list()
|
||||
max_y = max(single$value, na.rm = T)
|
||||
#cat("Plotting:", i, "max_y:", max_y, "\n")
|
||||
|
||||
for (i in mediators) {
|
||||
cat("Plotting:", i, "\n")
|
||||
single=df[df$mediator==i,]
|
||||
|
||||
max_y = max(single$value, na.rm = T)
|
||||
cat("Plotting:", i, "max_y:", max_y, "\n")
|
||||
|
||||
#----------
|
||||
# boxplot
|
||||
#----------
|
||||
p = ggplot(single)+ geom_boxplot(aes(x = timepoint
|
||||
, y = value
|
||||
, color = obesity
|
||||
#, palette = c("#00BFC4", "#F8766D")
|
||||
))+
|
||||
scale_colour_manual(values=c("#00BFC4", "#F8766D")) +
|
||||
#----------
|
||||
# boxplot
|
||||
#----------
|
||||
p = ggplot(single)+ geom_boxplot(aes(x = timepoint
|
||||
, y = value
|
||||
, color = obesity
|
||||
#, palette = c("#00BFC4", "#F8766D")
|
||||
))+
|
||||
scale_colour_manual(values=c("#00BFC4", "#F8766D")) +
|
||||
theme(axis.text.x = element_text(size = 15)
|
||||
, axis.text.y = element_text(size = 15
|
||||
, angle = 0
|
||||
|
@ -36,11 +39,14 @@ doMyPlotsStats <- function(df) {
|
|||
, legend.position = "none"
|
||||
, plot.subtitle = element_text(size = 20, hjust = 0.5)
|
||||
, plot.title = element_text(size = 20, hjust = 0.5)) +
|
||||
labs(title = i)
|
||||
labs(title = i)
|
||||
|
||||
#--------
|
||||
# stats
|
||||
#---------
|
||||
stats_state <- try({
|
||||
cat('Calculating stats for:', i, "\n")
|
||||
|
||||
#--------
|
||||
# stats
|
||||
#---------
|
||||
stat_df <- single %>%
|
||||
group_by(timepoint, mediator) %>%
|
||||
wilcox_test(value ~ obesity, paired = F) %>%
|
||||
|
@ -49,17 +55,24 @@ doMyPlotsStats <- function(df) {
|
|||
|
||||
stat_df <- stat_df %>%
|
||||
add_xy_position(x = "timepoint", dodge = 0.8)
|
||||
p2 = p + stat_pvalue_manual(stat_df
|
||||
#, y.position = max_y
|
||||
, label = "{p_format} {p.signif}"
|
||||
, hide.ns = T
|
||||
, tip.length = 0)+
|
||||
scale_y_continuous(expand = expansion(mult = c(0.05, 0.25)))
|
||||
plots[[i]] <- p2
|
||||
}, silent=TRUE)
|
||||
#print(stats_state)
|
||||
if ( is.error(stats_state)) {cat("⚠️ Stats ERROR:", i, "\n")}
|
||||
if ( ! is.error(stats_state)) {
|
||||
cat('Adding stats to plot for:', i, "\n")
|
||||
|
||||
p = p + stat_pvalue_manual(stat_df
|
||||
#, y.position = max_y
|
||||
, label = "{p_format} {p.signif}"
|
||||
, hide.ns = T
|
||||
, tip.length = 0)+
|
||||
scale_y_continuous(expand = expansion(mult = c(0.05, 0.25)))
|
||||
}
|
||||
return(plots)
|
||||
plots[[i]] <- p
|
||||
|
||||
}
|
||||
return(plots)
|
||||
}
|
||||
|
||||
###############################################################
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue