Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different bin width in histogram with ggplot2, r

I have read in other questions with similar titles but have had no luck solving my problem. I have a dataframe with two columns: sales and size. sales is numeric, size is character with "Small (1-20)", "Medium (20-50)", "Large (50-100)" and "Extra Large (>200)". I would like to make a histogram where the bins widths are equal to the different sizes. with breaks= seq(.....) the interval is the same. Is there some way I can have different intervals in my bins using ggplot2?

I am sorry if this is a duplicate but I have really tried to find the answer in other questions and either this exact question haven't been posed or I am too stupid to understand the explanations.

mydf <- data.frame(
  Sales = c(301, 5, 4, 26, 19, 82, 111, 41, 29, 12),
  Size = c("Extra Large (>200)", "Small (1-20)", "Small (1-20)", "Medium (21-50)", 
      "Small (1-20)", "Large (51-200)", "Large (51-200)", 
      "Medium (21-50)", "Medium (21-50)", "Small (1-20)" ))

mydf %>% ggplot(aes(Sales))+geom_histogram(aes(y=..count..))

The histogram that is created need some band width adjustment and it's here I would like to use the "width" of the categories already described.

like image 496
Mactilda Avatar asked Sep 07 '25 14:09

Mactilda


1 Answers

geom_histogram() will allows you to specify the breakpoints for your histogram. For example

mydf %>% ggplot(aes(Sales)) + 
  geom_histogram(breaks=c(0,20,50,200, 500))

enter image description here

like image 100
MrFlick Avatar answered Sep 09 '25 05:09

MrFlick