Basic Scatter Plot and linear fitted line

Lets scatter into some points created by data in xy-space for some time. Data are scattered everywhere but what relation is there between some specific variable with other. Almost all the research work relied under this fundamental question. Cutting down to simple talking and stick to the heading, we can use mtcars dataset packaged with R programming package.

The dataset from Motor Trend US magazine, 1974 comprises fuel consumption and 10 various aspects of automobile design and their performance for 32 automobiles of different models. I will try to obtain the scatter plot for the model and the fitted line for the model.

In R, there are three popular packages for obtaining plots – Base Graphics, Lattice Plot and ggPlot. following examples will codes for all these packages. Lets move forward for creating simple scatter plot of mpg vs disp from the mtcars dataset.

The Plot

scatter-base

Source Code

The Plot

You need to install lattice and latticeExtra package for creating lattice plots. latticeExtra include a wide variety of various manipulations and tweaking.

scatter-lattice

Source Code

To install lattice and latticeExtra package, you can use install.packages() function. It should be loaded after installation with library() function.

The Plot

Creating a ggPlot graphics is possible with installation of ggplot2 package. This package can create beautiful and sophisticated plots with simple syntax.

scatter-ggplot

Source Code

Installation of ggplot2 package,


The output is,

Call:
lm(formula = mpg ~ disp, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.8922 -2.2022 -0.9631  1.6272  7.2305 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 29.599855   1.229720  24.070  < 2e-16 ***
disp        -0.041215   0.004712  -8.747 9.38e-10 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.251 on 30 degrees of freedom
Multiple R-squared:  0.7183,	Adjusted R-squared:  0.709 
F-statistic: 76.51 on 1 and 30 DF,  p-value: 9.38e-10

The model we have fitted can also be written in functional form, i.e. in equation, as,

\(
y=29.6-0.04x
\)

This means, the effect of displacement on mile per gallon of the cars in the model is negative and its magnitude is 0.04. In other words, on one unit change of displacement, the car will travel 0.04 less per gallon.

Leave a Reply