Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show statistically significant difference in a graph

Tags:

r

ggplot2

I have carried out an experiment with six treatments and each treatment was performed in the light and darkness. I have used ggplot2 to make bar plot graph. I would like add the significance letters (e.g. LSD result) into the graph to show the difference between light and darkness for each treatment but it gives me an error. Any suggestion?

data <- read.table(header = TRUE, text = 
'T0 T1 T2 T3 T4 T5   LVD
40 62 50 45 45 58 Light
30 60 44 40 30 58 Light
30 68 42 35 32 59 Light
47 75 58 55 50 70  Dark
45 75 52 54 42 78  Dark
50 75 68 48 56 75  Dark
')

gla <- melt(data,id="LVD")
ggplot(gla, aes(x=variable, y=value, fill=as.factor(LVD))) + 
  stat_summary(fun.y=mean, 
               geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) + 
  stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar",
               color="black",position=position_dodge(.7), width=.2) +
  scale_fill_manual("Legend", values = c("Light" = "white", "Dark" ="gray46")) + 
  xlab("Treatments")+
  ylab("Germination % ") +
  theme(panel.background = element_rect(fill = 'white', colour = 'black'))

enter image description here

till here it perfectly works but when I use geom_text it gives an error

+ geom_text(aes(label=c("a","b","a","a","a","a, a","b","a","b","a","b")))

The error is:

Error: Aesthetics must be either length 1 or the same as the data (36): label, x, y, fill
like image 399
user3801226 Avatar asked Sep 28 '17 07:09

user3801226


People also ask

How do you find the statistically significant difference?

Start by looking at the left side of your degrees of freedom and find your variance. Then, go upward to see the p-values. Compare the p-value to the significance level or rather, the alpha. Remember that a p-value less than 0.05 is considered statistically significant.

How do you prove statistical significance?

To carry out a Z-test, find a Z-score for your test or study and convert it to a P-value. If your P-value is lower than the significance level, you can conclude that your observation is statistically significant.


1 Answers

The problem is that you have 36 data points, which you summarize to 12. ggplot will only allow mapping to 36 data points in geom_text (which the error tells you). In order to use the summarized 12 points, you do need to use stat_summary once again.

The basic rule is that statistical transformations (like summaries) do *not* transfer between layers (i.e. geoms and stats). So geom_text has no idea what the y values computed by the original stat_summary actually are.

Then you also need to fix the typo in your letters.

We end up with:

ggplot(gla, aes(x=variable, y=value, fill=as.factor(LVD))) + 
  stat_summary(fun.y=mean, 
               geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) + 
  stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar",
               color="black",position=position_dodge(.7), width=.2) +
  stat_summary(geom = 'text', fun.y = max, position = position_dodge(.7), 
               label = c("a","b","a","a","a","a", "a","b","a","b","a","b"), vjust = -0.5) +
  scale_fill_manual("Legend", values = c("Light" = "white", "Dark" ="gray46")) + 
  xlab("Treatments") +
  ylab("Germination % ") +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 85)) +
  theme_bw()

enter image description here

I don't like dynamite plots, so here's my version:

let <- c("a","b","a","a","a","a", "a","b","a","b","a","b")
stars <- ifelse(let[c(TRUE, FALSE)] == let[c(FALSE, TRUE)], '', '*')

ggplot(gla, aes(x = variable, y = value)) + 
  stat_summary(aes(col = as.factor(LVD)), 
               fun.y=mean, fun.ymin = min, fun.ymax = max,
               position = position_dodge(.3), size = .7) + 
  stat_summary(geom = 'text', fun.y = max, position = position_dodge(.3), 
               label = stars, vjust = 0, size = 6) +
  scale_color_manual("Legend", values = c("Light" = "black", "Dark" ="gray46")) + 
  xlab("Treatments") +
  ylab("Germination % ") +
  scale_y_continuous(expand = c(0.1, 0)) +
  theme_bw()

enter image description here

like image 111
Axeman Avatar answered Sep 27 '22 19:09

Axeman