2016-09-14 59 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