Kozolovska
Kozolovska

Reputation: 1119

ggplot2 contour binwidth/bins default

What is the default value for the parameter bins/binwidth of geom_contour?

I couldn't find it in the help of the function.

Thanks

Upvotes: 0

Views: 686

Answers (2)

camille
camille

Reputation: 16862

Take a look at the source code for stat_contour. The number of bins and their breaks are set in StatContour's compute_group function definition:

compute_group = function(data, scales, bins = NULL, binwidth = NULL,
                           breaks = NULL, complete = FALSE, na.rm = FALSE) {
    # If no parameters set, use pretty bins
    if (is.null(bins) && is.null(binwidth) && is.null(breaks)) {
      breaks <- pretty(range(data$z), 10)
    }
    # If provided, use bins to calculate binwidth
    if (!is.null(bins)) {
      binwidth <- diff(range(data$z)) / bins
    }
    # If necessary, compute breaks from binwidth
    if (is.null(breaks)) {
      breaks <- fullseq(range(data$z), binwidth)
    }

    contour_lines(data, breaks, complete = complete)
}

You can provide geom_contour/stat_contour with your own number of bins or a binwidth, or with a vector of breaks. If these are all null, breaks is set to approximately 10 pretty breaks covering the range of your z variable. Pretty break calculations take the number of breaks, n that you would like, but based on the actual data and how "pretty" (e.g. whole numbers, even numbers, multiples of 5, multiples of 10) your breaks are, you may end up with more or less than 10 breaks.

So, there is no static default binwidth or number of bins, because it depends on your data. The closest you could say is the default is roughly 10 bins.

Upvotes: 3

Vedha Viyash
Vedha Viyash

Reputation: 728

The default binwidth is 30 unless it is overwritten. You cannot find it because due to the lack of the usage, many functions don't support it anymore. geom_bar() used to support it before but now if you use it you'll get a warning telling to you use geom_histogram() instead. The default bins for histogram is also 30. Although, I'm not sure that you can use binwidth in geom_contour()

Upvotes: 0

Related Questions