ggplot条形图中的中心文本图层

时间:2017-01-14 03:13:31

标签: r ggplot2

我有一些带有一些文字的直方图,我试图将它作为相应类型的中心

df = read.table(text = "
id   year  type amount
                1  1991  HIIT     22
                2  1991 inter    144
                3  1991  VIIT     98
                4  1992  HIIT     20
                5  1992 inter    136
                6  1992  VIIT    108
                7  1993  HIIT     20
                8  1993 inter    120
                9  1993  VIIT    124
                10 1994  HIIT     26
                11 1994 inter    118
                12 1994  VIIT    120
                13 1995  HIIT     23
                14 1995 inter    101
                15 1995  VIIT    140
                16 1996  HIIT     27
                17 1996 inter    103
                18 1996  VIIT    162
                19 1997  HIIT     24
                20 1997 inter     96
                21 1997  VIIT    172
                22 1998  HIIT     24
                23 1998 inter     92
                24 1998  VIIT    177
                25 1999  HIIT     28
                26 1999 inter     45
                27 1999  VIIT    220
                28 2000  HIIT     26
                29 2000 inter     36
                30 2000  VIIT    231", header = TRUE, sep = "")
library(dplyr);
library(ggplot2);
library(scales);

df %>%
  mutate(type = factor(type, levels = c("inter",  "VIIT", "HIIT"))) %>%
  group_by(year) %>%
  mutate(ratio = amount/sum(amount),
         pos=cumsum(ratio)-ratio/2) %>%
  ggplot(aes(x=factor(year), y=ratio, fill=type)) +
  geom_bar(stat="identity") +
  geom_text(aes(y = pos, label = percent(pos)), size = 4) +
  scale_y_continuous(name="", labels = percent) +
  coord_flip()

我的情节如下: enter image description here

你能帮我解决这个问题,因为我不知道怎么用位置参数

修复它

由于

2 个答案:

答案 0 :(得分:7)

我不确定您正在尝试做什么,但假设您希望文本位于条形图的每个彩色部分的中间

  1. 在酒吧的中心;和
  2. 是一个显示条形大小的有意义的数字
  3. 要解决第一个问题,在通过type计算pos值之前,您需要按cumsum排序数据。

    要修复第二个问题,您应该向标签审核显示ratio,而不是pos,这不是一个有意义的数字,而不是放置标签的水平坐标。

    df %>%
      mutate(type = factor(type, levels = c("inter",  "VIIT", "HIIT"))) %>%
      group_by(year) %>%
      arrange(desc(type)) %>%
      mutate(ratio = amount / sum(amount),
             pos = cumsum(ratio) - ratio / 2) %>%
      ggplot(aes(x = factor(year), y = ratio, fill = type)) +
      geom_bar(stat = "identity") +
      geom_text(aes(y = pos, label = percent(ratio)), size = 4) +
      scale_y_continuous(name="", labels = percent) +
      coord_flip()
    

    enter image description here

    顺便说一句,这不是一个直方图,它是一个堆积的条形图。 histogram是完全不同的。

    编辑 - 替代,更简单的方法

    正如评论中指出的那样,最近添加到ggplot2 position_stack将为您计算位置,而不是在数据管道中创建pos变量。所以下面的代码可能是做整件事的一种更简洁的方式(给出相同的结果):

    df %>%
      group_by(year) %>%
      mutate(type = factor(type, levels = c("inter",  "VIIT", "HIIT"))) %>%
      mutate(ratio = amount / sum(amount)) %>%
      ggplot(aes(x = factor(year), y = ratio, fill = type)) +
      geom_bar(stat = "identity") +
      geom_text(aes(label = percent(ratio)), position = position_stack(vjust = 0.5), size = 4) +
      scale_y_continuous(name="", labels = percent) +
      coord_flip()
    

答案 1 :(得分:2)

尝试以下代码

library(ggplot2)
library(dplyr)
library(formattable)

df <- read.table(text = "
                 id year type  amount
                 1  1991 HIIT  22
                 2  1991 inter 144
                 3  1991 VIIT  98
                 4  1992 HIIT  20
                 5  1992 inter 136
                 6  1992 VIIT  108
                 7  1993 HIIT  20
                 8  1993 inter 120
                 9  1993 VIIT  124
                 10 1994 HIIT  26
                 11 1994 inter 118
                 12 1994 VIIT  120
                 13 1995 HIIT  23
                 14 1995 inter 101
                 15 1995 VIIT  140
                 16 1996 HIIT  27
                 17 1996 inter 103
                 18 1996 VIIT  162
                 19 1997 HIIT  24
                 20 1997 inter 96
                 21 1997 VIIT  172
                 22 1998 HIIT  24
                 23 1998 inter 92
                 24 1998 VIIT  177
                 25 1999 HIIT  28
                 26 1999 inter 45
                 27 1999 VIIT  220
                 28 2000 HIIT  26
                 29 2000 inter 36
                 30 2000 VIIT  231", header = TRUE, sep = "")


pd <- df %>%
  mutate(type = factor(type, levels = c("inter", "VIIT", "HIIT"))) %>%
  group_by(year) %>%
  arrange(year, desc(type)) %>% 
  mutate(ratio = amount/sum(amount),
         pos = cumsum(ratio) - ratio/2)

pd %>% 
  ggplot(aes(x = factor(year), y = ratio, fill = type)) +
  geom_bar(stat = "identity") +
  geom_text(aes(y = pos, label = percent(ratio)), size = 4) +
  scale_y_continuous(name = "", labels = percent) +
  coord_flip()