Reputation: 65
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'))
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
Upvotes: 5
Views: 4621
Reputation: 13
I fount it the simplest way to show the statistical significance with asterisks and lines. fig2 + geom_text(x=1.5,y=89, label = "***") + annotate("segment", x=c(1,1,2), xend=c(1,2,2), y=c(84,86,86), yend=c(86,86,84), size=1)adds 'geom_text' and 'annotate'
[1]: https://i.sstatic.net/fs0zN.png
Upvotes: -1
Reputation: 35242
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()
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()
Upvotes: 7