2016-12-01 2 views
2

統計パッケージには、ベースパッケージで再現したいプロットがあります。Rベースを使った積み重ねられた棒グラフ:積み重なった各バーの中に値を追加する方法

プロットは次のようになります。

enter image description here

は、これまでのところ私はプロットを持っているが、私はバーの各部分に中央にラベルを追加するために問題を抱えています。

私のコードは次のようになります。自分の与えられた例えば

data <- sample(5, 10 , replace = TRUE) 

colors <- c('yellow','violet','green','pink','red') 

relative.frequencies <- as.matrix(prop.table(table(data))) 

bc <- barplot(relative.frequencies, horiz = TRUE, axes = FALSE, col = colors) 

答えて

2

、我々は(すべての読者は、この部分をスキップして次のにジャンプすることができます)を行うことができます。

set.seed(0) ## `set.seed` for reproducibility 
dat <- sample(5, 10 , replace = TRUE) 
colors <- c('yellow','violet','green','pink') 
h <- as.matrix(prop.table(table(dat))) 
## compute x-location of the centre of each bar 
H <- apply(h, 2L, cumsum) - h/2 
## add text to barplot 
bc <- barplot(h, horiz = TRUE, axes = FALSE, col = colors) 
text(H, bc, labels = paste0(100 * h, "%")) 

strip


すべての読者の場合

ここで、アイデアを消化するための包括的な例を作成します。

ステップ1:実験のためのパーセントのおもちゃの行列を生成

## a function to generate `n * p` matrix `h`, with `h > 0` and `colSums(h) = 1` 
sim <- function (n, p) { 
    set.seed(0) 
    ## a positive random matrix of 4 rows and 3 columns 
    h <- matrix(runif(n * p), nrow = n) 
    ## rescale columns of `h` so that `colSums(h)` is 1 
    h <- h/rep(colSums(h), each = n) 
    ## For neatness we round `h` up to 2 decimals 
    h <- round(h, 2L) 
    ## but then `colSums(h)` is not 1 again 
    ## no worry, we simply reset the last row: 
    h[n, ] <- 1 - colSums(h[-n, ]) 
    ## now return this good toy matrix 
    h 
    } 

h <- sim(4, 3) 
#  [,1] [,2] [,3] 
#[1,] 0.43 0.31 0.42 
#[2,] 0.13 0.07 0.40 
#[3,] 0.18 0.30 0.04 
#[4,] 0.26 0.32 0.14 

ステップ2:積み重ね棒グラフを理解し、各積み重ね棒グラフ

の「中間の高さ」を取得

積み上げ棒グラフの場合、棒の高さは、各列の累積合計がhです。

H <- apply(h, 2L, cumsum) 
#  [,1] [,2] [,3] 
#[1,] 0.43 0.31 0.42 
#[2,] 0.56 0.38 0.82 
#[3,] 0.74 0.68 0.86 
#[4,] 1.00 1.00 1.00 

現在、各積み重ね棒グラフのミッド/センターを取得するために戻ってh/2をシフト:

H <- H - h/2 
#  [,1] [,2] [,3] 
#[1,] 0.215 0.155 0.21 
#[2,] 0.495 0.345 0.62 
#[3,] 0.650 0.530 0.84 
#[4,] 0.870 0.840 0.93 

ステップ3:縦棒グラフの場合は塗りつぶされた数字

と棒グラフを生成します、上記のHは、各積み重ねバーの中心の座標をyとします。 x座標はbarplot(不可視)によって返されます。 textを使用しているとき、我々は複製xの要素nrow(H)回のそれぞれに必要なことを、注意してください:横棒グラフの場合

vertical barchart

x <- barplot(h, col = 1 + 1:nrow(h), yaxt = "n") 
text(rep(x, each = nrow(H)), H, labels = paste0(100 * h, "%")) 
Hは上記 xが中心の座標できます積み重ねられた各バーの y座標は barplot(不可視)によって返されます。 textを使用しているとき、我々は yさんの各要素に 複製nrow(H)回を必要とすることに、注意してください。

ここ
y <- barplot(h, col = 1 + 1:nrow(h), xaxt = "n", horiz = TRUE) 
text(H, rep(y, each = nrow(H)), labels = paste0(100 * h, "%")) 

Horizontal bar-chart

+0

すごいですよ。いい答えだ! – useR

2

mapplyを使用して別のソリューションです:

invisible(mapply(function(k, l) text(x = (k - l/2), y = bc, 
      labels = paste0(l*100, "%"), cex = 1.5), 
      cumsum(relative.frequencies), relative.frequencies)) 

mapplyされますsapplyの多変量バージョンです。この場合、2つの入力、すなわちcumsum(relative.frequencies)relative.frequenciesが入力され、これらの2つのベクトルを使用してtext()関数が適用されます。 x =は、それぞれの累積合計からそれぞれの半分を引いたものに対応するrelative.frequenciesのラベルの座標です。 relative.frequenciesがプロットされるラベルとして再び使用されます。

invisible()機能は、コンソールへの出力の印刷を抑制します。

enter image description here

関連する問題