├── README.rst ├── data_example.csv ├── plot_example.R ├── plot_example.png ├── tukeySignificanceLettersFunction.Rdata └── tukey_function.R /README.rst: -------------------------------------------------------------------------------- 1 | tukey_test_plot 2 | ================== 3 | 4 | Match tukey test results (letters or asteriks resulting from the HSD.test function) and set them 5 | to their corresponding bar in a barplot. It takes into account possible facets from ggplot2. 6 | 7 | -------------------------------------------------------------------------------- /data_example.csv: -------------------------------------------------------------------------------- 1 | Condition,WT,Treat1,Treat2,Treat3 2 | A,100,144.2539306,130.7659073,0 3 | A,100,146.0224139,145.529896,0 4 | A,100,134.6920264,143.6780096,306.4734019 5 | A,100,127.0179911,131.65245,296.929821 6 | A,100,132.2664043,0,301.8913208 7 | A,100,0,138.2632507,283.9499005 8 | A,100,121.8566817,137.1079693,315.5952886 9 | B,100,0,129.1794925,0 10 | B,100,87.84681752,0,133.2588449 11 | B,100,0,149.2984717,155.085099 12 | B,100,0,0,131.7327292 13 | B,100,0,151.4696964,161.5442101 14 | B,100,71.61632162,112.4261786,137.4076521 15 | B,100,0,122.5391303,0 16 | -------------------------------------------------------------------------------- /plot_example.R: -------------------------------------------------------------------------------- 1 | ################################################################################### 2 | # Load tukey significance function 3 | ################################################################################### 4 | 5 | load("tukeySignificanceLettersFunction.Rdata") 6 | 7 | ################################################################################### 8 | # Load data example and apply function 9 | ################################################################################### 10 | data_example <- read.table("./data_example.csv", sep =",", header=T) 11 | 12 | significance<-tukey_plot_significance(data_example) 13 | 14 | # Extract letters positions 15 | tukey_letters<-significance$tukey_letters 16 | stats<-significance$stats 17 | 18 | # Set asteriks position (so it won't fall on top of the error bars) 19 | offset<-7 20 | offset_asterisk<-10 21 | 22 | plot<- ggplot( tukey_letters, aes(x = variable, y = value.x, fill = L1)) + 23 | geom_bar(stat = "identity", position =position_dodge(),colour="black",width=.7,size=.5)+ 24 | geom_errorbar(aes(ymin=value.x-se, ymax=value.x+se), width=.1,size=.5,position=position_dodge(.7))+ 25 | geom_line(size=.8)+ 26 | theme( 27 | plot.title = element_text(lineheight=.8, face="bold",size=10), 28 | axis.title = element_text(size =8, face="bold"), 29 | axis.text = element_text(angle=45, vjust=1,size=8,face="bold"), 30 | axis.ticks = element_line(size = rel(2.5)), 31 | axis.ticks.length = unit(0.5, "cm") 32 | )+ 33 | labs( 34 | title="your title", 35 | x="x axis title", 36 | y="y axis title" 37 | )+ 38 | facet_wrap(~L1)+ ## You can use face_wrap function only if you need it+ 39 | geom_text(data =tukey_letters, 40 | aes(x=xpos, y=ymax+offset_asterisk,label=groups), 41 | size = 5,position=position_dodge(.5) 42 | ) 43 | plot 44 | -------------------------------------------------------------------------------- /plot_example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vicruiser/tukey_test_plot/4ffe0400fe7409aef36bc86b9c514772514d6e12/plot_example.png -------------------------------------------------------------------------------- /tukeySignificanceLettersFunction.Rdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vicruiser/tukey_test_plot/4ffe0400fe7409aef36bc86b9c514772514d6e12/tukeySignificanceLettersFunction.Rdata -------------------------------------------------------------------------------- /tukey_function.R: -------------------------------------------------------------------------------- 1 | ## >>>>>>>>>>>>> R version 5.1 2 | 3 | ################################################################################### 4 | # Required packages 5 | ################################################################################### 6 | library(ggplot2) 7 | library(grid) 8 | library(fBasics) 9 | library(tidyr) 10 | library(reshape2) 11 | library(agricolae) 12 | library(Rmisc) 13 | library(gdata) 14 | library(rlist) 15 | library(stringr) 16 | 17 | 18 | ################################################################################### 19 | # Tukey plot significance function 20 | ################################################################################### 21 | tukey_plot_significance<-function(data) 22 | { 23 | 24 | ##Convert 0 values to NA values 25 | data[data == 0] <- NA 26 | data_melted <- reshape2::melt(data) 27 | colnames(data_melted)[1] = 'X' 28 | stats <- summarySE(data_melted, measurevar="value", groupvars=c("X","variable"), na.rm=TRUE) 29 | names(stats)[1]<-"group" 30 | 31 | # ANOVA + Tukey test + statistics (mean,sd,SEM...) 32 | variable_list <- split(data_melted, f= data_melted$X) 33 | aov <- lapply(variable_list, function(x) aov(value~variable, data = x )) 34 | stats_data <- lapply(variable_list, function(x) summarySE(x, measurevar="value", groupvars=c("variable"),na.rm=TRUE) ) 35 | tukey <- lapply(aov, function(x) HSD.test(x,"variable",group=FALSE)) 36 | tukey_groups <- lapply(aov, function (x) HSD.test(x,"variable")) 37 | 38 | ##Tukey test with letter groups 39 | ##Loop to extract data groups, names, and x & y positions for plotting 40 | stats_data <- lapply(stats_data, function(x) {x$ymax <-x$value + x$se; 41 | row.names(x) <- x$variable; 42 | return(x)}) 43 | 44 | tukey_letters <- lapply(tukey_groups, function(x) x$groups) 45 | tukey_letters <- mapply(function(x, y) merge(x, y, by=0) , tukey_letters, stats_data, SIMPLIFY = FALSE) 46 | tukey_letters <- mapply(function(x, y) {y$xpos <- match( y$variable, x[,1]); return(y)}, stats_data, tukey_letters , SIMPLIFY = FALSE) 47 | 48 | 49 | ### Another way to compare our data statistically with tukey test is with asterisks. 50 | ## We are going to create a data frame of each experimental condition desired where 51 | ## p-value and asterisks will appear and their relation with x position and y position between the 52 | ## two groups compared. On this way, we are going to be able to draw a segment or path connecting both bars in our bar plot 53 | ## or as many as we want/have. 54 | 55 | 56 | 57 | ast <- lapply(tukey, function(x) subset(x$comparison, signif. != " ")) 58 | asterisks <- lapply(ast, function(x) cbind(x, 59 | setNames(object= data.frame(str_split_fixed(rownames(x)," - ",2)), c("g1", "g2"))) ) 60 | asterisks1<-asterisks 61 | path <- mapply( function(x, y) {x$x1 <- y[match(x$g1, y$variable), "xpos"]; return(x)}, 62 | asterisks, 63 | tukey_letters, 64 | SIMPLIFY = FALSE) 65 | path <- mapply( function(x, y) {x$x2 <- y[match(x$g2, y$variable), "xpos"]; return(x)}, 66 | path, 67 | tukey_letters, 68 | SIMPLIFY = FALSE) 69 | 70 | path <- mapply( function(x, y) {x$y1 <- y[match(x$g1, y$variable), "value.x"]; return(x)}, 71 | path, 72 | tukey_letters, 73 | SIMPLIFY = FALSE) 74 | path <- mapply( function(x, y) {x$y2 <- y[match(x$g2, y$variable), "value.x"]; return(x)}, 75 | path, 76 | tukey_letters, 77 | SIMPLIFY = FALSE) 78 | path <- lapply( path, function(x) {x$xmed <- as.numeric(rowMeans(x[,c("x1", "x2")])) ; return(x)}) 79 | 80 | path <- mapply( function(x, y) {x$ymax <- rowMaxs(x[,c("y1","y2")])+ max(y$se); return(x)}, 81 | path, 82 | tukey_letters, 83 | SIMPLIFY = FALSE) 84 | df_path = melt(path, id.vars= names(path[[1]])) 85 | df_tukey_letters = melt(tukey_letters, id.vars= names(tukey_letters[[1]])) 86 | 87 | ## To manipulate the resultant list (asterisks) we're going to change the name to "path". This data will be 88 | ## used for geom_segment() in ggplot 89 | 90 | 91 | #Resultant list has different data frames depending on the number of conditions. 92 | # Each data frame holds the following information: 93 | # - sig.=asterisks 94 | # - xmed=place to set the asterirsks, in the halfway between two compared groups 95 | # - ymax= max position of bar + position of erro bar, x1=x,x2=xend,y1=y,y2=yend are coordenates for geom_segment()) 96 | # 97 | 98 | # 99 | result <- list(data_melt = data_melted, 100 | aov = aov, 101 | summary.aov = summary(aov), 102 | tukey_letters = df_tukey_letters, 103 | stats = stats, 104 | stats_groups = stats_data, 105 | asterisks = asterisks, 106 | geom.path = df_path) 107 | return(result) 108 | } 109 | 110 | 111 | 112 | ################################################################################################## 113 | 114 | save("tukey_plot_significance", file="tukeySignificanceLettersFunction.Rdata") 115 | 116 | 117 | ################################################################################################## 118 | 119 | --------------------------------------------------------------------------------