2017-09-24 52 views
0

特徵頻率的劇情我具有類似於下面的矩陣的數據幀:R:每

r= 50 
c = 10 
testdata <- matrix(rbinom(r*c,1,0.5),r,c) 
examplev <- rep(c(290,320,390,460,520,580,710,780,800,100001),5) 
testdata <- cbind(testdata,examplev) 

說每個二進制列代表一個特徵和在數據的類別的最後一列。我想創建一個圖表,顯示y軸上的特徵1到10以及x軸上的類別1到10,顯示包含特徵y的我的數據記錄中有多少是類x的成員。任何想法如何編寫R中的代碼?

enter image description here

+1

你們是不是要每個數據的曲線圖?你可以找小提琴劇情嗎? http://ggplot2.tidyverse.org/reference/geom_violin.html – lebelinoz

+0

你的評論也有幫助。我從未聽過小提琴劇情。謝謝! – Diana01

回答

1

不是100%你問什麼,因爲我用條形圖來代替密度圖,但是這是接近你想要什麼。

TABLE = sapply(as.data.frame(testdata[,1:10]), 
    function(x) table(x, testdata[,11])[2,]) 

## You will need to stretch the graphics window horizontally 
par(mfrow=c(1, 10)) 
for(i in 1:10) { 
    barplot(TABLE[i,], horiz=TRUE, xlim=c(0,max(TABLE)), 
     las=1, xlab=rownames(TABLE)[i]) 

}

Boxplots