The number of histogram columns of the base R function hist
is given by the argument breaks
but it is necessary to be careful, since with the question data the two instructions below give the same number of columns.
hist(histo$MCP)
hist(histo$MCP, breaks = 5)
As you can see, there are only 4 columns, both with and without breaks = 5
. This happens because according to the documentation of the function if breaks
is a number, that value is indicative only the number of histogram columns. From help("hist")
, original.
breaks one of:
a vector Giving the breakpoints between Histogram Cells,
a Function to Compute the vector of breakpoints,
a single number Giving the number of Cells for the Histogram,
a Character string naming an Algorithm to Compute the number of Cells (see ːDetails'),
a Function to Compute the number of Cells.
In the last three cases the number is a suggestion only; as the
breakpoints will be set to
Pretty
values, the number is Limited to 1e6 (with a Warning if it was
Larger). If breaks is a Function, the x vector is supplied to it as
the only argument (and the number of breaks is only Limited by the
amount of available memory).
Google Translation edited by me.
one of:
a vector that provides the breaking points between histogram cells,
a function to calculate the break point vector,
a single number that provides the number of cells for the histogram,
a string that names an algorithm to compute the number of cells (see 'Details'),
a function to calculate the number of cells.
In the last three cases, the number is only a suggestion; as the points
break will be set to beautiful values
(Pretty),
the number is limited to 1e6 (with a warning if higher). If breaks is
a function, the vector x is given to it as the only argument (and the
number of breaks is limited only by the amount of memory
available).
The best way to force the function to have a number of columns chosen by the user is to calculate the vector breaks
previously.
num_breaks <- 5
brks <- seq(floor(min(histo$MCP)), ceiling(max(histo$MCP)), length.out = num_breaks + 1)
Note that the following are different and is the first that hist
uses unless brks
is explicitly given.
pretty(brks)
#[1] 0 10 20 30 40
brks
#[1] 0.0 7.6 15.2 22.8 30.4 38.0
Now the graph intended.
hist(histo$MCP, breaks = brks, xlab= "MCP área (ha)", ylab = "Frequência")
Data in format dput
histo <-
structure(list(id = c("MB02", "MB03", "MB04", "MB05", "MB06",
"MB07", "MB08", "MB09", "MB10", "MB11", "MB12", "MB13", "MB14",
"MB15", "MB16"), MCP = c(12.59, 0.001, 1.311, 0.692, 16.153,
6.861, 37.665, 5.684, 12.99, 16.912, 37.665, 5.839, 8.889, 9.33,
2.011)), class = "data.frame", row.names = c(NA, -15L))
Two notes: 1) in
read.csv
is not necessaryheader = TRUE
, that is already the value; 2) to havesep=";", dec=","
andheader = TRUE
automatically exists theread.csv2
.– Rui Barradas