【问题标题】:Exact number of every value next to the bar [duplicate]条形旁边每个值的确切数字[重复]
【发布时间】:2017-08-26 14:03:17
【问题描述】:

拥有这样的数据集:

df <- structure(list(word = structure(c(1L, 12L, 23L, 34L, 43L, 44L, 
45L, 46L, 47L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 27L, 
28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 
42L), .Label = c("word1", "word10", "word11", "word12", "word13", 
"word14", "word15", "word16", "word17", "word18", "word19", "word2", 
"word20", "word21", "word22", "word23", "word24", "word25", "word26", 
"word27", "word28", "word29", "word3", "word30", "word31", "word32", 
"word33", "word34", "word35", "word36", "word37", "word38", "word39", 
"word4", "word40", "word41", "word42", "word43", "word44", "word45", 
"word46", "word47", "word5", "word6", "word7", "word8", "word9"
), class = "factor"), frq = c(1975L, 1665L, 1655L, 1469L, 1464L, 
1451L, 1353L, 1309L, 1590L, 1545L, 1557L, 1556L, 1130L, 1153L, 
1151L, 1150L, 1144L, 1141L, 1115L, 194L, 195L, 135L, 135L, 130L, 
163L, 167L, 164L, 159L, 153L, 145L, 143L, 133L, 133L, 153L, 153L, 
150L, 119L, 115L, 115L, 115L, 114L, 113L, 113L, 113L, 115L, 102L, 
101L)), .Names = c("word", "frq"), class = "data.frame", row.names = c(NA, 
-47L))

通过这个命令行,我生成了一个条形图

dat2 = transform(df,word = reorder(word,frq))
df2 <- head(dat2, 10)
p = ggplot(df2, aes(x = word, y = frq)) + geom_bar(stat = "identity", fill = "yellow")
p2 <- p +coord_flip()

如何在每根柱的末尾有 frq 的数量?

【问题讨论】:

  • ... + geom_text(aes(label = frq), vjust = 0)

标签: r


【解决方案1】:

我会使用annotate..

p2 + annotate(geom = "text",x = df2$word, y= df2$frq, label = df2$frq)

【讨论】:

  • 谢谢。这个作品!但只是一个问题,你知道我怎么能把数字弄对一点,因为数字的一半在酒吧里,第二个在外面?
  • 您可以通过调整 x 和 y 来轻松定位注释。例如,在这种情况下,一个简单的解决方法是为您的 y 添加一个小的固定值(例如 80)。 p2 + annotate(geom = "text",x = df2$word, y = 80 + df2$frq, label = df2$frq)
猜你喜欢
  • 2013-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 2022-01-06
  • 1970-01-01
相关资源
最近更新 更多