0

I searched for this but I only came with this similar question but labelling the count values in stacked histogram. What I want to do is that label just one price value on top of each histogram bar.

using the similar histogram-ggplot-show-count-label-for-each-bin-for-each-category

  ggplot(aes(x = price ), data = diamonds) + 
  geom_histogram(aes(fill = cut ), binwidth=1500, colour="grey20", lwd=0.2) +
  stat_bin(binwidth=1500, geom="text", colour="white", size=3.5,
           aes(label=..count.., group=cut, y=0.8*(..count..))) +
  scale_x_continuous(breaks=seq(0,max(diamonds$price), 1500))

enter image description here

When I change label=..count.. to ..price.. I get

Error in FUN(X[[i]], ...) : object 'price' not found

How I can we put price value on top of each histogram?

thanks in advance!

Alexander
  • 4,527
  • 5
  • 51
  • 98
  • [This](https://stackoverflow.com/questions/10327267/annotation-above-bars) thread seems to have your answer. Re: your error, `..count..` is a specific built-in `stat` variable that calls something similar to a `sum` – Punintended Mar 12 '18 at 17:51
  • How will you decide which singular price value to show for the bin? The bins contain a range of prices. – MrFlick Mar 12 '18 at 18:06
  • @MrFlick That's a good point. Something like in the middle value is fine! – Alexander Mar 12 '18 at 18:08

1 Answers1

0

This is kind of cobbled together. I don't know how to redefine something like the ..count.. special variable for a function like mean or median. Instead, I calculated the mean of price within each bin and then annotate-d. I chose to not put the mean price on top of the bars, since that would suggest incorrectly to the reader that these were counts, but they're not.

library(plyr)
mean.cut <- ddply(diamonds, .(cut(price,seq(0,max(diamonds$price), 1500))), 
                              summarize, v=mean(price))

ggplot(diamonds) +geom_histogram(aes(x=price,fill = cut ), binwidth=1500, 
                                 colour="grey20", lwd=0.2) +  
  scale_x_continuous(breaks=seq(0,max(diamonds$price), 1500))+
  annotate("text", x=seq(0,max(diamonds$price), 1500),  y=mean.cut$v,
                  label=round(mean.cut$v,0))

enter image description here

I played around with formatting of hte price value and liked this code for labels of the various options I tried:

label=sprintf(" $%-5s", round(mean.cut$v,-1) )
IRTFM
  • 258,963
  • 21
  • 364
  • 487