Search code examples
rplotcolorslegendlegend-properties

Changing the color of legend in plot for R


so I am hiving a minor issue here with my graph's legend. I can't seem to get "RCB Class 1,2 or 3" to show as a black dot instead of red. I am new to R and making graphs with R. Please help :) Image and code attached below. Also, if anyone knows how to make the legend font size smaller and how to add more axis tick marks so that it counts by 10s, please let me know. enter image description here par(mar = c(6, 5, 5, 10), xpd = TRUE) # par parameters to add space for legend

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','black'),
panel.first = grid())
legend("topright", inset = c(- 0.45, 0),   # Create legend outside of plot
   legend = c("pCR","RCB Class 1,2 or 3"),
   pch=1,
   col="red","black")

Solution

  • We can specify the col as a vector by concatenating (c) the two colors

    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','black'),
       panel.first = grid())
    legend("topright", inset = c(- 0.45, 0),  
       legend = c("pCR","RCB Class 1,2 or 3"),
       pch=1,
       col=c("red","black"))