Quality Learning Support For All
First time here? Checkout the FAQs!
x

*Math Image Search only works best with SINGLE, zoomed in, well cropped images of math. No selfies and diagrams please :)

For Example

Math Image Search 1
Math Image Search 2

Join the MathsGee Club for exclusive quizzes, courses and interactive content without ads.



ASK - ANSWER - COMMENT - VOTE - CREATE


Sites: Global Q&A | Wits | MathsGee Club | Joburg Libraries | StartUps | Zimbabwe | OER

MathsGee is Zero-Rated (You do not need data to access) on: Telkom |Dimension Data | Rain | MWEB

1 like 0 dislike
116 views
How will you create scatterplot matrices in R language?
in Data Science & Statistics by Diamond (88,334 points) | 116 views

1 Answer

0 like 0 dislike
Best answer
  1. Launch RStudio as described here: Running RStudio and setting up your working directory

  2. Prepare your data as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files

  3. Import your data into R 

Data

iris data is used in the following examples. iris data set gives the measurements in centimeters of the variables sepal length and width, and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.

head(iris)

 

R base scatter plot matrices: pairs()

  • Basic plots:
pairs(iris[,1:4], pch = 19)

 

  • Show only upper panel:
pairs(iris[,1:4], pch = 19, lower.panel = NULL)

 

  • Color points by groups (species)
my_cols <- c("#00AFBB", "#E7B800", "#FC4E07")  
pairs(iris[,1:4], pch = 19,  cex = 0.5,
      col = my_cols[iris$Species],
      lower.panel=NULL)

 

  • Add correlations on the lower panels: The size of the text is proportional to the correlations.
# Correlation panel
panel.cor <- function(x, y){
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- round(cor(x, y), digits=2)
    txt <- paste0("R = ", r)
    cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.5, txt, cex = cex.cor * r)
}
# Customize upper panel
upper.panel<-function(x, y){
  points(x,y, pch = 19, col = my_cols[iris$Species])
}
# Create the plots
pairs(iris[,1:4], 
      lower.panel = panel.cor,
      upper.panel = upper.panel)

 

  • Add correlations on the scatter plots:
# Customize upper panel
upper.panel<-function(x, y){
  points(x,y, pch=19, col=c("red", "green3", "blue")[iris$Species])
  r <- round(cor(x, y), digits=2)
  txt <- paste0("R = ", r)
  usr <- par("usr"); on.exit(par(usr))
  par(usr = c(0, 1, 0, 1))
  text(0.5, 0.9, txt)
}
pairs(iris[,1:4], lower.panel = NULL, 
      upper.panel = upper.panel)

 

Use the R package psych

The function pairs.panels [in psych package] can be also used to create a scatter plot of matrices, with bivariate scatter plots below the diagonal, histograms on the diagonal, and the Pearson correlation above the diagonal.

library(psych)
pairs.panels(iris[,-5], 
             method = "pearson", # correlation method
             hist.col = "#00AFBB",
             density = TRUE,  # show density plots
             ellipses = TRUE # show correlation ellipses
             )

 

 

by Diamond (88,334 points)

Related questions

0 like 0 dislike
0 answers
0 like 0 dislike
0 answers
asked Mar 12, 2019 in Data Science & Statistics by MathsGee Diamond (88,334 points) | 50 views
0 like 0 dislike
0 answers
1 like 0 dislike
1 answer
0 like 0 dislike
0 answers
0 like 0 dislike
0 answers
asked Mar 11, 2019 in Data Science & Statistics by Edzai Zvobwo Bronze Status (8,810 points) | 45 views
0 like 0 dislike
0 answers
asked Mar 12, 2019 in Data Science & Statistics by MathsGee Diamond (88,334 points) | 36 views
0 like 0 dislike
0 answers
1 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
0 answers
0 like 0 dislike
0 answers
0 like 0 dislike
0 answers
asked Mar 11, 2019 in Data Science & Statistics by Edzai Zvobwo Bronze Status (8,810 points) | 30 views
0 like 0 dislike
0 answers
asked Mar 11, 2019 in Data Science & Statistics by Edzai Zvobwo Bronze Status (8,810 points) | 54 views
1 like 0 dislike
1 answer
1 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
0 answers
0 like 0 dislike
0 answers
0 like 0 dislike
1 answer
asked Sep 6 in Mathematics by Siyavula Bronze Status (6,280 points) | 7 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Mar 11, 2019 in Data Science & Statistics by Edzai Zvobwo Bronze Status (8,810 points) | 44 views
0 like 0 dislike
0 answers
0 like 0 dislike
0 answers
asked Mar 12, 2019 in Data Science & Statistics by MathsGee Diamond (88,334 points) | 40 views
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
0 answers
0 like 0 dislike
1 answer
asked Jun 26, 2020 in Data Science & Statistics by Teddy Bronze Status (9,942 points) | 55 views

Join the MathsGee Answers & Explanations community and get study support for success - MathsGee Answers & Explanations provides answers to subject-specific educational questions for improved outcomes.



On MathsGee Answers & Explanations, you can:


  1. Ask questions
  2. Answer questions
  3. Comment on Answers
  4. Vote on Questions and Answers
  5. Donate to your favourite users
  6. Create/Take Live Video Lessons

Posting on MathsGee Answers & Explanations


  1. Remember the human
  2. Behave like you would in real life
  3. Look for the original source of content
  4. Search for duplicates before posting
  5. Read the community's rules

Join the MathsGee Club for exclusive quizzes, courses and interactive content without ads.



Wits Maths Questions

Solve the linear equation:
Posted on Thursday September 02, 2021

Solve the equation:

(5(2-3x)+3(5x-2)-7(4-x)=0)

What is the definition of arcsin?
Posted on Thursday September 02, 2021

What is the definition of arcsin?

What are the condition for the existence of an inverse trigonometric function?
Posted on Thursday September 02, 2021

What are the condition for the existence of an inverse trigonometric function?

Click Here To Read More.
 

Given \(y=2 x=f(x)\) find \(f^{-1}\)
Posted on Thursday September 02, 2021

Given (y=2 x=f(x)) find (f^{-1})

What is the notation for inverse trigonometric functions?
Posted on Thursday September 02, 2021

What is the notation for inverse trigonometric functions?

MathsGee Tools

Math Worksheet Generator

Math Algebra Solver

Trigonometry Simulations

Vectors Simulations

Matrix Arithmetic Simulations

Matrix Transformations Simulations

Quadratic Equations Simulations

Probability & Statistics Simulations

PHET Simulations

Visual Statistics

Join the MathsGee Club for exclusive quizzes, courses and interactive content without ads.



MathsGee ZOOM | eBook

Join the MathsGee Club for exclusive quizzes, courses and interactive content without ads.