I have the data frame df
, structured like this:
age salary graduate
19 22000 "no"
25 24000 "yes"
23 26000 "yes"
30 30000 "no"
I made the plot of the salary as a function of age, changing the color of the dot whether the person is a graduate or not in this way:
plot(df$age, df$salary, col=ifelse(df$graduate=="yes", "blue", "red"), pch=16, xlab="age", ylab="salary")
How can I add a legend titled "Graduate" with an explanation of the color of the dots (i.e. blue dot = graduate, red dot = not graduate)?
CodePudding user response:
You can change the shape using pch
see ?legend
for more details
df <- data.frame(age=c(19,25,23,30),salary=c(22000,24000,26000,30000),graduate=c("no","yes","no","no"))
plot(df$age, df$salary, col=ifelse(df$graduate=="yes", "blue", "red"), pch=16, xlab="age", ylab="salary")
legend("topleft",pch=16, legend=c("Graduated", "Not Graduated"),
col=c("blue", "red"), title="Graduated")
Output: