Problem with ranges when generating a histogram

1

I would like to create a frequency histogram, defining the ranges of each bar in the histogram. I thought that by defining the number of bars, their ranges would be defined but I have not managed to obtain a desired histogram.

the code that I am trying to use is the following:

 qplot(b2$residual2,
+       geom="histogram",
+       binwidth = 20,  
+       xlab = "Gruop of Residual",  
+       ylab = "Number of ocurrencies",  
+       fill=I("blue"), 
+       col=I("black"), 
+       alpha=I(.2),
+       xlim=c(-300,300))

Could you suggest me how to do it? when trying to do it with ggplot2, it generates an error:

ggplot(b2, aes(residual2))+ + geom_histogram(bin=30) Warning: Ignoring unknown parameters: bin stat_bin() using bins = 30. Pick better value with binwidth
    
asked by daniel kater 08.01.2018 в 15:54
source

1 answer

0

Let's see, the error (which is actually a warning)

  

Warning: Ignoring unknown parameters: bin stat_bin () using bins = 30.   Pick better value with binwidth

is basically because geom_histogram() does not have a parameter bin but is called bins , if you execute ?geom_histogram() you will see the help

bins     Number of bins. Overridden by binwidth. Defaults to 30

You're not misguided to graph a histogram. You can use

# Genero un data.frame de ejemplo
set set.seed(300)
b2 <- as.data.frame(cbind(residual2=sample(1:30, 1000,replace = TRUE)))

ggplot(data=b2, aes(b2$residual2)) + 
    geom_histogram(bins=30)

With what you would get something like this:

Or

qplot(b2$residual2,
      geom="histogram",
      binwidth = 0.5,  
      xlab = "Gruop of Residual",  
      ylab = "Number of ocurrencies",  
      fill=I("blue"), 
      col=I("black"), 
      alpha=I(.2),
      xlim=c(1,30))

Where should you:

  • Modify binwidth = 20 , which is the width of each bar not the amount for something like that binwidth = 0.5 a smaller width to be able to see all
  • And xlim=c(-300,300) by xlim=c(1,30) to resemble the previous example in which you set 30 bars, here what we are saying is to limit the bars to show the first 30, that will vary according to what you need

The result:

    
answered by 08.01.2018 в 20:57