R图例问题,点的符号被线遮盖 [英] R legend issue, symbols of points are masked by lines
问题描述
有没有一种方法可以画线,使它们从点的一侧开始,或者允许符号位于前景中?
Is there a way to draw the lines in such a way that they would start on the side of the points, or allow the symbols to be in foreground?
我的解决方案是使符号更大且更可见.
My solution was to make the symbols bigger and more visible.
用于R程序的图形{graphics}.
Edit 1: it's for plot {graphics} of the R program.
每个热门请求的代码.
legend(2,.4,bty='n', c('sugar','citrus','none'), pch=c('s','c','u'), pt.bg='white',lty= c(1,2,3), lwd=1.5, title="Condition",pt.cex=c(1.5),cex=1.5)
这对于plot(type ='b')是解决的,但对于图例来说不然.
Edit 3: This is solved for plot(type='b') but somehow not for legend.
感谢阅读!
推荐答案
按照@JeffAllen的建议,这是一种获取我认为您想要的方法.它需要修改legend()
函数以返回点的位置(这些点由body(legend)[[46]]
中的x1
和y1
给出).
Going with the suggestion by @JeffAllen, here is a way to get what I think you might want. It requires modifying the legend()
function to return the position of the points (these are given by x1
and y1
in body(legend)[[46]]
).
legend2 <- legend
body(legend2)[[49]] <- quote(
invisible(list(rect = list(w = w, h = h, left = left, top = top),
text = list(x = xt, y = yt), points = list(x = x1, y = y1)))
)
作图:
plot(-100:100, -100:100, type = "b")
在绘制图例时,在线条上绘制白色圆圈(pch = 21
和pt.bg = 'white'
),然后将legend2()
不可见返回的值分配给对象.另请注意对pt.lwd
和pt.cex
的更改.
While drawing the legend, draw white circles (pch = 21
with pt.bg = 'white'
) over the lines, and assign the values invisibly returned by legend2()
to an object. Note also the changes to pt.lwd
and pt.cex
.
myLegend <- legend2(1, .8, bty = 'n', c('sugar','citrus','none'), pch = 21,
pt.bg = 'white', pt.lwd = 0, lty = c(1, 2, 3), lwd = 1.5, title = "Condition",
pt.cex = c(1.8), cex = 1.5)
最后,使用points()
绘制要在图例中使用的字符,并提供对象myLegend
的x和y值.
Finally, draw the characters you'd like to use in the legend using points()
, supplying the x and y values from the object myLegend
.
points(myLegend$points$x, myLegend$points$y, pch = c('s','c','u'), cex = 1.5)
这应该使您像:
这篇关于R图例问题,点的符号被线遮盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!