Here we have some basic plots: mosaicplot, scatter plot, and boxplot about score.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
plot(moody$GRADE,moody$ON_SMARTPHONE)
plot(moody$GRADE,moody$SCORE)
plot(moody$SCORE,moody$FINALEXAM)
In the above code, it can be found that only one plot shows at a time, which can be annoying sometimes. How can we combine all of these plots together, and get a big graph?
Here we use "par" function
1. First let's create a graph with two plots inside (two mosaic plot)
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(mfrow=c(2,1))
# 2 refers to 2 rows, 1 refers to 1 column, in other word you can have two plots in this graph, and each one takes up one row.
plot(moody$ON_SMARTPHONE,moody$ASKS_QUESTIONS)
plot(moody$LEAVES_EARLY,moody$LATE_IN_CLASS)
2. Now let's try to combine "Grade vs Smartphone" and "Grade vs Score" together.
Be careful about the result!
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(mfrow=c(2,1))
plot(moody$GRADE,moody$ON_SMARTPHONE)
plot(moody$GRADE,moody$SCORE)
Oops! It shows "Error in plot.new() : figure margins too large"
You may often encounter such a condition. The problem is that you have a small region which is not sufficiently large enough to contain the default margins.
In that case, the "mar" function helps you to adjust the size of the plots
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(mfrow=c(2,1))
par(mar=c(1,2,1,2))
#mar=c(height 1, width 1, height 2, width 2), we need two rows to contain two plots, so each plot should take 1/2 of the whole graph.
plot(moody$GRADE,moody$ON_SMARTPHONE)
plot(moody$GRADE,moody$SCORE)
Similarly, we can have a big graph with three plots:
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(mfrow=c(3,1))
# 3 refers to 3 rows, 1 refers to 1 column, in other word you can have three plots in this graph, and each one takes up one row
plot(moody$GRADE,moody$ON_SMARTPHONE)
plot(moody$GRADE,moody$SCORE)
plot(moody$SCORE,moody$FINALEXAM)
Because of limited space, sometimes you can't see all the content in one plot. For example, we can't see all the words on Y-axis in the previous mosaic plot.
So let's adjust the direction of those words.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(mfrow=c(3,1))
mosaicplot(moody$GRADE~moody$ON_SMARTPHONE,las=2)
#"las" function changes the direction of the words on X-axis and Y-axis
plot(moody$GRADE,moody$SCORE)
plot(moody$SCORE,moody$FINALEXAM)
Now you may apply the same rule to create a four row graph to contain four plots:
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(mfrow=c(4,1))
plot(moody$GRADE,moody$ON_SMARTPHONE)
plot(moody$GRADE,moody$SCORE)
plot(moody$SCORE,moody$FINALEXAM)
plot(moody$ON_SMARTPHONE,moody$ASKS_QUESTIONS)
Very crowded, right? Now is the right time to shift your attention to separate the columns.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(mfrow=c(2,2))
#generate a graph which has 2 rows and 2 columns
plot(moody$GRADE,moody$ON_SMARTPHONE)
plot(moody$GRADE,moody$SCORE)
plot(moody$SCORE,moody$FINALEXAM)
plot(moody$ON_SMARTPHONE,moody$ASKS_QUESTIONS)
You may not always want all the plots to be of equal size. To change the size of plots, you can apply the "fig" function.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
#the format of the fig= parameter is a numerical vector of the form c(x1, x2, y1, y2)
# the value of x and y should between 0 and 1
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
How can we add another plot in the same graph after we have already created one
For example, how can we add a horizontal boxplot after the scatter plot?
Can we directly use the "par" function again without any changes?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
par(fig=c(0,0.8,0.7,1))
#This code implies that the next plot takes the 0~0.8 horizontal range and 0.7~1 vertical range
boxplot(moody$SCORE, horizontal=TRUE)
We can see that the scatter plot and the boxplot show on different pages, which is not what we want.
Here is the second way to combine plots together.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
par(fig=c(0,0.8,0.7,1),new=TRUE)
#"new = TRUE" means adding a plot to the existing plot. If "new = FALSE", the boxplot will show up on a new page.
boxplot(moody$SCORE, horizontal=TRUE)
Then we can also add a vertical boxplot related to the same scatter plot.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
par(fig=c(0.7,1,0,0.8),new=TRUE)
#This code implies that the next plot takes the 0~0.8 vertical range and 0.7~1 horizontal range
boxplot(moody$FINALEXAM, horizontal=FALSE)
#horizontal=FALSE means the boxplot is vertical
Now we can conbine the scatter plot and those two boxplots together.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
par(fig=c(0.7,1,0,0.8),new=TRUE)
boxplot(moody$FINALEXAM, horizontal=FALSE)
par(fig=c(0,0.8,0.7,1),new=TRUE)
boxplot(moody$SCORE, horizontal=TRUE)
When you combine different plots together, sometimes you may also want to erase the border of some plots to show closer relationships between each other.
For example you may want to erase the borders of the two boxplots. Then you can type in "axes=FALSE".
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
par(fig=c(0.7,1,0,0.8),new=TRUE)
boxplot(moody$FINALEXAM, horizontal=FALSE,axes=FALSE)
#axes=FALSE can erase the border
par(fig=c(0,0.8,0.7,1),new=TRUE)
boxplot(moody$SCORE, horizontal=TRUE,axes=FALSE)
How can we add a big title for multiple plots?
Here we also use the code above.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
par(fig=c(0,0.8,0.7,1),new=TRUE)
boxplot(moody$SCORE, horizontal=TRUE,axes=FALSE)
par(fig=c(0.7,1,0,0.8),new=TRUE)
boxplot(moody$FINALEXAM, horizontal=FALSE,axes=FALSE)
title("Score vs Final exam" )
#"title(content of the title)"
The title is now above the last plot that we put into the graph, but how can we let the title above the whole graph?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
par(fig=c(0.7,1,0,0.8),new=TRUE)
boxplot(moody$FINALEXAM, horizontal=FALSE,axes=FALSE)
par(fig=c(0,0.8,0.7,1),new=TRUE)
boxplot(moody$SCORE, horizontal=TRUE,axes=FALSE)
title("Score vs Final exam",outer=TRUE )
#outer=TRUE means to add the title out of the whole graph
Finally, let's adjust the position of the title.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
par(fig=c(0,0.8,0,0.8))
plot(moody$SCORE,moody$FINALEXAM)
par(fig=c(0.7,1,0,0.8),new=TRUE)
boxplot(moody$FINALEXAM, horizontal=FALSE,axes=FALSE)
par(fig=c(0,0.8,0.7,1),new=TRUE)
boxplot(moody$SCORE, horizontal=TRUE,axes=FALSE)
title("Score vs Final exam",outer=TRUE ,line=-3)
#The value of line adjusts the position of the title. Smaller the value, the closer the title is to the graph.