在R ggplot中的堆叠条形图顶部添加总计数

D普拉特

除了在我拥有的不同类别中的计数数量之外,我还要在堆积的条形图上添加一个总数。

这是我的情节:

在此处输入图片说明

因此,在10(首先在x轴上),我将有一个5,然后又是5,依此类推。

我发现了很多有关此的帖子,但是没有一个帖子允许我解决我的问题。我最接近的可能是这个:如何将带有观察计数的标签添加到stat_summary ggplot?但问题是我需要将字符串作为“因素”

这是上面图的代码:

ggplot(my_df, aes(x=percentage_int, fill = score)) + 
  geom_bar(aes(y = (..count..))) +
  geom_text(stat='count', aes(label=..count..),position = position_stack(vjust = 0.5))

这是我通过改写上面的文章尝试的代码:

label_df = my_df %>% group_by(percentage_int) %>% summarise(n=n())
    
ggplot(my_df, aes(x=percentage_int, fill = score)) + 
          geom_bar(aes(y = (..count..))) +
          geom_text(stat='count', aes(label=..count..),position = position_stack(vjust = 0.5)) +
          geom_text(data=label_df,aes(fill = score, x = percentage_int, label=n))

我不太确定要了解为什么它不起作用...

看来我无法避免为此创建额外的df ...

使用stat_summary的解决方案也很好,因为我也尝试过。谢谢 !

这是我的数据测试:

structure(list(percentage_int = structure(c(13L, 17L, 10L, 9L, 
14L, 8L, 19L, 11L, 18L, 12L, 6L, 15L, 4L, 16L, 5L, 2L, 20L, 3L, 
7L, 13L, 17L, 18L, 12L, 4L, 11L, 3L, 14L, 2L, 19L, 15L, 7L, 16L, 
6L, 8L, 5L, 20L, 10L, 9L, 19L, 8L, 9L, 11L, 12L, 20L, 13L, 14L, 
10L, 18L, 15L, 16L, 3L, 5L, 17L, 4L, 2L, 7L, 6L, 17L, 5L, 19L, 
7L, 18L, 9L, 20L, 14L, 16L, 11L, 8L, 3L, 13L, 10L, 6L, 4L, 15L, 
12L, 2L, 16L, 18L, 19L, 14L, 13L, 20L, 7L, 17L, 15L, 2L, 9L, 
5L, 3L, 4L, 12L, 10L, 6L, 11L, 8L, 6L, 19L, 13L, 5L, 12L), .Label = c("0", 
"10", "15", "20", "25", "30", "35", "40", "45", "5", "50", "55", 
"60", "65", "70", "75", "80", "85", "90", "95"), class = "factor"), 
    score = c("Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "0.8 > R score >= 0.7", 
    "Rscore = 0.96", "0.8 > R score >= 0.7", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "0.8 > R score >= 0.7", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "0.8 > R score >= 0.7", "Rscore = 0.96", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "0.5 > R score >= 0.2", "Rscore = 0.96", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "0.8 > R score >= 0.7", "Rscore = 0.96", "Rscore = 0.96", 
    "0.8 > R score >= 0.7", "Rscore = 0.96", "0.8 > R score >= 0.7", 
    "0.8 > R score >= 0.7", "0.8 > R score >= 0.7", NA, "0.8 > R score >= 0.7", 
    "0.8 > R score >= 0.7", "R score < 0.2", "0.8 > R score >= 0.7", 
    "Rscore = 0.96", "Rscore = 0.96", "0.8 > R score >= 0.7", 
    "0.8 > R score >= 0.7", "R score < 0.2", "Rscore = 0.96", 
    "0.8 > R score >= 0.7", "0.8 > R score >= 0.7", "0.8 > R score >= 0.7", 
    "0.8 > R score >= 0.7", "Rscore = 0.96", "0.8 > R score >= 0.7", 
    "Rscore = 0.96", "0.8 > R score >= 0.7", "Rscore = 0.96", 
    NA, "Rscore = 0.96", "0.8 > R score >= 0.7", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "0.8 > R score >= 0.7", 
    "Rscore = 0.96", "Rscore = 0.96", "0.8 > R score >= 0.7", 
    "0.8 > R score >= 0.7", "0.8 > R score >= 0.7", "Rscore = 0.96", 
    "Rscore = 0.96", NA, "Rscore = 0.96", "0.8 > R score >= 0.7", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", "Rscore = 0.96", 
    "0.5 > R score >= 0.2", "Rscore = 0.96", "Rscore = 0.96", 
    "0.9 > R score >= 0.8")), row.names = c("1410", "1411", "1412", 
"1413", "1414", "1415", "1416", "1417", "1418", "1419", "1420", 
"1421", "1422", "1423", "1424", "1425", "1426", "1427", "1428", 
"1448", "1449", "1450", "1451", "1452", "1453", "1454", "1455", 
"1456", "1457", "1458", "1459", "1460", "1461", "1462", "1463", 
"1464", "1465", "1466", "1619", "1620", "1621", "1622", "1623", 
"1624", "1625", "1626", "1627", "1628", "1629", "1630", "1631", 
"1632", "1633", "1634", "1635", "1636", "1637", "1771", "1772", 
"1773", "1774", "1775", "1776", "1777", "1778", "1779", "1780", 
"1781", "1782", "1783", "1784", "1785", "1786", "1787", "1788", 
"1789", "1828", "1829", "1830", "1831", "1832", "1833", "1834", 
"1835", "1836", "1837", "1838", "1839", "1840", "1841", "1842", 
"1843", "1844", "1845", "1846", "1885", "1886", "1887", "1888", 
"1889"), class = "data.frame")

