Plotting with Continuous Explanatory Variables: Scatterplots

x <- 1:10
x
##  [1]  1  2  3  4  5  6  7  8  9 10

Using “concatenate()” to add specific elements to list y

y <- c(11,12,9,7,5,8,4,4,5,3)
y
##  [1] 11 12  9  7  5  8  4  4  5  3

Here, x = explanatory variable, y = response variable. Plotting them against each other on a scatterplot below using plot(). All material is added to the existing graph, till another plot command goes out.

“pch” refers to “plotting character” and “xlab” refers to “x-axis label”.

“abline()” helps add straight lines to plots, while “lm(y~x)” estimates y as a linear function of x.

lines() helps in drawing another customer line with a list of x-points(0,10) and a list of y-points (12,0), “lty” refers to line type, which is 2 in this case.

plot(x,y,pch=1,xlab="explanatory variable",ylab="response variable")
abline(lm(y~x),lty=1)
lines(c(0,10),c(12,0),lty=6)

plot(x,y,pch=2,xlab="explanatory variable",ylab="response variable")
abline(lm(y~x), lty=3)
lines(c(0,10),c(12,0),lty=2)

plot(x,y,pch=3,xlab="explanatory variable",ylab="response variable")
abline(lm(y~x), lty=5)
lines(c(0,10),c(12,0),lty=3)

plot(x,y,pch=4,xlab="explanatory variable",ylab="response variable")
abline(lm(y~x), lty=7)
lines(c(0,10),c(12,0),lty=4)

plot(x,y,pch=5,xlab="explanatory variable",ylab="response variable")
abline(lm(y~x), lty=9)
lines(c(0,10),c(12,0),lty=5)

Adding a custom list of coordinates to an existing scatterplot-

plot(x,y,pch=5,xlab="explanatory variable",ylab="response variable")
abline(lm(y~x), lty=2)
lines(c(0,10),c(12,0),lty=3)
v<-c(2,4,6,8,10)
w<-c(8,5,6,6,2)
points(v,w,pch=3)
abline(lm(w~v),lty=4)

Plotting with Categorical Explanatory Variables: Box Plots read.table function helps read the data from the text file and copy it into our dataframe “weather”. names() helps get/set names of an object, giving variables names from the weather dataframe in this case. The attach() helps attach a specific data source to R’s search directory, using which we can refer to objects in that dataframe directly.

weather<- read.table("c:\\temp\\SilwoodWeather.txt",header=T)
names(weather)
## [1] "upper" "lower" "rain"  "month" "yr"
attach(weather)
month<-factor(month)
is.factor(month)
## [1] TRUE
plot(month, upper)

CREATING GRAPHS WITH COLORS

pie(rep(1, 30), col = rainbow(30), radius = 0.9)

pie(rep(1, 15), col = rainbow(15), radius = 1)

pie(rep(1, 5), col = rainbow(5), radius = 0.5)

Highlight points on a graph using colour, draw different lines in different colours, and/or change the colour of the background.

seq() generates a random sequence using the to/from values as well as increment valu supplied as arguments.

par() is invoked to set or query graphical parameters of a visualization. Parameters are supplied as argument.

plot() is used here only to create a frame - on top of which both lines will be plotted finally.We have used y2 as the y parameter for this plot since y2 has larger units compared to y1. Finally, lines() helps plot the required curves.

x <- seq(0,10,0.1)
y1 <- 2 + 3*x - 0.25*x^2
y2 <- 3 + 3.3*x - 0.3*x^2
par(bg="ghostwhite")
plot(x,y2,type="n",ylab="")
lines(x,y1,col="blue")
lines(x,y2,col="red")

COLORED SCATTERPLOTS

The “for” loop helps join the daily min and max temps by creating a line between (1,tmin[x]) and (1,tmax[x]).

jantemps<-read.table("c:\\temp\\jantemp.txt",header=T)
attach(jantemps)
names(jantemps)
## [1] "tmax" "tmin" "day"
max(tmax)
## [1] 10.8
min(tmin)
## [1] -11.5
plot(day,tmax,ylim=c(-12,12),type="n",ylab="Temperature")
points(day,tmax,col="red",pch=16)
points(day,tmin,col="blue",pch=16)
for (i in 1:31) lines(c(i ,i ), c( tmin[i], tmax[i] ), col="yellow")

COLORED HISTOGRAMS

x <- rnorm(1000)
par(bg="sky blue")
hist(x, col="yellow", main="Histogram of Random Variable X")

COLORED PIE CHARTS

“fate” stores the value of the proportion of insects landing on a given plant. Using names() we define the names of plat species and then create a pie chart using pie().

fate <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
names(fate) <- c("Ragwort","Thistle","Willowherb","Rush","Orchid", "Knapweed")
pie(fate, col = c("purple","violetred3","green4","cornsilk","cyan","white"))

MULTIVARIATE PLOTS

pollution<-read.csv("c:\\temp\\pollute.csv",header=T)
attach(pollution)
## The following object is masked _by_ .GlobalEnv:
## 
##     pollution
## The following object is masked from weather:
## 
##     rain
names(pollution)
## [1] "pollution"  "temp"       "industry"   "population" "wind"      
## [6] "rain"       "wet.days"
pairs(pollution,panel=panel.smooth)

TREE BASED MODELS

library(tree)
regtree <- tree(pollution ~.,data=pollution)
plot(regtree)
text(regtree)

CONDITIONING PLOTS

par() takes graphical parameters for plots and “mfrow” provides arrangement of plots (no. of rows and total number of plots per row).

coplot(pollution$pollution ~ pollution$temp | pollution$rain, xlab="Temp", ylab="Pollution")

coplot(pollution$population ~ pollution$temp | pollution$rain, xlab="Temp", ylab="Population")

par(mfrow=c(1,1))

LOGARITHMIC AXES

plotdata<-read.table("c:\\temp\\plotdata.txt", header =T)
attach(plotdata)
## The following objects are masked _by_ .GlobalEnv:
## 
##     x, y
names(plotdata)
## [1] "x" "y"
par(mfrow=c(2,2))
plot(plotdata$x,plotdata$y,type="b")
plot(plotdata$x,plotdata$y,log="xy",type="b")
plot(plotdata$x,plotdata$y,log="y", type="b")
plot(plotdata$x,plotdata$y,log="x", type="b")

SCALING AXES

To change the upper and lower values on an axis

par(mfrow=c(1,2))
plot(plotdata$x,plotdata$y,type="l")
plot(plotdata$x,plotdata$y,ylim=c(0,50),type="l")

TEXT IN GRAPHS This is done using text() which takes x,y values of the placement location alongwith the text as arguments.

map.places <-read.csv("c:\\temp\\map.places.csv",header=T)
attach(map.places)
names(map.places)
## [1] "Wanted"
map.data<-read.csv("c:\\temp\\bowens.csv",header=T)
attach(map.data)
names(map.data)
## [1] "place" "east"  "north"
nn<-ifelse(map.data$north<60,north+100,north)
plot(c(20,100),c(60,110),type="n",xlab="",ylab="")