Question

I am trying to get a correct legend for a series of points in a plot, which look like:

plot(c(3,1),pch=21,bg="white",col="black")
points(c(2,1),pch=21,bg="black",col="black")
points(c(1,1),pch=21,bg="dark grey",col="black")

I thought the legend code would be:

legend("topright",legend=c('Point3','Point2','Point1'),pch=c(21,21,21),
       bg=c('white','black','dark grey'),col=c('black','black','black'),bty='n')

But apparently I am wrong, because I only get three white points with black boarder. Why isn't this working and what is the correct code?

Was it helpful?

Solution

If you read help("legend") you find out that bg specifies the background of the legend. You need to use pt.bg:

legend("topright", legend=c('Point3','Point2','Point1'), pch=c(21,21,21),
     pt.bg=c('white','black','dark grey'), col=c('black','black','black'), bty='n')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top