下面的代码运行良好,它正确地标记了条形图,但是,如果我尝试geom_text的直方图我失败,因为geom_text需要一个y分量和直方图的y分量是从不作为代码的一部分的频率然后如何带来直方图的标签?
效果很好
ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) +
geom_bar(stat="identity", position="identity") +
geom_text(aes(label=Anomaly10y,vjust=1.5))
Run Code Online (Sandbox Code Playgroud)
问题 - 下面的geom_text代码中没有Y组件(用?表示)
ggplot(csub,aes(x=Anomaly10y)) +
geom_histogram()
geom_text(aes(label=?,vjust=1.5))
Run Code Online (Sandbox Code Playgroud)
默认情况下,geom需要x和y组件,
当我没有y-component时我该怎么办,因为它是由函数自动生成的?
MrF*_*ick 36
geom_histogram()只是一个花哨的包装,stat_bin所以你可以用自己喜欢的条形和文字自己.这是一个例子
#sample data
set.seed(15)
csub<-data.frame(Anomaly10y = rpois(50,5))
Run Code Online (Sandbox Code Playgroud)
然后我们用它绘制它
ggplot(csub,aes(x=Anomaly10y)) +
stat_bin(binwidth=1) + ylim(c(0, 12)) +
stat_bin(binwidth=1, geom="text", aes(label=..count..), vjust=-1.5)
Run Code Online (Sandbox Code Playgroud)
要得到

解决方案是使其美观:
set.seed(15)
csub <- data.frame(Anomaly10y = rpois(50, 5))
Run Code Online (Sandbox Code Playgroud)
现在绘制它
csub %>%
ggplot(aes(Anomaly10y)) +
geom_histogram(binwidth=1) +
stat_bin(binwidth=1, geom='text', color='white', aes(label=..count..),
position=position_stack(vjust = 0.5))
Run Code Online (Sandbox Code Playgroud)
结果图将是
| 归档时间: |
|
| 查看次数: |
23128 次 |
| 最近记录: |