0

Please find the data that I need to visualize here. I want to reveal if there is any variable that is significantly different than the reference variable. I draw the barplot, list the variables, which will be compared with each other, make the statistics and draw the significance lines as you see below. It is all good up to this point.

library(tidyverse)
library(here)
library(readxl)
library(ggforce)
library(ggplot2)
library(ggsignif)
library(ggpubr)
library(rstatix)

data <- read.csv(here("data", "data.csv"), sep = ";")

# List the variables to be compared with each other in pairwise comparison
my_comparisons <- list( c("%0", "250 µg/ml"), c("%0", "100 µg/ml"), c("%0", "50 µg/ml"), c("%0", "25 µg/ml"))

#Statistics
stat.test <- data %>%
  t_test(OD ~ Material_Concentration, p.adjust.method = "bonferroni", comparisons = my_comparisons) %>%
  add_significance("p.adj")

stat.test

# add significance
stat.test <- stat.test %>%
  add_xy_position(x = "Material_Concentration")

#plot the data
p <- ggplot(data, aes(Material_Concentration, OD, colour = Material_Concentration))+          
  stat_summary(fun = mean, geom = "col", aes(fill = Material_Concentration), show.legend = FALSE)+
  stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2, show.legend = FALSE)+
  xlab("Treatment Concentrations")+
  ylab("Average OD")+
  theme(panel.background = element_blank(), plot.title = element_text(color="red", size=40, face="bold.italic", hjust = 0.5), 
        axis.text.x = element_text(size = 20, angle = 45, hjust = 1), 
        axis.text.y = element_text(size = 15), 
        axis.title.y = element_text(size = 15), 
        axis.title.x = element_text(size = 15))+
  stat_pvalue_manual(stat.test, label = "p.adj.signif")
  
p

Positioning of significance lines are correct in this image, however I want to rearrange the variables

However, when I rearrange the positions of variables on x-axis, by using scale-x-discrete the x-positions of significance lines do not change.

library(tidyverse)
library(here)
library(readxl)
library(ggforce)
library(ggplot2)
library(ggsignif)
library(ggpubr)
library(rstatix)

data <- read.csv(here("data", "data.csv"), sep = ";")

# List the variables to be compared with each other in pairwise comparison
my_comparisons <- list( c("%0", "250 µg/ml"), c("%0", "100 µg/ml"), c("%0", "50 µg/ml"), c("%0", "25 µg/ml"))

#Statistics
stat.test <- data %>%
  t_test(OD ~ Material_Concentration, p.adjust.method = "bonferroni", comparisons = my_comparisons) %>%
  add_significance("p.adj")

stat.test

# add significance
stat.test <- stat.test %>%
  add_xy_position(x = "Material_Concentration")

#plot the data
p <- ggplot(data, aes(Material_Concentration, OD, colour = Material_Concentration))+          
  stat_summary(fun = mean, geom = "col", aes(fill = Material_Concentration), show.legend = FALSE)+
  stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2, show.legend = FALSE)+
  xlab("Treatment Concentrations")+
  ylab("Average OD")+
  theme(panel.background = element_blank(), plot.title = element_text(color="red", size=40, face="bold.italic", hjust = 0.5), 
        axis.text.x = element_text(size = 20, angle = 45, hjust = 1), 
        axis.text.y = element_text(size = 15), 
        axis.title.y = element_text(size = 15), 
        axis.title.x = element_text(size = 15))+
  scale_x_discrete(limits = c("250 µg/ml", "100 µg/ml", "50 µg/ml", "25 µg/ml", "%0", "%10"))+
  stat_pvalue_manual(stat.test, label = "p.adj.signif")
  
p

Positioning of significance lines are not correct in this image

How can I solve this problem? I would be really glad if you may reveal my mistake.

Thank you

Erdem Erikçi
  • 33
  • 1
  • 6
  • I would suggest changing your data by making your `x` column a factor with the levels in the order that you want. Something like `data$Material_Concentration <- factor(data$Material_Concentration, levels = c("250 µg/ml", "100 µg/ml", "50 µg/ml", "25 µg/ml", "%0", "%10"))` immediately after reading in the data. If you need more help than that, please make your example [reproducible, as described at this link](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by sharing a small amount of sample data. – Gregor Thomas Sep 22 '21 at 17:29
  • Thank you for the suggestion. It worked perfectly. It is the first time I ask a question on this platform. I will take care of reproducibility next time. – Erdem Erikçi Sep 23 '21 at 13:56
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 24 '21 at 09:12

0 Answers0