2016-10-16 5 views
1

整列されたヒストグラムを同じグラフのマトリックスの行からプロットする必要があります。次の例では、積み重なった5つのヒストグラムをプロットする必要があります。ただし、histコマンドは行列全体のヒストグラムをプロットします。私が使用できる回避策はありますか?同じグラフ内に整列されたヒストグラムをプロットする

ありがとうございます。

アルトゥーロ

x <- 10; y <- 10; g <- 5 
dat <- matrix(rnorm((x + y) * g), ncol = x + y) 
hist (dat) 
+0

このように、各行に個別の棒グラフを表示し、比較のために縦に積み重ねる必要がありますか? – shayaa

+0

それは正しいです。各行にはそれぞれ独自のy軸と共有x軸があります。 – Arturo

答えて

-1

これは私が同じチャートで2つのヒストグラムを生成するために使用していたアプローチです。

列xが連続変数であり、列yが係数変数であるデータセットを持つことを検討してください。各レベルは1つのヒストグラムを生成します。下記のggplot2を使用したコードの例をご覧ください:

OverlayedHist <- function(mData  , 
          featureVar , 
          grouper  , 
          mbinwidth , 
          mTitle  , 
          mxlab  , 
          mylab  , 
          mlegendTitle 
){ 

    # function name: OverlayedHist 
    #  purpose: To produce overlayed histograms against a grouping variable 
    #   Input: 
    #   mData: dataset object in data.table format 
    # featureVar: name of continuous variable to produce histogram 
    #  grouper: the grouping variable to produce the histogram 
    #  mbinwidth: binwidth (see ggplot2 parameters) 
    #  mTitle: Character to define title 
    #   mxlab: Character to define xlab name 
    #   mylab: Character to define ylab name 
    # mlegendTitle: Character to define legend title 

    library(data.table) 
    library(ggplot2) 
    library(plotly) 

    p <- ggplot(allDat, aes(eval(parse(text = featureVar)), fill = eval(parse(text = grouper)))) + 
    geom_histogram(alpha = 0.7, position = 'identity', binwidth = mbinwidth) + 
    scale_fill_manual(values=c("#377EB8","#E41A1C")) + 
    ggtitle(mTitle) + 
    xlab(mxlab) + ylab(mylab) + 
    guides(fill=guide_legend(title=mlegendTitle)) + theme(plot.title = element_text(size=10)) 


    return(ggplotly(p)) 

} 

私はこれが役立つことを願っています。

乾杯! K.

+0

あなたのメソッドをテストするためにいくつかの作業コードを投稿してください。 – Arturo

+0

上記のインタラクティブプロットのより詳細なバージョンをご覧になれます – mammask

1

ggplot簡単に(長い形式のデータが必要です)。

library(tidyr); library(dplyr); library(ggplot2) 

df <- dat %>% t() %>% as.data.frame() %>% gather(row) # chage data into a long format 
Breaks <- hist(dat, plot=F)$breaks      # get (or decide) breaks 

ggplot(df, aes(x = value, fill = row)) + geom_histogram(position = "stack", breaks = Breaks) 

enter image description here

[EDITED]
はこれが何をしたいですか?

## original 
ggplot(df, aes(x = value)) + 
    geom_histogram(breaks = Breaks) + 
    facet_wrap(~ row)   # make histogram par group. 

    ## modified 
ggplot(df, aes(x = value, fill = row)) +     # change fill colour 
    geom_histogram(breaks = Breaks) + 
    facet_wrap(~ row, ncol = 1) +       # bring graphs into line 
    # facet_wrap(~ row, ncol = 1, scales = "free_y") # if you don't want fixed scale 
    theme(strip.background = element_blank(), strip.text.x = element_blank()) # delete labels 

enter image description here

[EDITED2:base_plotアプローチ]
基本プロット時間を節約できます。

## example data 
x <- 1500; y <- 1500; g <- 30 
set.seed(1); dat <- matrix(rnorm((x + y) * g), ncol = x + y) 

## decide breaks 
Breaks <- seq(-4.5, 4.5, 0.5) 

## change par() to draw multiple graphs 
par.old <- par(mar = c(0.1, 4.0, 0.1, 0.5), oma = c(4, 0, 0, 0), mfrow = c(nrow(dat), 1)) 

for(i in 1:nrow(dat)) { 
    hist(dat[,i], breaks = Breaks, xaxt = "n", xlab = "", main = "") 
# grid(NULL) 
    } 
par(new=T) 
hist(dat[,nrow(dat)], breaks = Breaks, main = "", yaxt = "n", 
    xlab = "x", ylab = "", border = NA)   # add x-axis 

par(par.old) 
+0

ありがとうございます。私のプロットの各行を分けて、その寄与がすぐに表示されるようにする必要があります – Arturo

+0

ありがとうございます。これは私が望むものに近いです。今私はコードを理解する必要があります.. :-) – Arturo

+0

コメント欄もありがとうございます。あなたが気にしないなら、もう1つの質問があります。場合によっては、私が提示した例のように、5x20ではなく30x6000までの配列をプロットする必要があります。私はこの場合、手順に時間がかかることに気付いた。私は、色を取り除くことで物事をスピードアップさせるかもしれないと考えていました。これが実現可能だと思いますか? @Arturo; – Arturo

関連する問題