8

w次のようなデータフレームがあります。

>head(w,3)
         V1        V2         V3        V4 V5        V6         V7        V8        V9       V10 V11        V12        V13        V14
1 0.2446884 0.3173719 0.74258410 0.0000000  0 0.0000000 0.01962759 0.0000000 0.0000000 0.5995647   0 0.30201691 0.03109935 0.16897571
2 0.0000000 0.0000000 0.08592243 0.2254971  0 0.7381867 0.11936323 0.2076167 0.0000000 1.0587742   0 0.50226734 0.51295661 0.01298853
3 8.4293893 4.9985040 2.22526463 0.0000000  0 3.6600283 0.00000000 0.0000000 0.2573714 0.8069288   0 0.05074886 0.00000000 0.59403855
         V15       V16      V17       V18      V19       V20       V21      V22         V23        V24       V25       V26       V27
1 0.00000000 0.0000000 0.000000 0.1250837 0.000000 0.5468143 0.3503245 0.000000 0.183144204 0.23026538 6.9868429 1.5774150 0.0000000
2 0.01732732 0.8064441 0.000000 0.0000000 0.000000 0.0000000 0.0000000 0.000000 0.015123385 0.07580794 0.6160713 0.7452335 0.0740328
3 2.66846151 0.0000000 1.453987 0.0000000 1.875298 0.0000000 0.0000000 0.893363 0.004249061 0.00000000 1.6185897 0.0000000 0.7792773
        V28 V29     V30       V31        V32        V33       V34       V35 V36        V37        V38       V39        V40    refseq
1 0.5543028   0 0.00000 0.0000000 0.08293075 0.18261450 0.3211127 0.2765295   0 0.04230929 0.05017316 0.3340662 0.00000000 NM_000014
2 0.0000000   0 0.00000 0.0000000 0.00000000 0.03531411 0.0000000 0.4143325   0 0.14894716 0.58056304 0.3310173 0.09162460 NM_000015
3 0.8047882   0 0.88308 0.7207709 0.01574767 0.00000000 0.0000000 0.1183736   0 0.00000000 0.00000000 1.3529881 0.03720155 NM_000016

dim(w)
[1] 37126    41

各列 (最後の列を除く) の密度曲線を 1 ページにプロットしようとしました。ggplot2 がこれを行うことができるようです。

この投稿に従ってこれを試しました:

ggplot(data=w[,-41], aes_string(x=colnames)) + geom_density()

しかし、次のように文句を言ってもうまくいきません。

Error in as.character(x) : 
  cannot coerce type 'closure' to vector of type 'character'

そして、このデータフレームの形式をggplot2が受け入れる形式に変換する方法がわかりません。または、Rでこの仕事をする他の方法はありますか?

4

4 に答える 4

5

「reshape」パッケージの「melt」を使用します (基本の reshape 関数を使用することもできますが、呼び出しはより複雑になります)。

require (reshape)
require (ggplot2)
long = melt(w, id.vars= "refseq")

ggplot(long, aes (value)) +
    geom_density(color = variable)

# or maybe you wanted separate plots on the same page?

ggplot(long, aes (value)) +
    geom_density() +
    facet_wrap(~variable)

これを ggplot でプロットする方法は他にもたくさんあります 。例については、 http ://docs.ggplot2.org/0.9.3.1/geom_histogram.html を参照してください。

于 2013-06-04T04:06:23.290 に答える
2

plotこれは、関数と小さなループを使用したソリューションです

あなたのプロットを呼び出す

plot(density(df[,1]), type = "n")

次に、これを実行して行を追加します

n = dim(df)[2]-1
for(i in 1:n){
lines(density(c(df[,i])))
}
于 2015-07-31T09:40:35.967 に答える