I have 6 scatter plots created using the "pairs" function, and I want to plot the linear regression line for each scatter plot on top of each respective scatter plot.
I have tried writing my own function and using this function as the argument for upper.panel
This is the code that produces my scatter plots on the upper diagonal. Each color represents a different class of iris flower.
pairs(iris_data_excel[1:4], lower.panel=NULL, col=c("red","blue","green")
[class_to_number])
Plot created from the above code
This is the function I wrote and tried to use as an argument for upper.panel
upper_panel_regression_line = function(x,y){
linear_regression = lm(x, y)
linear_regression_line = abline(linear_regression)
}
This is where I input the function for the argument "upper.panel"
pairs(iris_data_excel[1:4], lower.panel=NULL, upper.panel =
upper_panel_regression_line, col=c("red","blue","green")[class_to_number])
This is the error I get
Error in lower.panel(...)
unused argument (col = c("red", "blue", "green")[class_to_number])
Example which can be used to reproduce the plot using the built in iris dataset:
#Extracts the iris species column from the iris dataset
iris_class = iris$Species
#Change the class from characters to numerical values to be used for
#indexing
# #1 = Iris-setosa
# #2 = Iris-versicolor
# #3 = Iris-virginica
class_to_number = as.numeric(factor(iris_class))
#Scatter plot matrix
pairs(iris[1:4], lower.panel=NULL, col=c("red","blue","green")
[class_to_number])