简体   繁体   English

ggplot2:如何在密度图中添加显着性条?

[英]ggplot2: how to add significance bar to density plot?

I am trying to generate a (grouped) density plot with significance test I did it with boxplots (with ggsignif packages) but I don't know how to do in densityplot 我正在尝试使用显着性测试生成(分组)密度图我用ggsignif图(使用ggsignif包)做了但我不知道如何在densityplot中做

setwd("~/myData")

data<-read.delim("genes.txt", head=T, row.names = 1)
data$Variance<-apply(data, 1, var)

head(data)
dim(data)

library(ggplot2)

library(ggsignif)

ggplot(data, aes(x=variance, col=type)) +geom_density() )
 geom_signif(comparisons = list(c("normal", "traited")),map_signif_level=TRUE)

i have this error: 我有这个错误:

error in f(...)
can oly handle data with groupes that are plotted on the x-axis 

在此输入图像描述

As written in the comments I guess there is type of bar is not implemented in ggsignif yet. 正如评论中所写的那样,我猜有没有在ggsignif实现bar的类型。 Here, a manual workaround with some manual pre- (means, pvalue) and postprocessing (make it look nice by tweaking vertical positioning). 这里是一个手动解决方法,包括一些手动预处理(均值,pvalue)和后处理(通过调整垂直定位使其看起来很漂亮)。

x = rnorm(10000,2)
y = rnorm(10000,-1)

require(ggplot2)

df1=data.frame(val=x,type="x")
df2=data.frame(val=y,type="y")
df.complete = rbind(df1,df2)

mean1 = mean(x)
mean2 = mean(y)
middle = (mean1 + mean2)/2

pval = 0.001 #replace by appropriate p-value from test of choice

g = ggplot(df.complete,aes(x=val,fill=type))
g = g + geom_density()
g = g + ylim(0,0.5) #enlarge sufficiently to have some space
g = g + geom_segment(aes(x=mean1,y=0.425,xend=mean1,yend=0.45)) #correct y and yend after visual inspection
g = g + geom_segment(aes(x=mean2,y=0.425,xend=mean2,yend=0.45)) #correct y and yend after visual inspection
g = g + geom_segment(aes(x=mean2,y=0.45,xend=mean1,yend=0.45))  #correct y and yend after visual inspection
g = g + geom_text(aes(x=middle,y=0.47,label=pval),hjust="center") #correct y after visual inspection
g

http://www.r-fiddle.org/#/fiddle?id=NUxpKHlv&version=2 http://www.r-fiddle.org/#/fiddle?id=NUxpKHlv&version=2

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

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