2016-09-14 65 views
0

我有这样的RDATA:load(url('https://dl.dropboxusercontent.com/u/43417085/density_plot.RData'))如何绘制值范围R中

我想绘制图形密度像this,在那里我可以看到want.IBS列跨越每个cutoff项值的范围。另外,我的X轴应该在-2到2之间,Y轴应该只有截断项目(不是数值)。

这是我试过的,但我能看到的只有黑线。有没有其他方法可以做到这一点?

p <- ggplot(mymat, aes(x=as.numeric(want.IBS), fill=cutoff)) #as suggested 
p <- p + geom_density(alpha=0.5) 
p <- p + xlab ("IBD") + ylab("cutoff") 
p <- p + theme_bw() 
p 
+0

你将需要由cutoff..I第一熔化数据将发布的代码示例 –

+1

'want.IBS'是一个因素,所以'p < - ggplot(mymat,aes(x = as.numeric(want.IBS),fill = cutoff))'是一个很好的起点。 – Haboryme

+0

你是对的,那是更快 –

回答

4
mm<-mymat[,2:3] 
dd<-melt(mm, id.vars=c("cutoff")) 
ggplot(dd, aes(x= as.numeric(value), fill=cutoff))+ 
    geom_density(alpha=0.5) 
    +xlab ("IBD") + ylab("cutoff")+theme_bw() 

enter image description here

+1

那么你不是在寻找一个密度图 – Haboryme

+0

@MAPK,所以你想重塑的价值? –

+1

@MAPK运行''范围(as.numeric(as.character(mymat $ want.IBS)))''你的范围是''[1] -0.0759 1.3300''不是-2到2,所以我不知道是什么你在说什么。 –

0

这应该完成它..但考虑重新调整数据或只绘制大值?对于%>%运营商,您需要magrittr。或者您可以像Haboryme建议的那样使用原始数据框架(+1)。

 data.frame(cutoff=mymat$cutoff,want.IBS=as.numeric(mymat$want.IBS)) %>% 
ggplot(.,aes(want.IBS))+geom_density(aes(fill=cutoff),alpha=0.8) 

enter image description here