首页 文章

使用R base的堆积条形图:如何在每个堆叠条形图中添加值

提问于
浏览
3

我在统计书中找到了一个情节,我想用基础包重现 .

情节看起来像这样:

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 回答

  • 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))
    

    mapplysapply 的多变量版本 . 在这种情况下,它需要两个输入: cumsum(relative.frequencies)relative.frequencies 并使用这两个向量应用 text() 函数 . x = 是标签的坐标,它将每个累积总和减去每个相应的 relative.frequencies 的一半 . 然后将 relative.frequencies 再次用作要绘制的标签 .

    invisible() 功能禁止将输出打印到控制台 .

    enter image description here

  • 2

    对于您给出的示例,我们可以( all readers can skip this part and jump to the next ):

    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


    适合所有读者

    I will now construct a comprehensive example for you to digest the idea.

    Step 1: generate a toy matrix of percentage for experiment

    ## 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
    

    Step 2: understand a stacked bar-chart and get "mid-height" of each stacked bar

    对于堆积条形图,条形的高度是 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
    

    Step 3: producing a bar-chart with filled numbers

    对于垂直条形图,上面的 H 给出了每个堆叠条形中心的 y 坐标 . x 坐标由 barplot (无形)返回 . 请注意,使用 text 时,我们需要 x 的每个元素 nrow(H) 次:

    x <- barplot(h, col = 1 + 1:nrow(h), yaxt = "n")
    text(rep(x, each = nrow(H)), H, labels = paste0(100 * h, "%"))
    

    vertical barchart

    对于水平条形图,上面的 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

相关问题