ggplot条形图中的中心文本层 [英] Center text layer in ggplot barchart

查看:47
本文介绍了ggplot条形图中的中心文本层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些文本的直方图,我正在尝试将其居中为相应的类型

I have a histogram with some text and I m trying to center it for the corresponding type

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

我的情节看起来像:

您能帮我解决此问题,因为我不知道如何使用位置参数对其进行修复

Can you help me to solve this problem because I have no idea how to fix it with the position parameter

谢谢

推荐答案

我不确定您要尝试执行的操作,但是假设您希望条形图每个彩色部分中间的文本

I'm not sure exactly what you're trying to do, but assume that you want the text in the middle of each colored segment of the bar to

  1. 在酒吧的中央;和
  2. 使用有意义的数字显示栏的大小

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

To fix the first issue, you need to have the data sorted by type before you calculate the pos value via cumsum.

要解决第二个问题,您应该向标签美学显示 ratio ,而不是 pos ,该数字除了作为放置坐标的水平坐标外,没有其他意义.标签.

To fix the second, you should be showing ratio to the label aesthetic, not pos, which is not a meaningful number other than being the horizontal coordinate to place the label.

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

顺便说一句,这不是直方图,而是堆积的填充条形图.直方图完全不同.

By the way, this isn't a histogram, it's a stacked filled bar chart. A histogram is something quite different.

正如评论中指出的那样,ggplot2的最新版本 position_stack 将为您计算位置,而不是在数据管道中创建 pos 变量.因此,下面的代码也许是整件事的更整洁的方式(给出了相同的结果):

As pointed out in the comments, the fairly recent addition to ggplot2, position_stack, will calculate the position for you rather than creating pos variable in your data pipeline. So the code below is perhaps a neater way of doing the whole thing (gives identical result):

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

这篇关于ggplot条形图中的中心文本层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