R ggplot2:在线图中添加显着性水平

时间:2018-02-02 08:02:07

标签: r testing ggplot2

我有以下MWE,其中我制作了一些带有刻面的线图。

我的数据位于mymelt数据框中,我想在情节中添加显着性值,存储在pvmelt数据框中。

我打算使用geom_text尝试此操作,但我想知道是否有更正确的方法可以使用geom_signif

这是我到目前为止的MWE:

##MWE
library(reshape2)
library(ggplot2)
set.seed(3)
mydf <- data.frame(treatment=rep(c('control','drug'),each=3), time=rep(c('week1','week2','week3'),2), fit1=runif(6, 0, 10), fit2=runif(6, 0, 10))
mymelt <- melt(mydf, measure.vars=c('fit1','fit2'))
mymelt
pvdf <- data.frame(contrast='drug - control', time=c('week1','week2','week3'), pv1=runif(3, 0, 0.075), pv2=runif(3, 0, 0.075))
pvmelt <- melt(pvdf, measure.vars=c('pv1','pv2'))
pvmelt$map.signif <- ifelse(pvmelt$value > 0.05, "", ifelse(pvmelt$value > 0.01,"*", "**"))
pvmelt
png(filename='test.png', height=500, width=800)
print(
    ggplot(mymelt, aes(x=time, y=value, group=treatment, col=treatment, shape=treatment)) +
    facet_grid(variable~.) +
    geom_line(aes(lty=treatment), size=1.5) +
    geom_point(alpha=0.5, size=4)
)
dev.off()

mymelt看起来像这样:

> mymelt
   treatment  time variable    value
1    control week1     fit1 1.680415
2    control week2     fit1 8.075164
3    control week3     fit1 3.849424
4       drug week1     fit1 3.277343
5       drug week2     fit1 6.021007
6       drug week3     fit1 6.043941
7    control week1     fit2 1.246334
8    control week2     fit2 2.946009
9    control week3     fit2 5.776099
10      drug week1     fit2 6.309793
11      drug week2     fit2 5.120159
12      drug week3     fit2 5.050239

虽然pvmelt看起来像这样:

> pvmelt
        contrast  time variable       value map.signif
1 drug - control week1      pv1 0.040052652          *
2 drug - control week2      pv1 0.041793708          *
3 drug - control week3      pv1 0.065093962           
4 drug - control week1      pv2 0.062228152           
5 drug - control week2      pv2 0.008358687         **
6 drug - control week3      pv2 0.052776627           

我生成的情节就是这个:

test1

但我想要这样的事情,pvmelt map.signif信息:

test2

实现它的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

以下内容应创建一个类似于您正在寻找的图表:

library(dplyr)

# change pvmelt's variable values to match mymelt's variable values
levels(pvmelt$variable) <- levels(mymelt$variable)

# join data frames
df <- left_join(mymelt, 
                pvmelt %>% select(time, variable, map.signif), 
                by = c("time", "variable"))

ggplot(df,
       aes(x = time, y = value, 
           col = treatment, shape = treatment, 
           group = treatment, lty = treatment,
           label = map.signif)) +
  facet_grid(variable~ .) +
  geom_line(size = 1.5) +
  geom_point(alpha = 0.5, size = 4) +
  geom_text(aes(y = max(value) + 0.3), # position slightly above highest point
            color = "black", size = 10) 

plot