Page 266 - Data Science class 11
P. 266

A simple scatterplot is created using the plot() function.
        Syntax
        The basic syntax for creating a scatterplot in R is:


                                plot(x, y, main, xlab, ylab, xlim, ylim, axes)
        The following is a description of the parameters used:
           • x is the data set whose values are the horizontal coordinates.
           • y is the data set whose values are the vertical coordinates.
           • main is the title of the graph.
           • xlab is the label in the horizontal axis.
           • ylab is the label in the vertical axis.

           • xlim is the limit of the values of x used for plotting.
           • ylim is the limit of the values of y used for plotting.
           • axes indicates whether both axes should be drawn on the plot.
        Example
        We use the data set "mtcars" available in the R environment to create a basic scatterplot. This is discussed in 7.1
        above. Let’s use the columns "wt" and "mpg" in mtcars.
        Type the following code snippet into the script panel of R:

             input <- mtcars[,c(‘wt’,’mpg’)]
             print(head(input))
        When we execute the above code, it produces the following result:



































        Creating the Scatterplot
        The below script will create a scatterplot graph for the relation between wt(weight) and mpg(miles per gallon).

             # Getting the input values.
             input <- mtcars[,c(‘wt’,’mpg’)]
             # Give the chart file a name.

          264   Touchpad Data Science-XI
   261   262   263   264   265   266   267   268   269   270   271