rplotgraphconditional-statementsgraph-coloring

Conditionally coloring data points based on a 3rd parameter with plot function in R


I am new to using R, and I am trying to make a scatterplot, which selectively will color data points based on a third parameter.

My y variable is PR staining, and my x variable is ER staining. I seek to color the data pts red which satisfy pcr=1; all the other data pts should be black. pcr is a separate column in my dataset.

ERPR_plot <-- plot(mint$er_pct,mint$pr_pct,
xlab="ER Staining (%)",
ylab="PR Staining (%)",
xlim=range(0:100),
ylim=range(0:100),
main = "PR Percent Staining vs. ER Percent Staining",
col = ifelse(mint$pcr = 1,'red','green'))


Error: unexpected '=' in:
"  main = "PR Percent Staining vs. ER Percent Staining",
col = ifelse(mint$pcr ="

Please help. I've been searching threads for hours :)


Solution

  • Here, we are using the assignment (=) instead of comparative operator (==) in ifelse test argument which based on the documentation of ?ifelse is

    test - an object which can be coerced to logical mode.

    ifelse(mint$pcr == 1,'red','green'))