1. Abhik Seal: Scatter plots in R
I have taken a sample from the Diamonds data from ggplot2 package
>sample1<-diamonds[sample(nrow(diamonds),400),]
>plot(sample1$price~sample1$carat,#Y~X
main = "Relation of diamonds price and carat",# Title
xlab ="Price",ylab = "carat",# X and Y label
xaxis="i",yaxis="i",# set axis style as internal
col ="red",# Color of the dots
pch=16)#Set the plotting symbol to filled dots
The pch attribute is changed to 2 a different symbol is noticed in figure 2.
Fig 1 Fig 2
To set the plot background color to gray we use the bgargument in the par() command
>par(bg = “gray”)
Then running the plot code you will get something like in fig 3. In par() until the plotting device is closed or a
new device is initiated, the background color stays the same.
To set the background color for a the plot region a rectangle must be drawn around the plot region figure 4 The
script is given below.
plot(sample1$price~sample1$carat,type="n") # "n" so that the plotted elements are invisible.
x<-par("usr") #gets us the co-ordinates of the plot region in a vector of form c(xleft, xright, ybottom, ytop)
rect(x[1],x[3],x[2],x[4],col="gray ")
points(plot(sample1$price~sample1$carat,pch=10)
2. Fig 3 Fig 4
If you want to make the title as red, axis values as blue and axis labels as red then you can do this figure 5
plot(sample1$price~sample1$carat,#Y~X
main = "Relation of diamonds price and carat",# Title
xlab ="Price",ylab = "carat",# X and Y label
xaxis="i",yaxis="i",# set axis style as internal
col.main ="red",# Color of the dots
col.axis="blue",
col.lab="red",pch=16)
Plotting point, symbols, styles and sizes
I used this data (http://www.imdpune.gov.in/ncc_rept/RESEARCH%20REPORT%202.pdf ) from the year 1999 to
2003 and the months from jan to December to plot yearly rainfall.
NB: Columns are years and rows are months.
Here is the code
plot(rain$Year1999 ,main="Yearly Rainfall in India",xlab="Month of the Year",ylab="rainfall",pch=1)
points(rain$Year2000,pch=2)
points(rain$Year2001,pch=3)
points(rain$Year2002,pch=4)
points(rain$Year2003,pch=5)
legend('top',legend=c("1999","2000","2001","2002","2003"),ncol=5,cex=0.9,bty="n",pch=1:5)
Lines format can also be plotted using the lty and lwd arguments in the plot
plot(rain$Year1999
,main="Yearly Rainfall in India",xlab="Month of the Year",ylab="rainfall(mm)",type="l",lty=1,lwd=2,ylim=c(0,400))
lines(rain$Year2000,lty=2,lwd=2)
lines(rain$Year2001,lty=3,lwd=2)
lines(rain$Year2002,lty=4,lwd=2)
lines(rain$Year2003,lty=5,lwd=2)
legend('topright',legend=c("1999","2000","2001","2002","2003"),ncol=5,cex=0.9,bty="n",lty=1:5,lwd=2)
3. Lines type code:
0: blank
1: solid (default)
2: dashed
3: dotted
4: dotdash
5: longdash
6: twodash
axis(1,at=1:length(rain$Month),labels=rain$Month)
lines(rain$Year2000,col="red",type="b",lwd=2)
lines(rain$Year2001,col="orange",type="b",lwd=2)
lines(rain$Year2002,col="purple",type="b",lwd=2)
lines(rain$Year2003,col="yellow",type="b",lwd=2)
legend('topright',legend=c("1999","2000","2001","2002","2003"),col=
c("black","red","orange","purple","yellow"),ncol=2,cex=0.9,bty="n",lty=1:5,lwd=2)
Using xyplot to plot data.
I have used the sample1 data from the diamonds dataset . You also require the lattice package for plotting.You can
also plot high quality plots using ggplot see my qplot tutorial at slideshare.
xyplot(price~carat,data=sample1,groups=color,auto.key=list(corner=c(1,1)))
The auto.key use to make a legend at the corner of the plot.
4. Fitting linear model lines using lm() and abline()
plot(sample1$price~sample1$carat)
lmfit<-lm(sample1$price~sample1$carat)
abline(lmfit,col='red')
legend("topright", bty="n", legend=paste("R2 is", format(summary(lmfit)$adj.r.squared, digits=4)))# putting the
R square in the plot.
library(scatterplot3d)
scatterplot3d(x=sample1$carat,y=sample1$depth,z=sample1$price,xlab="carat",ylab="depth",zlab="price")#fig
scatterplot3d(x=sample1$carat,y=sample1$depth,z=sample1$price,xlab="carat",ylab="depth",zlab="price",pch
=16,highlight.3d=TRUE,angle=45,type="h")
5. More on the scatterplot3d at http://www.jstatsoft.org/v08/i11/paper
>plot(sample1$price~sample1$carat)
>rug(sample1$carat)
>rug(sample1$price,side=2,col="blue",ticksize=0.02)
The rug function adds a set of lines just above the x and y axis.The closely packed lines represent
the density of the data.The side argument takes four values,1: bottom axis (default) ,2: left ,3: top ,4:
right.The size of the tick marks are adjusted by ticksize.
smoothScatter :smoothScatter() function produces a smoothed color density representation of the
scatter plot, obtained through a kernel density estimate.
Lab.palette<-colorRampPalette(c("blue","red"), space = "Lab")
smoothScatter(sample1$price~sample1$carat,colramp = Lab.palette)