I have difficulty drawing line in a ggplot. I am reciting the issue discussed here. The solution suggested there doesn't work when I have one more factor introduced in gplot aesthetics. Here is the code.
pp <- ggplot(mtcars, aes(factor(cyl), mpg, colour = factor(cyl))) + geom_boxplot()
df1 <- data.frame(a = c(1, 1:3,3), b = c(39, 40, 40, 40, 39))
df2 <- data.frame(a = c(1, 1,2, 2), b = c(35, 36, 36, 35))
df3 <- data.frame(a = c(2, 2, 3, 3), b = c(24, 25, 25, 24))
pp + geom_line(data = df1, aes(x = a, y = b)) + annotate("text", x = 2, y = 42, label = "*", size = 8) +
geom_line(data = df2, aes(x = a, y = b)) + annotate("text", x = 1.5, y = 38, label = "**", size = 8) +
geom_line(data = df3, aes(x = a, y = b)) + annotate("text", x = 2.5, y = 27, label = "n.s.", size = 8)
This produces an error Error in factor(cyl) : object 'cyl' not found
. I know that I am not describing the best data here, but I hope the problem is clear. I want to draw a uni-color line on ggplot and write the significance value above it irrespective of grouping in the plot.