尝试这个:

#Code
label_df = my_df %>% group_by(percentage_int) %>% summarise(n=n())
#Create some vars
label_df$score<-NA
#Plot
ggplot(my_df, aes(x=percentage_int, fill = score)) + 
  geom_bar(aes(y = (..count..))) +
  geom_text(stat='count', aes(label=..count..),position = position_stack(vjust = 0.5)) +
  geom_text(data=label_df,aes(fill = score, x = percentage_int, y=n, label=n),
            position = position_dodge(0.9),vjust=-0.5)

在此处输入图片说明

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

向ggplot2 R中的堆叠条形图添加误差线-已解决

来自分类Dev

R中的堆叠的条形图

来自分类Dev

向R中的ggplot2中的堆叠条形图添加水平线,并在图例中显示

来自分类Dev

为什么在r ggplot2中添加构面时,堆叠条形图会发生变化

来自分类Dev

R studio-R中的堆叠条形图

来自分类Dev

R ggplot中的多元条形图

来自分类Dev

将geom_line添加到R中的堆叠条形图

来自分类Dev

R中的堆叠条形图:对数据重新排序

来自分类Dev

R条件着色中的堆叠条形图

来自分类Dev

ggplot2中条形图的计数和百分比如何?[R

来自分类Dev

带组的R堆叠条形图

来自分类Dev

如何在R中使用ggplot / geom_bar在条形图顶部的数据集中添加自定义标签?

来自分类Dev

用 R ggplot2 中的百分比从头开始分类变量的堆叠条形图

来自分类Dev

R中按组的计数数据的条形图

来自分类Dev

从 R 中的 2 个计数变量创建条形图

来自分类Dev

在 R 中的分组条形图 ggplot 中更改颜色

来自分类Dev

如何在R代码中向条形图添加SE条形图?

来自分类Dev

R中的条形图

来自分类Dev

R中ggplot条形图的x轴限制

来自分类Dev

ggplot2中的单个条形图,R

来自分类Dev

条形图(ggplot,R)中的美学错误

来自分类Dev

R中ggplot条形图的x轴限制

来自分类Dev

r中带有ggplot的相对条形图

来自分类Dev

带有ggplot2的R中的条形图

来自分类Dev

R ggplot中并排的堆积条形图

来自分类Dev

如何在R中的同一图中显示并排条形图以及堆叠条形图?

来自分类Dev

r ggplot2堆叠的条形图未绘制所有层

来自分类Dev

R ggplot2堆叠条形图通过列的值归一化

来自分类Dev

R ggplot2堆叠条形图通过列的值归一化

Related 相关文章

热门标签

归档