SlideShare a Scribd company logo
1 of 5
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)
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)
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.
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")
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)

More Related Content

What's hot

Experement no 6
Experement no 6Experement no 6
Experement no 6
Smita Batti
ย 
My favorite slides
My favorite slidesMy favorite slides
My favorite slides
Mitchell Wand
ย 

What's hot (20)

Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
ย 
Kwp2 091217
Kwp2 091217Kwp2 091217
Kwp2 091217
ย 
NumPy Refresher
NumPy RefresherNumPy Refresher
NumPy Refresher
ย 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics lab
ย 
Julia Set
Julia SetJulia Set
Julia Set
ย 
Lecture on graphics
Lecture on graphicsLecture on graphics
Lecture on graphics
ย 
Surface3d in R and rgl package.
Surface3d in R and rgl package.Surface3d in R and rgl package.
Surface3d in R and rgl package.
ย 
Experement no 6
Experement no 6Experement no 6
Experement no 6
ย 
My favorite slides
My favorite slidesMy favorite slides
My favorite slides
ย 
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
ย 
PYTHON PROGRAMS FOR BEGINNERS
PYTHON PROGRAMS FOR BEGINNERSPYTHON PROGRAMS FOR BEGINNERS
PYTHON PROGRAMS FOR BEGINNERS
ย 
2) quadratics gral form
2) quadratics gral form2) quadratics gral form
2) quadratics gral form
ย 
INVERSE FUNCTION
INVERSE FUNCTIONINVERSE FUNCTION
INVERSE FUNCTION
ย 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics Functions
ย 
Computer lab (programs)
Computer lab (programs)Computer lab (programs)
Computer lab (programs)
ย 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
ย 
Lesson 5: Functions and surfaces
Lesson 5: Functions and surfacesLesson 5: Functions and surfaces
Lesson 5: Functions and surfaces
ย 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
ย 
R-ggplot2 package Examples
R-ggplot2 package ExamplesR-ggplot2 package Examples
R-ggplot2 package Examples
ย 
Interpolation graph c++
Interpolation graph c++Interpolation graph c++
Interpolation graph c++
ย 

Similar to R scatter plots

CIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkCIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & Coursework
TUOS-Sam
ย 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Vyacheslav Arbuzov
ย 
Lectures r-graphics
Lectures r-graphicsLectures r-graphics
Lectures r-graphics
etyca
ย 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
Tanwir Zaman
ย 
Use C++ for all 1 bool mapContainsKeysarrayltintgt nu.pdf
Use C++ for all  1 bool mapContainsKeysarrayltintgt nu.pdfUse C++ for all  1 bool mapContainsKeysarrayltintgt nu.pdf
Use C++ for all 1 bool mapContainsKeysarrayltintgt nu.pdf
admin447081
ย 

Similar to R scatter plots (20)

Q plot tutorial
Q plot tutorialQ plot tutorial
Q plot tutorial
ย 
MatplotLib.pptx
MatplotLib.pptxMatplotLib.pptx
MatplotLib.pptx
ย 
CIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & CourseworkCIV1900 Matlab - Plotting & Coursework
CIV1900 Matlab - Plotting & Coursework
ย 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
ย 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
ย 
Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
ย 
Programming with matlab session 6
Programming with matlab session 6Programming with matlab session 6
Programming with matlab session 6
ย 
Lectures r-graphics
Lectures r-graphicsLectures r-graphics
Lectures r-graphics
ย 
statistical computation using R- an intro..
statistical computation using R- an intro..statistical computation using R- an intro..
statistical computation using R- an intro..
ย 
Exploratory data analysis using r
Exploratory data analysis using rExploratory data analysis using r
Exploratory data analysis using r
ย 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
ย 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
ย 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
ย 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
ย 
R Functions in Dataframe.pptx
R Functions in Dataframe.pptxR Functions in Dataframe.pptx
R Functions in Dataframe.pptx
ย 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
ย 
R programming language
R programming languageR programming language
R programming language
ย 
Lecture_3.pptx
Lecture_3.pptxLecture_3.pptx
Lecture_3.pptx
ย 
Use C++ for all 1 bool mapContainsKeysarrayltintgt nu.pdf
Use C++ for all  1 bool mapContainsKeysarrayltintgt nu.pdfUse C++ for all  1 bool mapContainsKeysarrayltintgt nu.pdf
Use C++ for all 1 bool mapContainsKeysarrayltintgt nu.pdf
ย 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
ย 

