geom_text works for histogram in R?

混江龙づ霸主 提交于 2019-12-06 16:42:11
Mike H.

Since no one has answered this I'll give it a try:

First, a few tips:

  • posting your data will make it more likely for people to answer (so we don't have to replicate it)
  • Doing some research on your own goes a long way. For example, you mention that you want a label for each bar - fine there are several ways to do it. A quick google search digs up this: Customize axis labels

Now to actually answer your question:

set.seed(1)
#Make sample data since none is provided
df <- data.frame(foo=sample(1:10,200,replace=T))

#This is what you want - use as.factor(foo) - this gives you the breaks at every bar.
g <- ggplot(df, aes(as.factor(foo)))

#Actually making the barplot and adding labels to it
g + geom_bar() +stat_count(aes(y=..count..,label=..count..),geom="text",vjust=-1)

To your question about ..count.. see: Special variables in ggplot (..count.., ..density.., etc.).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!