Binwidth for a Histogram in R

Binwidth for a Histogram in R

This is my program so far. I need to ask the user to input their own bindwidth next and i can't figure out how to do it.

file.name <- readline('Please enter name and file location: ')
has.header <- readline('Does your data file contain a header (Y/N)? ')

if (has.header == 'Y')
{
  file.df <- read.table( file = file.name, sep=',', header = TRUE)
}else
{
  file.df <- read.table ( file = file.name, sep=',', header = FALSE)
}
cat('\n')
print(file.df)
cat('\n')

#Histogram 
print(head(file.df))
column <- as.numeric(readline('What column do you want to graph (enter           number)? '))
print(hist(file.df[ ,column]))

#New bin width
binsize <- readline("Enter a new bin width for your graph: ")
1

1 Answer

binsize <- as.integer(readline("Enter a new bin width for your graph: "))
xt <- readline("Enter x axis title: ")
yt <- readline("Enter y axis title: ")

binwidth <- as.integer(nrow(file.df)/binsize))

hist(file.df[, column], breaks = binwidth, xlab = xt, ylab = yt)
6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.