More from Abhik Seal

Networks
NetworksNetworks
Networks
Abhik Seal
ย 
Modeling Chemical Datasets
Modeling Chemical DatasetsModeling Chemical Datasets
Modeling Chemical Datasets
Abhik Seal
ย 
Introduction to Adverse Drug Reactions
Introduction to Adverse Drug ReactionsIntroduction to Adverse Drug Reactions
Introduction to Adverse Drug Reactions
Abhik Seal
ย 
Mapping protein to function
Mapping protein to functionMapping protein to function
Mapping protein to function
Abhik Seal
ย 
Sequencedatabases
SequencedatabasesSequencedatabases
Sequencedatabases
Abhik Seal
ย 
Chemical File Formats for storing chemical data
Chemical File Formats for storing chemical dataChemical File Formats for storing chemical data
Chemical File Formats for storing chemical data
Abhik Seal
ย 
Understanding Smiles
Understanding Smiles Understanding Smiles
Understanding Smiles
Abhik Seal
ย 
Learning chemistry with google
Learning chemistry with googleLearning chemistry with google
Learning chemistry with google
Abhik Seal
ย 
3 d virtual screening of pknb inhibitors using data
3 d virtual screening of pknb inhibitors using data3 d virtual screening of pknb inhibitors using data
3 d virtual screening of pknb inhibitors using data
Abhik Seal
ย 

More from Abhik Seal (20)

Chemical data
Chemical dataChemical data
Chemical data
ย 
Clinicaldataanalysis in r
Clinicaldataanalysis in rClinicaldataanalysis in r
Clinicaldataanalysis in r
ย 
Virtual Screening in Drug Discovery
Virtual Screening in Drug DiscoveryVirtual Screening in Drug Discovery
Virtual Screening in Drug Discovery
ย 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
ย 
Data handling in r
Data handling in rData handling in r
Data handling in r
ย 
Networks
NetworksNetworks
Networks
ย 
Modeling Chemical Datasets
Modeling Chemical DatasetsModeling Chemical Datasets
Modeling Chemical Datasets
ย 
Introduction to Adverse Drug Reactions
Introduction to Adverse Drug ReactionsIntroduction to Adverse Drug Reactions
Introduction to Adverse Drug Reactions
ย 
Mapping protein to function
Mapping protein to functionMapping protein to function
Mapping protein to function
ย 
Sequencedatabases
SequencedatabasesSequencedatabases
Sequencedatabases
ย 
Chemical File Formats for storing chemical data
Chemical File Formats for storing chemical dataChemical File Formats for storing chemical data
Chemical File Formats for storing chemical data
ย 
Understanding Smiles
Understanding Smiles Understanding Smiles
Understanding Smiles
ย 
Learning chemistry with google
Learning chemistry with googleLearning chemistry with google
Learning chemistry with google
ย 
3 d virtual screening of pknb inhibitors using data
3 d virtual screening of pknb inhibitors using data3 d virtual screening of pknb inhibitors using data
3 d virtual screening of pknb inhibitors using data
ย 
Poster
PosterPoster
Poster
ย 
Indo us 2012
Indo us 2012Indo us 2012
Indo us 2012
ย 
Weka guide
Weka guideWeka guide
Weka guide
ย 
Pharmacohoreppt
PharmacohorepptPharmacohoreppt
Pharmacohoreppt
ย 
Document1
Document1Document1
Document1
ย 
Qsar and drug design ppt
Qsar and drug design pptQsar and drug design ppt
Qsar and drug design ppt
ย 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
ย 

Recently uploaded (20)

Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
ย 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
ย 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
ย 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
ย 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
ย 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
ย 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
ย 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
ย 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
ย 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
ย 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
ย 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
ย 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
ย 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
ย 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
ย 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
ย 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
ย 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
ย 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
ย 

R scatter plots

  • 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)