Increase the number of columns in the histogram

Asked

Viewed 47 times

4

Hello, I’m having trouble increasing the number of columns in a histogram. I tried to change the number of Bins from 10 to 5 but it didn’t work.

histograma: 

id  MCP  
#MB02   12,59
#MB03   0,001
#MB04   1,311
#MB05   0,692
#MB06   16,153
#MB07   6,861
#MB08   37,665
#MB09   5,684
#MB10   12,99
#MB11   16,912
#MB12   37,665
#MB13   5,839
#MB14   8,889
#MB15   9,33
#MB16   2,011

histo<-read.csv("histograma.csv", header = T, sep=";", dec=",")
hist(histo$MCP, xlab= "MCP área (ha)", ylab = "Frequência", bin=5)
  • Two notes: 1) in read.csv is not necessary header = TRUE, that is already the value; 2) to have sep=";", dec="," and header = TRUE automatically exists the read.csv2.

2 answers

5

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)

inserir a descrição da imagem aqui

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")

inserir a descrição da imagem aqui

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))

1

Utilize breaks:

hist(histo$MCP, xlab= "MCP área (ha)", ylab = "Frequência", breaks = 5)

I believe Bins is used in ggplot.

  • I get it, not much has changed, really. In this case I would need to change the structure of the X-axis so as to stay 0-5, 5-10, 10-15 etc.?

  • In this case if you put the breaks = 6 it will go from 5 to 5, only on the x axis it will show from 10 to 10. The bars will go from 0 - 5, 5 - 10, 10 - 15.... etc...

  • It worked, thank you very much!

  • For nothing! Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.