I'm trying to add significance lines in a box plot. I used the solutions from here and here. So I have this code:
ggplot(treatmentonly, aes(y = BagChange_pr, group = B, x = B )) +
stat_boxplot (geom="errorbar", width = 0.5) +
geom_boxplot (fill=c("#d55e00", "#cc79a7", "#0072b2", "#f0e442", "#009e73"),
outlier.size = 2, outlier.shape = 1) +
geom_line (aes(x = c(1, 1:4, 4),
y = c(10, 11, 11, 11, 11, 10)),
inherit.aes = FALSE )+
annotate("text", x = 2.5, y = 11.5, label = "*", size = 8) +
labs(x="Block", y="Envelope Weight Change [%]",
title = "B") +
theme(plot.title = element_text(hjust = 0.5))+
coord_cartesian(ylim = c(-4, 12))
But I get the following error: "Aesthetics must be either length 1 or the same as the data (40): x and y"
I don't understand what I did differently to the examples in the links, that would cause the problem. Who can help?
EDIT: Here is the data collected with dput(). I hope this is the right format, I'm sorry, I'm new to this.
structure(list(B = c("1", "1", "1", "1", "2", "2", "2", "2",
"3", "3", "3", "3", "4", "4", "4", "4", "5", "5", "5", "5", "1",
"1", "1", "1", "2", "2", "2", "2", "3", "3", "3", "3", "4", "4",
"4", "4", "5", "5", "5", "5"), BagChange_pr = c(-1.28888888888891,
-0.0444444444444025, -0.355555555555576, -0.0444444444444025,
9.82222222222216, 3.68888888888887, -0.355555555555576, -3.4666666666667,
1.20000000000005, 3.77777777777775, 1.55555555555562, -0.488888888888894,
3.95555555555556, 4.3555555555556, 2.31111111111109, 4.53333333333332,
1.55555555555562, -1.28888888888891, -0.400000000000023, 2.31111111111109,
-1.33333333333334, -1.37777777777779, 2.66666666666671, 3.19999999999998,
3.3333333333333, 0.488888888888916, 1.33333333333332, 5.82222222222226,
NA, 5.33333333333337, 3.28888888888892, -1.42222222222218, 4.17777777777775,
7.91111111111107, 6.53333333333335, 7.06666666666662, 7.24444444444448,
6.35555555555558, 1.55555555555562, 5.95555555555554)), row.names = c(NA,
-40L), class = c("tbl_df", "tbl", "data.frame"))
As you see B is not numeric although it's numbers. That's because this is just a subset of the data. In another subset there are actually characters, and I'll have to do the same chart with this subset later.