4

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.

Community
  • 1
  • 1
Steve'sConnect
  • 145
  • 2
  • 8

1 Answers1

2

geom_path is expecting a grouping variable. Since there's no group, you need to add aes(group=1):

g1 + geom_path(aes(group=1), x=c(1.5,1.5,3.5,3.5), y=c(13,14,14,13)) +  
  annotate("text", x=2.5,y=14.4,label="*", size=8)

The second error, object 'Integrated' not found, is because your second data set needs to have the same aesthetic and facetting variables as the original data set, even if you don't explicitly use them for that layer of the plot. For example, this will work:

data2 <- data.frame(x = c(1.5, 1.5, 3.5, 3.5), y = c(13, 14, 14, 13),
                    Integrated=NA, Related=NA)
g1 + geom_path(data = data2, aes(x = x, y = y))

A few other comments that I hope will be of use:

1) You don't need to save g1 every time you add a new statement. You can just string them all together in a single command with + at the end of each statement:

g1 <- ggplot(data = mis.eff, aes(x = interaction(Integrated, Related), 
             y = Rate, fill = interaction(Integrated, Related))) + 
       layer(geom="bar", stat="identity", position = position_dodge()) +
       scale_fill_manual(values=c("#990000", "#CC6666", "#000099", "#9999CC")) +
       ... etc.

2) In your annotation, you set y=15, which is above the y-range of the plot, so it doesn't appear. I've changed the value to 14.4. You can also set the y limits manually if you want to change them.

3) I've increased the size of the asterisk so it's more prominent. You can of course adjust this as needed.

eipi10
  • 91,525
  • 24
  • 209
  • 285