为躲避条形图添加显着性条形

时间:2017-10-31 00:39:34

标签: r ggplot2 tidyverse

我的数据看起来像这样:

     reading condition   time
       <dbl>     <chr> <fctr>
1  0.0603376         A      4
2  0.0000000         A      4
3  0.0576497         A      4
4 11.4080000         B      4
5 11.0495000         B      4
6 14.0659000         B      4
...

我希望使用ggplot2绘制一个条形图,并在具有自定义注释的躲闪列上显示重要条。我正在使用ggsignif扩展程序包。文档中的示例有效:

ggplot(mpg, aes(class, hwy)) +
  geom_boxplot() +
  geom_signif(annotations = c("First", "Second"),
              y_position = c(30, 40), xmin=c(4,1), xmax=c(5,3))

但是当我用我的数据尝试它时却没有。我试过调整参数,但看不出为什么它不起作用。有人可以帮忙吗?

df %>% 
ggplot(aes(time, reading,  fill = condition, group = condition)) +
  geom_bar(stat = "summary", fun.y = "mean", position = position_dodge(width = 0.5), width = 0.5) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_signif(annotations = c("First", "Second"),
              y_position = c(4, 12), xmin=c(1.5,2.5), xmax=c(2.5,3.5))

数据:

df <- structure(list(reading = c(0.0603376, 0, 0.0576497, 11.408, 11.0495, 
14.0659, 0, 0, 0, 1.00595, 0.862592, 1.93105, 0, 0, 0, 0.0726477, 
0.0778178, 0), condition = c("A", "A", "A", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "A", "A", "A", "A", "A", "A"), time = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 
2L), .Label = c("0", "2", "4"), class = "factor")), .Names = c("reading", 
"condition", "time"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -18L))

1 个答案:

答案 0 :(得分:0)

只需从代码的第一行删除group = condition

enter image description here

df %>% 
  ggplot(aes(time, reading,  fill = condition)) +
  geom_bar(stat = "summary", fun.y = "mean", position = position_dodge(width = 0.5), width = 0.5) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_signif(annotations = c("First", "Second"),
          y_position = c(4, 12), xmin=c(1.5,2.5), xmax=c(2.5,3.5))