When the data x and data y that you put in are both numerical, then most likely, the plot function would draw a scatter plot for you.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM)
#moody$SCORE is data x, and moody$FINALEXAM is data y.
But there are many other transformations for you to use:
1: type="l" for lines
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,type="l")
2:type="b" for both lines and points
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,type="b")
3:type="c" for only the lines, not points, from type="b".
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,type="c")
4:type="o" for both axes "overplotted"
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,type="o")
5.type="h" for 'histogram' like
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,type="h")
6.type="s" for stair steps
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,type="s")
Now, let's come back to the original scatter plot.
How can we change the direction of the text on X-axis and Y-axis?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,las=2)
#different values of las refer to different directions.
How can we add a title for the plot?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,main="score vs exam")
#"main" means adding the title.
How can we add a subtitle?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,sub="individually")
#"sub" means subtitle
We can also add labels to X axis and Y axis
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,xlab="score",ylab="exam")
#"xlab" means the label for X-axis, "ylab" means the label for Y-axis.
How can we add color to the title?
For example, we want to fill the title with red color.
Then we need to use the "title" function.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM)
title(main="score vs exam",col.main="red")
#"col.main" means color of the title
Similarly, we can add color to the subtitle.
For example we want to add a blue color to subtitle.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM)
title(sub="individually",col.sub="blue")
We can also add a color and change size of the X label and Y label.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,xlab="",ylab="")
#we should first leave xlab and ylab to be blank to avoid overlap
title(xlab="score",ylab="exam",col.lab="orange",cex.lab=1.5)
#col.lab controls the color of labels, the value of cex.lab controls the size of the labels (default is 1).
How can we change the color of the points?
For example we want the points to be blue.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,col="blue")
How can we change the shape of the points?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM,pch=5)
#change the value of pch and see what would happen
How can we add a regression line to the plot?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM)
abline(lm(moody$FINALEXAM~moody$SCORE))
#"lm" means a regression line. The reason that I changed the order of the "FINALEXAM" and "SCORE" is that I must use "~" rather than a "," , and in that case, you should change the order of the data.
You can also change the color of the regression line to distinguish it
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/SmallMoody.csv")
plot(moody$SCORE,moody$FINALEXAM)
abline(lm(moody$FINALEXAM~moody$SCORE),col="red")
At last, the function "plot" is a general function, it may also make a mosaic plot, barplot, histogram based on the type of data you provided.
Now it's your time to try it!