简体   繁体   English

图例在R图中落后

[英]Legend goes behind the lines in R plot

I am generating a plot using the following sample code 我正在使用以下示例代码生成图

x=seq(1,10, length=100)       
y=seq(-5,5,length=100)    
pdf("plot.pdf")   
plot(y~x, type="n", xlab="", ylab="")     
for(i in 1:4){   
y=rnorm(100, mean=i, sd=i)   
lines(x,y, type="l",lty=i)     
}   
legend("topright", legend=c("a", "b", "c"), lty=c(1,2,3,4), pch=c(1,2,3,4))   
dev.off()   

and when I save the plot in pdf, then the legend goes behind the drawn lines. 当我将图以pdf格式保存时,图例就会出现在绘制的线条后面。 While I want that the graphs should looks like the following while saving in the form of pdf. 虽然我希望这些图以pdf格式保存时应如下所示。 在此处输入图片说明

As you're seeing, the background color for the legend defaults to "transparent" . 如您所见,图例的背景色默认为"transparent" To set it instead to an opaque "white" , use its bg parameter, like this: 要将其设置为不透明的"white" ,请使用其bg参数,如下所示:

legend("topright", legend=c("a", "b", "c"), lty=c(1,2,3,4), pch=c(1,2,3,4),
       bg = "white")

在此处输入图片说明

It's not on top, it's transparent. 它不在顶部,它是透明的。 set the argument bg = 'white' in the legend command. 在图例命令中设置参数bg = 'white'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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