0

I am using the following ggplot construct:

factorName="adj_book_equity";

p <-ggplot(data,aes(x=data[,factorName])) + 
    geom_histogram(aes(y=..count..,fill=..count..),bins=NBins,col="red") + 
    stat_bin(bins=NBins,geom="text",aes(label=..count..),vjust=-1)+    
    scale_x_continuous(breaks = pretty(data[,factorName], n = NBins),
    labels = comma,na.value=NA_real_)+   
    scale_y_continuous(labels = comma,breaks=pretty_breaks(n=5))+   
    scale_fill_gradient("Count", low = "green", high = "red",labels = comma)+ 
    labs(title=paste("Histogram-",factorName,sep=""),x=factorName)

Histogram from code:Note the x-axis labels

enter image description here

The main issue is that I would like the boundaries of the histogram bars to be aligned with the x-axis tick marks (major axis) or have them displayed so that i can interpret the histogram easily ie. obtaining the count below a certain value vs the count between 2 values on the x axis as seen from the graph. the display values of 0, 1000, ....on axis do not appear to be related to the count displayed on the bar labels. Your help is greatly appreciated.

I can't provide the data being discussed. But if needed, I can use an alterate dataset to help understand the question.

Thanks

Edit:

NBins=15;
factorName="mpg"; 
data=mtcars;



 p <-ggplot(data,aes(x=data[,factorName])) + 
    geom_histogram(aes(y=..count..,fill=..count..),bins=NBins,col="red") + 
    stat_bin(bins=NBins,geom="text",aes(label=..count..),vjust=-1)+    
    scale_x_continuous(breaks = pretty(data[,factorName], n = NBins),
    labels = comma,na.value=NA_real_)+   
    scale_y_continuous(labels = comma,breaks=pretty_breaks(n=5))+   
    scale_fill_gradient("Count", low = "green", high = "red",labels = comma)+ 
    labs(title=paste("Histogram-",factorName,sep=""),x=factorName)

MTCarss Based Histogram

The revised histogram shows what I would like to look at. what are the x axis labels at the boundary of each bar (the ? sign )? I need some information on what are the boundaries of each column bar in the histogram so that I know that xyz count of data is less than this boundary... Or better if i can align the column bars and their boundaries with the x-axis tick marks and labels .....

user41409
  • 1
  • 1
  • 1
    I am not quite sure that I understand your question. If you could supply an exemplary dataset and perhaps an illustration of what you're trying to accomplish --- that would be great. – Peter H. Dec 15 '17 at 17:30
  • Have you looked at using `binwidth` and `center` or `boundary` instead of `bins`? That would give you control over where the bars are placed, which could then allow you to possible set the breaks at the start of each bar. – aosmith Dec 15 '17 at 20:43
  • @aosmith: Could you please give me an example of using center or boundary ? – user41409 Dec 16 '17 at 00:46
  • Just look at the duplicate. – Uwe Dec 16 '17 at 01:31

0 Answers0