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

* * Source Code

1 2 3 4 5 6 7 |
model.1<-lm(mpg~disp, data=mtcars) plot(mpg~disp, data=mtcars, xlab="", ylab="", pch=22, bg="gray") title(main="Scatterplot of n Mile per Gallon vs Displacement", xlab="Displacement", ylab="Mile per Gallon") abline(model.1, lty=2, lwd=2, col="red") text(300,30, pos=3, labels=bquote( y == .(round(model.1$coef[1],2)) * x + .(round(model.1$coef[2],2)))) |

* * The Plot

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

* * Source Code

1 2 3 4 5 6 7 8 9 |
library(lattice) library(latticeExtra) plt<-xyplot(mpg~disp, data=mtcars, panel=function(x,y,...){ panel.xyplot(x, y, pch=22, fill="gray", cex=1.2) panel.lmlineq(x,y, at=0.7, adj = c(0,-20), style=2, rotate=F, col.line="red", lty=2, lwd=2) }) update(plt, main="Scatterplot of n Mile per Gallon vs Displacement") |

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

1 2 |
install.package("lattice") install.package("latticeExtra") |

* * The Plot

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

* * Source Code

1 2 3 4 5 6 7 |
library(ggplot2) ggPlt<-ggplot(mtcars, aes(x=disp, y=mpg)) ggPlt<-ggPlt+geom_point(pch=22, fill="gray", cex=3) ggPlt<-ggPlt+geom_smooth(method="lm", se=FALSE, col="red", lty=2) ggPlt<-ggPlt+ggtitle("Scatterplot of n Mile per Gallon vs Displacement") ggPlt<-ggPlt+xlab("Displacement")+ylab("Miles per Gallon") print(ggPlt) |

Installation of ggplot2 package,

1 |
install.packages("ggplot2") |

1 2 |
model.1<-lm(mpg~disp, data=mtcars) summary(model.1) |

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.