Scatter plots in RAbstract: The main purpose of this page is to learn to plot in R. This document also explains plotting scatter plot in R and how it can be used to visualize and interpret the data.
Introduction: scatter plots are widely used in economics and finance to get a basic idea of the underlying datset.
The dataset used is house price dataset available alongwith an undergarduate textbook “Introductiory Econometrics” by Jeffrey M. Wooldridge. The data consists of 506 observations.The dataset consist of the following variables:
- price- median housing price, $
- crime- crimes committed per capita
- nox - nitrous oxide, parts per 100 mill.
- rooms - avg number of rooms per house
- dist - weighted dist. to 5 employ centers
- radial - accessibiliy index to radial hghwys
- proptax - property tax per $1000
- stratio - average student-teacher ratio
- lowstat - % of people 'lower status'
- lprice- log(price)
- lnox - log(nox)
- lproptax - log(proptax)
R code: Since the data was available in the Raw text format i copy pasted the data in Excel and saved it as a CSV file under my R directory. Then i use the read.csv command to read in the csv file and save the dataset in hprice. I have also made use of the colnames command to get additional information of the colnames and the summary command to breifly look at the center and distribution of X and Y variables.
hprice <- read.csv("hprice.csv", header = TRUE, sep = ",") colnames(hprice)
##  "price" "crime" "nox" "rooms" "dist" "radial" ##  "proptax" "stratio" "lowstat" "lprice" "lnox" "lproptax"
## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 5000 16800 21200 22500 25000 50000
The following line of commands will plot a scatterplot in R.The scatter plot generated will have circles which are not filled up. However, to fill up the circles the user needs to use the “pch” command in R.
## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 0.01 0.08 0.26 3.61 3.68 89.00
plot(hprice$price, hprice$crime, col = "BLUE")
plot - basic plot command in R hprice$price - X variable in Plot hprice$crime - Y variable in plot command col - color
plot(hprice$price, hprice$crime, pch = 19, col = "BLUE")
You would observe that the points are too close to each other and so an additional command “cex” can be added. cex command will help in reducing the size of the circles.
plot(hprice$price, hprice$crime, pch = 19, cex = 0.5, col = "BLUE")
The image above provides much more information about the relationship between x and Y variables. We can observe that as the home prices rise the crime rate significantly drop. It is not hard to reason with this, as people would not like to live in areas where the crime rates are high and hence the supply of houses are greater than the demand resulting in lower prices. On the other hand demand for houses will be much higher when the crime rates are low as people are willing to spend extra for their security and safety.