R ggplot2:向折线图添加显着性水平 [英] R ggplot2: add significance level to line plot

查看:62
本文介绍了R ggplot2:向折线图添加显着性水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下MWE,其中我使用了多个构面来绘制线图.

I have the following MWE in which I make some line plots with facets.

我的数据在 mymelt 数据框中,并且我想向图添加显着性值,并存储在 pvmelt 数据框中.

My data is in the mymelt data frame, and I want to add significance values to the plot, stored in the pvmelt data frame.

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

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.

这是我到目前为止拥有的MWE:

This is the MWE I have so far:

##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           

我生成的情节就是这样的

The plot I generate is this one:

但是我想要这样的东西,并带有 pvmelt map.signif信息:

But I want something like this, with pvmelt map.signif information:

完成它的最好方法是什么?

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) 

这篇关于R ggplot2:向折线图添加显着性水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