Lin*_* Ma 2 r histogram ggplot2
想知道是否geom_text适用hist?尝试了以下代码,似乎没有效果。在为每个直方图桶绘制每个条时,我只想显示标签(属于特定直方图桶的元素数量)。任何解决方案表示赞赏。谢谢。
p <- hist(df$foo,
main="title",xlab="foo")
p + geom_text()
Run Code Online (Sandbox Code Playgroud)
编辑 1,试过geom_bar,这是我的代码,它似乎工作不正常,因为我希望每个条上都标有一个数字。在图中,它只显示 2.5、5、7.5 和 10,我希望为每个条显示 1、2、3、...、9、10。
g <- ggplot(df, aes(df$foo))
g + geom_bar()
Run Code Online (Sandbox Code Playgroud)
问候, 林
由于没有人回答这个问题,我会试一试:
首先,一些提示:
现在来实际回答你的问题:
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)
Run Code Online (Sandbox Code Playgroud)
关于您的问题,..count..请参阅:ggplot 中的特殊变量 (..count.., ..density.., etc.)。
| 归档时间: |
|
| 查看次数: |
2850 次 |
| 最近记录: |