rdatapoint

r - change colors of data points in a scatterplot


I am learning R and working with the Default dataset. My attempt is to graphically show a relationship between balance, income, and default cases. I have managed to put together a graph and now need assistance with coloring specific points.

Note: Please excuse me if this topic has been discussed before. I looked in the forum for a similar post but failed to find one.

Here is my code:

dataset(Default)

plot(Default$balance~Default$income, col=Default$student, las=1, xlab = "Income",
    ylab="Balance", main="Income and balance effects on default     
    loans",pch=as.numeric(Default$default), cex = 0.7)

legend("topright",legend=unique(Default$default), title = "Default?", pch = c(1,2))

Could a member help me:

  1. assign a different color to those which default (triangles in the graph)
  2. create 2 legends: one which shows the division of the population (either a student or not), and the second which lists that triangles represent those who defaulted and circles do the same for those who did not.

Edit: The Default dataset is in the ISLR package. Thank you @ richard for pointing this out.


Solution

  • Here is the answer for others to reference:

    plot(Default$balance~Default$income, col=ifelse(Default$default == "Yes", "green",     
    Default$student), las=1, xlab = "Income", ylab="Balance", main="Income and balance 
    effects on default loans",pch=as.numeric(Default$default), cex = 0.7)
    
    legend("topright",legend=unique(Default$default), title = "Default?", pch = c(1,2), 
    col = c("black", "green"))
    
    legend("topleft", legend=unique(Default$student), title = "Student?", 
    col=c("black", "red"), pch=1)