将直方图上的原始数字按比例覆盖

时间:2019-05-30 09:36:47

标签: r ggplot2

我有一个具有以下属性的数据集:

  • 多个地点
  • 开放不同时间段
  • 在不同的时间出现不同的照顾者

这里有一些示例代码:

dt <- tibble(
  "location" = c("A", "B", "C", "D", "A", "B", "C", "A", "B", "C", "A", "B", "A", "A", "A"),
  "months.since.start" = c(0,0,0,0,1,1,1,2,2,2,3,3,4,5,6),
  "carer" = c("HCA", "nurse", "nurse", "dr", "HCA", "dr", "nurse", "HCA", "dr", "nurse", "dr", "HCA", "dr", "nurse", "HCA")
)

理想情况下,我想做的是两件事:

  1. 将直方图与计数(下面的图A)以线形叠加在 直方图的顶部及其比例(下面的图B)
  2. 将数字与活动位置(图A中的geom_text)的比例重叠(在图B中)

我所指的情节: enter image description here

我知道它们在this post中解决的不同轴上起作用,但是我不需要显示图A的轴,它仅用于显示活动位置随时间的变化。

我怀疑可以通过将总数除以4(即活动位置的总数)来完成第一部分,然后将直方图放在比例计数之上,但是我不确定该怎么做。 / p>

1 个答案:

答案 0 :(得分:0)

我已经设法回答了自己的问题-我认为我会提出而不是删除它,因为在其他任何地方都找不到(尽管我很欣赏这是相当简单的代码)。

这是情节:

enter image description here

这是上面剧情的代码:

dt %>%
  ggplot(., size = 2, aes(months.since.start)) +
  geom_histogram(binwidth = 1,                  # original chart with proportions
                 position = "fill", 
                 aes(fill = carer)) + 
  geom_histogram(binwidth = 1,                  # the barchart with the total count
                 color = 'grey', 
                 alpha = 0,                     # transparent boxes
                 aes(y=..count../4)) +          # divided by the total number of locations (4 in this case), so that it becomes a fraction of 1 and therefore will fit within the y-axis
  geom_text(stat = 'count', 
            aes(label=..count..), 
            position=position_fill(vjust=0.05), #the text, adjusted using position_fill, so that the position is fixed
            color = 'white') + 
  theme_minimal()