updating the function

This commit is contained in:
Tanushree Tunstall 2020-10-31 19:52:18 +00:00
parent 9c67a208e2
commit 6575fc7aba
2 changed files with 48 additions and 38 deletions

View file

@ -3,8 +3,7 @@ getwd()
setwd("~/git/mosaic_2020/") setwd("~/git/mosaic_2020/")
getwd() getwd()
############################################################ ############################################################
# TASK: boxplots at T1 # TASK: boxplots with stats
# FIXME: currently not rendering, problem with NAs for stats?
############################################################ ############################################################
doMyPlots <- function(x, title = "TEST") { doMyPlots <- function(x, title = "TEST") {
mediators = levels(as.factor(x$mediator)) mediators = levels(as.factor(x$mediator))
@ -18,30 +17,30 @@ doMyPlots <- function(x, title = "TEST") {
max_y = max(single$value, na.rm = T) max_y = max(single$value, na.rm = T)
cat("Plotting:", i, "max_y:", max_y, "\n") cat("Plotting:", i, "max_y:", max_y, "\n")
#----------
# boxplot # boxplot
#----------
p2 = ggboxplot(single p2 = ggboxplot(single
, x = "timepoint" , x = "timepoint"
, y = "value" , y = "value"
, color = "obesity" , color = "obesity"
, palette = c("#00BFC4", "#F8766D")) #, palette = c("blue", "red")
, palette = c("#00BFC4", "#F8766D")) +
p2 = p2 +theme(axis.text.x = element_text(size = 15) theme(axis.text.x = element_text(size = 15)
, axis.text.y = element_text(size = 15 , axis.text.y = element_text(size = 15
, angle = 0 , angle = 0
, hjust = 1 , hjust = 1
, vjust = 0) , vjust = 0)
, axis.title.x = element_blank() , axis.title.x = element_blank()
, axis.title.y = element_ , axis.title.y = element_blank()
, plot.title = element_text(size = 20, hjust = 0.5) , legend.position = "none"
, strip.text.x = element_text(size = 15, colour = "black") , plot.subtitle = element_text(size = 20, hjust = 0.5)
, legend.title = element_text(color = "black", size = 20) , plot.title = element_text(size = 20, hjust = 0.5)) +
, legend.text = element_text(size = 15) labs(title = i)
, legend.direction = "horizontal") + #--------
labs(title = title
, x = ""
, y = "Levels")
# stats # stats
#---------
stat_npa2 <- single %>% stat_npa2 <- single %>%
group_by(timepoint, mediator) %>% group_by(timepoint, mediator) %>%
wilcox_test(value ~ obesity, paired = F) %>% wilcox_test(value ~ obesity, paired = F) %>%
@ -51,15 +50,16 @@ doMyPlots <- function(x, title = "TEST") {
stat_npa2 <- stat_npa2 %>% stat_npa2 <- stat_npa2 %>%
add_xy_position(x = "timepoint", dodge = 0.8) add_xy_position(x = "timepoint", dodge = 0.8)
# add stats to boxplot
p2 = p2 + stat_pvalue_manual(stat_npa2 p2 = p2 + stat_pvalue_manual(stat_npa2
, y.position = max_y #, y.position = max_y
, label = "{p} {p.signif}" , label = "{p} {p.signif}"
, hide.ns=T , hide.ns=T
, tip.length = 0) + , tip.length = 0)+
scale_y_continuous(expand = expansion(mult = c(0.05, 0.25))) scale_y_continuous(expand = expansion(mult = c(0.05, 0.25))) +
scale_x_discrete()
plots[[i]] <- p2 plots[[i]] <- p2
} }
return(plots) return(plots)
@ -70,26 +70,32 @@ doMyPlots <- function(x, title = "TEST") {
# test # test
#============ #============
source("boxplot_linear.R") source("boxplot_linear.R")
#============= #=============
# Output: # Output:
#============= #=============
outfile_bp = paste0("boxplots_TEST_stats_all", ".pdf") outfile_bp = paste0("boxplots_TEST", ".pdf")
output_boxplot_stats = paste0(outdir_plots, outfile_bp); output_boxplot_stats output_boxplot_stats = paste0(outdir_plots, outfile_bp); output_boxplot_stats
pdf(output_boxplot_stats, width=22, height=16)
pdf(output_boxplot_stats, width=32, height=18)
med_names = c("eotaxin3", "il12p70", "itac", "il13") med_names = c("eotaxin3", "il12p70", "itac", "il13")
#lf_test = lf_fp_npa[lf_fp_npa$mediator == "il12p70",]
lf_test = lf_fp_npa[lf_fp_npa$mediator%in%med_names,] lf_test = lf_fp_npa[lf_fp_npa$mediator%in%med_names,]
foo=doMyPlots(lf_test) foo=doMyPlots(lf_test)
baz=cowplot::plot_grid(plotlist=foo, align = 'hv', ncol=2, nrow=2) #baz=cowplot::plot_grid(plotlist=foo, align = 'hv', ncol=2, nrow=2)
baz baz
dev.off()
#=========================
# entire sample_type data # entire sample_type data
#bar=doMyPlots(lf_fp_npa) #=========================
my_sample_type = "NPA"
outfile_bp = paste0("boxplots_stats_", my_sample_type, ".pdf")
output_boxplot_stats = paste0(outdir_plots, outfile_bp); output_boxplot_stats
pdf(output_boxplot_stats, width=22, height=16)
bar=doMyPlots(lf_fp_npa)
#bang=cowplot::plot_grid(plotlist=bar, align = 'hv', ncol=7, nrow=5) #bang=cowplot::plot_grid(plotlist=bar, align = 'hv', ncol=7, nrow=5)
#bang #bang
dev.off()
dev.off()

18
boxplot_with_stats.R Executable file → Normal file
View file

@ -62,15 +62,21 @@ p2 = p2 + stat_pvalue_manual(stat_npa2
, hide.ns=T , hide.ns=T
, tip.length = 0)+ , tip.length = 0)+
scale_y_continuous(expand = expansion(mult = c(0.05, 0.25))) + scale_y_continuous(expand = expansion(mult = c(0.05, 0.25))) +
scale_x_discrete()
plots[[i]] <- p2 plots[[i]] <- p2
} }
#============================
# combine: cowplot_plot_grid
#============================
#cowplot::plot_grid(plotlist=plots, align = 'hv', ncol=2, nrow=2) #cowplot::plot_grid(plotlist=plots, align = 'hv', ncol=2, nrow=2)
pdf("test.pdf", width = 22, height = 16)
cowplot::plot_grid(plotlist=plots, align = 'hv', ncol=7, nrow=5)
dev.off()
#===========================
# combine: ggpubr::ggarrange
#===========================
pdf("test2.pdf", width = 22, height = 16) pdf("test2.pdf", width = 22, height = 16)
npa_plot<- ggpubr::ggarrange(plotlist = plots, align = "hv" npa_plot<- ggpubr::ggarrange(plotlist = plots, align = "hv"
, ncol = 7 , ncol = 7
, nrow = 5 , nrow = 5
@ -86,9 +92,7 @@ annotate_figure(npa_plot,
#fig.lab = "Figure 1", fig.lab.face = "bold" #fig.lab = "Figure 1", fig.lab.face = "bold"
) )
pdf("test.pdf", width = 22, height = 16)
cowplot::plot_grid(plotlist=plots, align = 'hv', ncol=7, nrow=5)
dev.off()
################################################################## ##################################################################
#======= #=======
# facet # facet