简体   繁体   中英

Force legend to fill pch selection in R plot

The question/problem I am facing right now is that when doing the plot function, I get the pch that are equal to 25 filled with color black by specifying the bg="black" parameter on function plot, but when I add the legend, I can't find any sort of combination of parameters that also fills the pch=25 to be black, which correspond to an inverted triangle, filled with color if specified. There is no actual inverted dark triangle in R, like 17 upside down (25 filled seems to be the only option).

Any help is really appreciated.

Data:

x,y,pch
1,4,17
1,1,25
1,2,25
1,3,25
2,6,17
2,5,25
2,7,25
2,8,25
3,6,17
3,9,17
3,5,25
3,7,25
3,8,25
4,6,17
4,9,17
4,5,25
4,7,25
4,8,25
5,6,17
5,9,17
5,5,25
5,7,25
5,8,25
6,9,17
6,8,25
7,9,17
7,8,25
8,10,17
8,12,17
8,9,17
8,11,25
9,10,17
9,12,17
9,9,17
9,11,25
10,10,2
10,12,17
10,11,6
10,13,25
11,10,17
11,12,17
11,11,25
11,13,25

Code:

plot(x=data[,1],y=data[,2],pch=data[,3],bg="black",col="black")
legend("bottomright", legend = c("+ A", "+ a", "- B", "- b"), 
 col = "black",  fill, pch = c(17,2,25,6), text.col = "black", 
 bg= "black", bty = "n", horiz = F )

Figure with inverted triangle unfilled in legend (-B) : 在此处输入图片说明

To expand on rawr's answer: When used in legend() , the argument bg= will set the background color for the entire legend (rather than fill for pch 21:27 ), if the legend is surrounded by a box. In your case, you suppressed the legend box so bg= "black" was simply ignored. You want to use pt.bg = c(NA,NA,"black",NA) . The NA's are used as place holders corresponding to the non-fill pch's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM