简体   繁体   English

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

[英]R ggplot2: add significance level to line plot

I have the following MWE in which I make some line plots with facets. 我有以下MWE,其中我使用多个构面绘制了一些线图。

My data is in the mymelt data frame, and I want to add significance values to the plot, stored in the pvmelt data frame. 我的数据在mymelt数据框中,并且我想向存储在pvmelt数据框中的图形添加有效值。

I was going to attempt this using geom_text , but I am wondering whether there is a more correct way of doing it with geom_signif . 我要尝试这种使用geom_text ,但我想知道是否有与它做一个比较正确的做法geom_signif

This is the MWE I have so far: 这是我到目前为止拥有的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 looks like this: 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

While pvmelt looks like this: 虽然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           

The plot I generate is this one: 我生成的图是这样的:

测试1

But I want something like this, with pvmelt map.signif information: 但是我想要这样的东西,并带有pvmelt map.signif信息:

测试2

What would be the best way to accomplish it? 最好的方法是什么?

The following should create a chart similar to what you're looking for: 以下内容应创建与所需内容相似的图表:

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) 

情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM