1

我有一个相当复杂的图表(至少对我来说),我想为重要的比较添加星号。我已经在我的数据框的单独列中编码了重要的比较。这是我的数据:

data = structure(list(mask = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
4L), .Label = c("roi1", "roi2", "roi3", "roi4"), class = "factor"), 
    comp = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 
    3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("comp1", 
    "comp2", "comp3"), class = "factor"), type = structure(c(1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("rand", "real"
    ), class = "factor"), dv = c(0.5001, 0.5002, 0.5001, 0.5002, 
    0.5002, 0.7658, 0.5, 0.8692, 0.4998, 0.8568, 0.5001, 0.8814, 
    0.5001, 0.5002, 0.5003, 0.5001, 0.5, 0.6943, 0.5003, 0.669, 
    0.5, 0.6205, 0.5001, 0.686), std = c(0.0258, 0.0351, 0.0351, 
    0.0258, 0.0258, 0.0347, 0.0346, 0.0256, 0.0347, 0.0254, 0.0239, 
    0.0351, 0.0351, 0.0347, 0.0256, 0.0347, 0.0256, 0.0258, 0.0348, 
    0.0256, 0.0351, 0.0254, 0.0254, 0.0347), sig = c(0L, 0L, 
    0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    1L, 0L, 1L, 0L, 1L, 0L, 1L)), class = "data.frame", row.names = c(NA, 
-24L))

head(data)
  mask  comp type     dv    std sig
1 roi1 comp1 rand 0.5001 0.0258   0
2 roi1 comp1 real 0.5002 0.0351   0
3 roi1 comp2 rand 0.5001 0.0351   0
4 roi1 comp2 real 0.5002 0.0258   0
5 roi1 comp3 rand 0.5002 0.0258   0
6 roi1 comp3 real 0.7658 0.0347   1

这是我的图表的代码:

ggplot(data=data, aes(x=comp, y=dv, fill=type)) +
  geom_bar(stat="identity", position=position_dodge()) +
  theme_bw() + facet_wrap(. ~ mask, ncol=2) + 
  scale_y_continuous(name = "accuracy", limits = c(0, 1)) +
  geom_errorbar(aes(ymin=dv-std, ymax=dv+std), width=.2,
                position=position_dodge(.9))

当前图表

但是,我想在相应的实数行在 sig 列中为 1 的条形之间添加一个星号,例如在 roi1 comp3 rand 和 real 之间。完成的图表应如下所示:

所需图表

有谁知道如何做到这一点?

干杯,马克斯

4

2 回答 2

1

使用以下代码

library(tidyverse)

ggplot(data=data, aes(x=comp, y=dv, fill=type)) +
  geom_bar(stat="identity", position=position_dodge()) +
  theme_bw() + facet_wrap(. ~ mask, ncol=2) + 
  scale_y_continuous(name = "accuracy", limits = c(0, 1)) +
  geom_errorbar(aes(ymin=dv-std, ymax=dv+std), width=.2,
                position=position_dodge(.9))+
  geom_point(data = data[data$sig ==1, ],aes(x=comp, y=0.99), 
             shape = "*", size=8, show.legend = FALSE)

在此处输入图像描述

于 2020-09-18T10:50:47.383 回答
1

您可以添加一个geom_text图层来执行此操作。如果需要,您可以为每个图层设置不同的数据。在这种情况下,我改变了该层的输入数据以包含所需的标签sig == 1,否则为空字符。

ggplot(data=data, aes(x=comp, y=dv, fill=type)) +
  geom_bar(stat="identity", position=position_dodge()) +
  theme_bw() + facet_wrap(. ~ mask, ncol=2) + 
  scale_y_continuous(name = "accuracy", limits = c(0, 1)) +
  geom_errorbar(aes(ymin=dv-std, ymax=dv+std), width=.2,
                position=position_dodge(.9)) +
  geom_text(data = data %>% mutate(sig1 = if_else(sig == 1, "*", "")),
            aes(x = comp, y = 0.95, label = sig1))

在此处输入图像描述

于 2020-09-18T10:51:21.593 回答