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

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

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

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
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)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.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
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

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)