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

标签 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

你能帮我解决这个问题吗,因为我不知道如何使用位置参数来修复它

谢谢

最佳答案

我不确定您到底想做什么,但假设您希望条形图每个彩色部分中间的文本

  1. 位于酒吧中央;和
  2. 用于显示条形大小的有意义的数字

要解决第一个问题,您需要先按 type 对数据进行排序,然后再通过 cumsum 计算 pos 值。

要解决第二个问题,您应该显示标签美观的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()

关于r - ggplot 条形图中的中心文本层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646433/

相关文章:

r - 库 "TableOne"多重比较。逐行计算 p 值

仅当整个 df r 中不存在时才从变量中删除 0

自更新到 R 3.4.0 以来,C++11 的 Rcpp 插件不起作用

r - 使用 RSQLite 在 R 中加载 SQLite 表

r - ggplot2 分组条形图中变量的总和

python - Snakemake - 在调用外部脚本之前加载集群模块

r - R中的现金流量图?

r - ggplot2:在离散轴上显示每第 n 个值

将线旋转到真北或垂直

r - ggplot 颜色条标签日期格式