百分比条形图底部一行中的观察数字(n)

时间:2018-03-23 07:35:56

标签: r ggplot2

我知道有很多类似的问题,但我无法找到问题的答案。每组和每天的观察数应写在百分比条形图的底部(!),如本例所示。

enter image description here

我不知道如何做到这一点。

我的数据框:

  day cohort  n  mean    se  
1   1 CLOSED 70 47.66 26.63
2   2 CLOSED 70 60.19 23.72
3   3 CLOSED 67 65.68 23.16
4   4 CLOSED 60 67.96 21.38
5   1   OPEN 66 31.45 31.59
6   2   OPEN 66 39.83 25.76
7   3   OPEN 61 38.53 24.06
8   3   OPEN 52 45.23 25.00
....

我的代码:

p2 <- ggplot(data = df2, aes(x = day, y = mean, fill = cohort))

p2 + geom_bar(stat = "identity",
              color = "black",
              position = position_dodge(0.9),
              size = 1) +
     geom_text(aes(label = n),
               color = "black",
               size = 3.5,
               position = position_dodge(width=0.9),
               vjust = 1.5) +
     labs(x = "Day",
          y = "Time in target range (%)")

1 个答案:

答案 0 :(得分:0)

您可以修复y value

geom_text

这是一个例子。

ggplot(mtcars[1:5,],aes(rownames(mtcars[1:5,]),qsec,label=qsec))+
    geom_bar(stat="identity",fill="white",colour="black")+ 
    geom_text(aes(label=qsec,y=1))

enter image description here