0

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.

sfyn
  • 35
  • 2
  • 8
  • 1
    I'm afraid I don't have `Default` in my base data sets. Is it in a package? – Rich Scriven Aug 28 '14 at 23:44
  • Item 1: Read ?points to find out which of the five triangles you are using. (If you are colouring them you probably want pch = 17, 24, or 25). Then use `plot(..., col = ifelse(Default$default == x, "red", "black"), ...)` where `x` is the pch for the triangle you are colouring. – Dale Aug 29 '14 at 00:05
  • @Richard: Thanks for looking into this. It is not a separate package. It is one of the preloaded datasets on my R studio version 3.0.3. – sfyn Aug 29 '14 at 00:47
  • Well, I can make an attempt with a different data set I guess. It'll be the same in theory. Looks like `Default` data is in the ISLR package – Rich Scriven Aug 29 '14 at 00:49
  • @Dale: The plot function finds the triangle by default when I use pch=a.numeric(Default$default). The gap in my logic is how to color these symbols separately. I looked over your suggestion about using the if/else but don't completely grasp your logic (I am new and learning). If else is a constraint so why use a constraint function when the graph always displays all points? – sfyn Aug 29 '14 at 00:53
  • @Dale I found another post with more details on the ifelse path ((http://stackoverflow.com/a/2376045/3988519))and I was able to get the right results. Thank you for your help. You can add this as an answer and I will select it. – sfyn Aug 29 '14 at 01:16

1 Answers1

0

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)
sfyn
  • 35
  • 2
  • 8