1

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

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 

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
JaO
  • 27
  • 3
  • 1
    Welcome to SO. Please share some data and code to make your problem reproducible: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – scoa May 03 '17 at 08:33
  • 1
    Oh I just saw you expanded the question - my answer is now a little off as I did not realize you wanted to use `ggsignif`- which is btw nice to learn about! – CMichael May 03 '17 at 08:49
  • However, the error bar and the vignette of `ggsignif` seem to suggest that it does not yet work on non-bar type diagrams. So potentially you are interseted in my solution after all. – CMichael May 03 '17 at 08:54

1 Answers1

0

As written in the comments I guess there is type of bar is not implemented in ggsignif yet. Here, a manual workaround with some manual pre- (means, pvalue) and postprocessing (make it look nice by tweaking vertical positioning).

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

CMichael
  • 1,856
  • 16
  • 20