Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically set histogram binwidth

Tags:

r

ggplot2

I am a total R noob learning ggplot. I don't understand why the first snippet works while the second doesn't. I wanted to find a good binwidth without guessing, so I tried an experiment that didn't work.

library(ggplot2)
attach(diamonds)
d <- diamonds
x <- ggplot(d, aes(x = price))
x <- x + geom_histogram(binwidth = 50)
x
# worked fine, but using the sequence and substituting i didn't
i <- seq(1, 101, by = 10)  #tried to avoid the first arg as zero, but didn't work
x <- ggplot(d, aes(x = price))
x <- x + geom_histogram(binwidth = i) 
x

the second throws an error

Error in seq.default(round_any(range[1], size, floor), round_any(range[2],  : 
  'from' must be of length 1
Error in exists(name, envir = env, mode = mode) : 
  argument "env" is missing, with no default

I don't understand what it wants. Thanks a lot

like image 658
drew Avatar asked May 16 '15 17:05

drew


People also ask

How do you determine bin size for a histogram?

Choose between 5 and 20 bins. The larger the data set, the more likely you'll want a large number of bins. For example, a set of 12 data pieces might warrant 5 bins but a set of 1000 numbers will probably be more useful with 20 bins. The exact number of bins is usually a judgment call.

How do I set up Binwidth?

To adjust the bin width, right click the horizontal axis on the histogram and then click Format Axis from the dropdown: What is this? In the window that appears to the right, we can see that Excel chose the bin width to be 29,000. We can change this to any number we'd like.


1 Answers

You may also want to consider the package manipulate if you are using RStudio:

install.packages("manipulate")
library(manipulate)
library(ggplot2)

df <- diamonds

manipulate(
  ggplot(df, aes(x = price)) +
    geom_histogram(binwidth = mybinwidth),
  mybinwidth = slider(10, 100, step = 10, initial = 20)
)

Picture of Manipulate

Aside: Please note that you do not need to attach(diamonds) if you are using ggplot2. Moreover, many people will argue against using attach altogether - and you may want to break the habit now. For example, the following works just fine:

ggplot(diamonds, aes(x = price)) + geom_histogram()
like image 77
JasonAizkalns Avatar answered Oct 15 '22 21:10

JasonAizkalns