R: How to add fill color to a dot with legend with black circle outline

Viewed 117

I am trying to create a legend (with empty plot) where each dot is associated with a group, but can't seem to figure out how to add fill color while having a black outline. It seems that my bg parameter is not working.

This is what my code looks like:

plot(NULL ,xaxt='n',yaxt='n',bty='n',ylab='',xlab='', xlim=0:1, ylim=0:1)
legend("topleft", legend =c("X", "A1", "A2", "A3", "A4", "A5", "A6"),pch=21, pt.cex=1.5, cex=0.8, bty='n', y.intersp=1.1, bg = c("black", "red", "yellow", "green", "cyan", "blue", "magenta"), col = "black")

enter image description here

1 Answers

As @DaveArmstrong said right in the comments, you need to use pt.bg like this:

plot(NULL, xaxt='n', yaxt='n', bty='n', ylab='', xlab='', xlim=0:1, ylim=0:1)
legend("topleft", 
       legend =c("X", "A1", "A2", "A3", "A4", "A5", "A6"), 
       pch=21, pt.cex=1.5, cex=0.8, bty='n', 
       y.intersp=1.1, 
       pt.bg = c("black", "red", "yellow", "green", "cyan", "blue", "magenta"), 
       col = "black")

Output:

enter image description here

Related