在ggplot中的比例数据条形图中添加计数标签

亚历克斯

我有一个用ggplot制作的比例数据的堆积条形图。我想添加构成这些比例的计数。

我的数据的一个子集:

dput(sus_dev_data)
structure(list(individual_code = structure(c(9L, 10L, 7L, 6L, 
8L, 1L, 4L, 3L, 2L, 25L, 23L, 21L, 22L, 24L, 20L, 16L, 17L, 19L, 
29L, 30L, 26L, 28L, 15L, 13L, 14L, 12L, 11L), .Label = c("D5WMA-113", 
"D5WMA-133", "D5WMA-136", "D5WMA-137", "D5WMA-141", "D6UIC-109", 
"D6UIC-72", "D6UIC-87", "D6UIC-89", "D6UIC-97", "S6WMA-806", 
"S6WMA-810", "S6WMA-811", "S6WMA-815", "S6WMA-820", "S6WMA-859", 
"S6WMA-863", "S6WMA-866", "S6WMA-875", "S6WMA-876", "S7UIC-1202", 
"S7UIC-1215", "S7UIC-1250", "S7UIC-1252", "S7UIC-1253", "S7UIC-709", 
"S7UIC-724", "S7UIC-780", "S7UIC-827", "S7UIC-840"), class = "factor"), 
    population = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L), .Label = c("UIC", "WMA"), class = "factor"), 
    time_point = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L), .Label = c("3", "8", "12"), class = "factor"), 
    days_to_feeding = c(3L, 3L, 4L, 3L, 5L, 4L, 3L, 4L, 5L, 4L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 4L), chill_number = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2"), class = "factor"), 
    days_to_pupation = c(135L, 142L, 143L, 155L, 149L, 159L, 
    153L, 171L, 9L, 67L, 53L, 49L, 72L, 67L, 55L, 64L, 60L, 122L, 
    53L, 51L, 49L, 53L, 50L, 56L, 44L, 47L, 60L)), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 20L, 21L, 22L, 23L, 24L, 26L, 27L, 28L, 29L, 30L), class = "data.frame")

我当前的ggplot代码:

(prop_plot <- ggplot(sus_dev_data, aes(x = time_point, fill = chill_number)) +
  Alex_Theme +
  geom_bar(position = "fill", color = "black") +
  scale_fill_manual(values = c("grey90", "white")) +
  scale_x_discrete(labels=c("3" = "13", "8" = "18",
                             "12" = "22")) +
  labs(y = 'Proportion pupated after 79 days', x = 'Weeks post-hatch')
)

现在,我想添加有助于这些比例的计数。我尝试过的所有选项要么将值固定在图的顶部,要么出现错误。我一直在关注这个类似的线程(https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2),但是这些选项都不适合我。

罗曼

您可以在绘制之前尝试计算数据

library(tidyverse)
df %>% 
  group_by(time_point) %>% 
  count(chill_number) %>% 
  mutate(perc = n/sum(n)) %>% 
  ggplot(aes(time_point, perc, fill =chill_number)) + 
    geom_col(position = position_stack()) + 
    geom_text(aes(label = n), position = position_stack(vjust =0.5), size =12)

在此处输入图片说明

或尝试

ggplot(df, aes(x = time_point, y = (..count..)/sum(..count..), fill = chill_number)) + 
  geom_bar() + 
  stat_count(geom = "text", aes(label = ..count..), position = position_stack(vjust =0.5))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用ggplot在堆叠的条形图中添加计数标签

如何在水平条形图中的条形右边添加计数标签?

如何使用ggplot2在条形图中的条形上添加频率计数标签?

在条形图中添加比例

在 R 中的条形图中的条形顶部添加数据标签

ggplot2-如何将比例标签添加到堆叠的比例条形图?

对ggplot比例条形图中的标签文本使用绝对值

分组条形图中的ggplot标签条

ggplot条形图中的标签问题

ggplot2 - 向条形图添加比例和计数值

如何在R中使用ggplot2在条形图中的条形图中添加标签?

如何在R中的计数ggplot条形图中添加一个变量的百分比

将标签添加到ggplot堆叠条形图中的整个条形

在条形图中添加条形图 (ggplot2)

ggplot2中比例堆积条形图的绝对标签

如何在ggplot2中将字符对象添加到数字标签:在条形图中将%符号添加到百分比标签

在ggplot2的条形图中添加百分比标签

将计数标签添加到群集条形图ggplot2

在ggplot条形图中更改特定数据点的文本标签颜色

ggplot在方格条形图中的计数旁边的方括号中显示份额

在ggplot中,如何在条形图中仅在特定条上方打印计数?

为ggplot中的分类和数值数据制作堆叠比例条形图

d3 v4如何在条形图中添加数据标签

无法更改列宽或在特定日期的条形图中添加单独的数据标签

如何在dimple.js条形图中添加数据标签?

如何在matplotlib的条形图中添加多个数据标签

ggplot2 中多面填充条形图中的标签百分比

ggplot2条形图中的标签NA

在条形图中重复计数?