Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

r - Using ggsignif with grouped bar graphs and facet_wrap not working

I am trying to use ggsignif for displaying significant stars in top of paired bar graphs using facet_wrap. However, I can′t manage to find a way of displaying one significant bar per facet. Here is what I mean:

    dat <- data.frame(Group = c("S1", "S1", "S2", "S2"),
                  Sub   = c("A", "B", "A", "B"),
                  Value = c(3,5,7,8))

    ggplot(dat, aes(Group, Value)) +
      geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) +
      geom_signif(y_position=c(5.3, 8.3), xmin=c(0.8, 1.8), xmax=c(1.2, 2.2),
              annotation=c("**", "NS"), tip_length=0) +
      scale_fill_manual(values = c("grey80", "grey20")) +
      facet_grid(~ Group, scales = "free")

output

Is there a way of making sure that each facet has its individual significance label?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The main problem seems to me is that the geom_signif layer doesn't know to what panel the variables go to, since it has no data argument provided.

I'm not that familiar with the package, but the documentation seems to suggest that manual = TRUE is recommended for plotting it in different facets. Doing that and making some adjustments for the errors that were thrown, I got the following to work:

ggplot(dat, aes(Group, Value)) +
  geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) +
  geom_signif(data = data.frame(Group = c("S1","S2")),
              aes(y_position=c(5.3, 8.3), xmin=c(0.8, 0.8), xmax=c(1.2, 1.2),
              annotations=c("**", "NS")), tip_length=0, manual = T) +
  scale_fill_manual(values = c("grey80", "grey20")) +
  facet_grid(~ Group, scales = "free")

enter image description here

The key seemed to be to provide a data argument from which the facetting code could deduce what bit goes in what panel.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...