Page 264 - Data Science class 11
P. 264

Histograms  plot  quantitative  data  with  ranges  of  the  data  grouped  into  bins  or  intervals,  while  bar  charts  plot
        categorical data.
        The main advantages of a histogram are its simplicity and versatility. It can be used in many different situations to
        offer an insightful look at frequency distribution. For example, it can be used in sales and marketing to develop the
        most effective pricing plans and marketing campaigns.
        R creates histogram using hist() function. This function takes a vector as an input and uses some more parameters
        to plot histograms.
        Syntax
        The basic syntax for creating a histogram using R is:

                                 hist(v,main,xlab,xlim,ylim,breaks,col,border)
        The following are the details of the parameters used in the above syntax:
           • v: is a vector containing numeric values used in histogram.
           • main: indicates title of the chart.

           • col: is used to set the colour of the bars.
           • border: is used to set the border colour of each bar.
           • xlab: is used to give a description of the x-axis.
           • xlim: is used to specify the range of values on the x-axis.
           • ylim: is used to specify the range of values on the y-axis.
           • breaks: is used to mention the width of each bar.
        The script given below will create and save the histogram in the current R working directory.
        Type the following code snippet in the script:

             # Creating data for the graph.
             v <- c(9,13,21,8,36,22,12,41,31,33,19)
             # Giving chart file a name.
             png(file = "histogram.png")
             # Creating the histogram.
             hist(v,xlab = "Weight",col = "yellow",border = "blue")
             # Save the file.
             dev.off()
        On execution of the above code, it produces the following result:



























          262   Touchpad Data Science-XI
   259   260   261   262   263   264   265   266   267   268   269