R 用比例覆盖直方图上的原始数字

R 用比例覆盖直方图上的原始数字,r,ggplot2,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"

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

  • 多个位置
  • 打开不同的时间长度
  • 在不同的时间点有不同的护理人员在场
下面是一些示例代码:

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

dt我已经设法回答了我自己的问题-我想我应该把它放上去,而不是删除它,因为我在其他任何地方都找不到它(尽管我知道这是相当简单的代码)

下面是情节:

这是上面图的代码:

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

我已经设法回答了我自己的问题——我想我应该把它放上去,而不是删除它,因为我在其他任何地方都找不到它(尽管我知道这是一个相当简单的代码)

下面是情节:

这是上面图的代码:

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