Although the basic question I'm asking has been addressed in at least one previous post, the method described there has not worked for me, nor has the proposed solution, discussed here.
For reference, I'm trying to re-create the figure linked here using ggplot2
in R.
Here's a simplified version of the data frame I'm using:
df <-data.frame(
Related = c("Rel","Rel","Unrel","Unrel"),
Integrated = c("Integ", "Unint", "Integ", "Unint"),
Rate = c(11,8,6,4),
SE = c(1.5,1.4,1.3,1.2))
In terms of the bars, SE error bars, axes, and general formatting, I've essentially accomplished what I've needed so far using the following (though, please note I'm handling the x-axes labels and reported p-value separately):
dodge <- position_dodge(width = 0.9)
g1 <- ggplot(data = df, aes(x = interaction(Integrated, Related), y = Rate, fill = interaction(Integrated, Related)))
g1 <- g1 + layer(geom="bar", stat="identity", position = position_dodge())
g1 <- g1 + scale_fill_manual(values=c("#990000", "#CC6666", "#000099", "#9999CC"))
g1 <- g1 + guides(fill=FALSE)
g1 <- g1 + geom_errorbar(aes(ymax = Rate + SE, ymin = Rate - SE), position = dodge, width = 0.2)
g1 <- g1 + coord_cartesian(ylim = c(0, 15))
g1 <- g1 + scale_y_continuous(breaks=seq(0, 14, 2))
g1 <- g1 + theme_classic()
g1 <- g1 + theme(text = element_text(size=20))
g1 <- g1 + ylab("Mismatch effect (%)")
g1 <- g1 + theme(axis.title.y=element_text(vjust=1.5))
g1 <- g1 + theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank())
The trouble being when I try to add the significance comparisons/relationship bars shown in the top of the figure. When I attempt to just add the asterisk and the top bar (without the second-tier branches)using the following code, it returns the error listed below and displays only the asterisk in the appropriate location.
The code:
g1 + geom_path(x=c(1.5,1.5,3.5,3.5),y=c(13,14,14,13))+ annotate("text",x=2.5,y=15,label="*")
Error:
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
After researching potential sources of the problem (such as the post linked above), I've tried creating a separate df
and explicitly referring to that as follows:
The code:
data2 <- data.frame(x = c(1.5, 1.5, 3.5, 3.5), y = c(13, 14, 14, 13))
g1 + geom_path(data = data2, aes(x = x, y = y))
But again, I receive an error:
The error:
Error in interaction(Integrated, Related) : object 'Integrated' not found
I've spent a lot of time trying to solve this problem but to no avail. I'd love to be able to recreate the image inked above. I'm really thankful for any help.