2013-06-30 37 views
1

我有一個數據幀,看起來像:ř產生具有一個圖形設備中的多個小提琴曲線

bin_with_regard_to_strand CLONE3 
31      0.14750872 
33      0.52735917 
28      0.48559060 
.       . 
.       . 

我想使用該數據幀以這樣的方式,所有的值的生成小提琴地塊CLONE3對應於給定值bin_with_regard_to_strand將生成一個圖。另外,我希望所有的圖都出現在同一個圖形設備中(我正在使用R-studio,並且我希望所有的圖都出現在一個繪圖窗口中)。 理論上我能做到這一點有:

vioplot(df$CLONE3[which(df$bin_with_regard_to_strand==1)], 
    df$CLONE3[which(df$bin_with_regard_to_strand==2)]...) 

但由於bin_with_regard_to_strand有60個不同的值,這似乎有點可笑。 我嘗試使用tapply

tapply(df$CLONE3, df$bin_with_regard_to_strand,vioplot) 

但是,這將打開60樓不同的窗口(每個情節)。 或者,如果我使用了add參數:

tapply(df$CLONE3, df$bin_with_regard_to_strand,vioplot(add=TRUE)) 

產生用數據的單個情節從所有值bin_with_regard_to_strand(由線分隔)。

有沒有辦法做到這一點?

+1

嘗試'ggplot2'這裏有一些例子http://docs.ggplot2.org/current/geom_violin.html – dickoa

回答

1

您可以使用par(mfrow=c(rows, columns))(詳情請參閱?par)。

(也參見?layout爲絡合劑安排)

d <- lapply(1:6, function(x)runif(100)) # generate some example data 

library("vioplot") 

par(mfrow=c(3, 2)) # use a 3x2 (rows x columns) layout 

lapply(d, vioplot) # call plot for each list element 

par(mfrow=c(1, 1)) # reset layout 

vioplots

+0

謝謝!那完全是我想要的 – user1614062

0

另一個替代mfrow,是使用layout。組織你的情節非常方便。您只需創建一個帶有圖表索引的矩陣。在這裏你可以做什麼。看來有60個箱子是一個龐大的數字。也許你應該組織他們2頁。

enter image description here

在N功能下面的代碼(地塊號)

library(vioplot) 
N <- 60 
par(mar=rep(2,4)) 
layout(matrix(c(1:N), 
       nrow=10,byrow=T)) 
dat <- data.frame(bin_with_regard_to_strand=gl(N,10),CLONE3=rnorm(10*N)) 
with(dat , 
    tapply(CLONE3,bin_with_regard_to_strand ,vioplot)) 
0

這是一個老問題,但儘管我會推出不同的解決方案,讓vioplot進行多次小提琴繪製在同一個圖上(即相同的座標軸),而不是像上述答案那樣的不同圖形對象。

基本上使用do.callvioplot應用於數據列表。最終,vioplot寫得不好(甚至不能設置標題,軸名稱等)。我通常更喜歡base R,但這種情況下ggplot2選項可能是最佳選擇。

x<-rnorm(1000) 
fac<-rep(c(1:10),each=100) 
listOfData<-tapply(x,fac,function(x){x},simplify=FALSE) 
names(listOfData)[[1]]<-"x" #because vioplot requires a 'x' argument 
do.call(vioplot,listOfData) 

resultingImage

相關問題