简体   繁体   English

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. 我的解决方案是使符号更大,更可见。

Edit 1: it's for plot {graphics} of the R program. 编辑1:用于R程序的图{graphics}。

Edit 2: the code per popular request. 编辑2:每个热门请求的代码。

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)

Edit 3: This is solved for plot(type='b') but somehow not for legend. 编辑3:这是为plot(type ='b')解决的,但对于图例来说不然。

Thanks for reading! 谢谢阅读!

The only thing I can come up with is to manually finagle the dash lengths until they end up looking the way you want them. 我唯一能想到的是手动设置虚线长度,直到它们最终看起来像您想要的样子。 For instance, this: 例如,这:

> plot(1,1)
> legend(c("A", "B"), col = 1:2, x = 1, y = .8, lty="99", pch=1:2)

produces the image below. 产生下面的图像。

The lty parameter allows you to specify the lengths of lines and dashes as hex characters. lty参数允许您将线条和破折号的长度指定为十六进制字符。 In this case, it's saying to create a line of length 9 then create a space of length 9 then repeat. 在这种情况下,它说创建线路长度的9然后创建长度的空间9然后重复。 It looks like 9 is about the best fit to space around a normal pch symbol. 看起来9与正常pch符号周围的空间最合适。

Note that you'd probably need to adjust this depending on the size of the image, symbol, etc. My advice ultimately would be to export the image from R and touch up the image to meet your needs in graphic editing software. 请注意,您可能需要根据图像,符号等的大小进行调整。我的建议最终是从R导出图像并修饰图像以满足图形编辑软件中的需求。

带有手动破折号长度的图形

You could also use the filled points offered by R (pch=21:25) and specify the fill color using pc.bg which gets passed to the points call when creating a legend. 您还可以使用R提供的填充点(pch = 21:25),并使用pc.bg指定填充颜色,该颜色会在创建图例时传递给points调用。

plot(1,1)
legend(c("A", "B"), col = 1:2, x = 1, y = .8, lty=1, pt.bg=1:2, pch=21:22)

generates the following: 生成以下内容:

填充点图

Going with the suggestion by @JeffAllen, here is a way to get what I think you might want. 遵循@JeffAllen的建议,这是一种获取我认为您可能想要的方法。 It requires modifying the legend() function to return the position of the points (these are given by x1 and y1 in body(legend)[[46]] ). 它需要修改legend()函数以返回点的位置(它们由body(legend)[[46]]x1y1给出)。

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)))
)

Make a plot: 作图:

plot(-100:100, -100:100, type = "b")

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. 绘制图例时,在线条上绘制白色圆圈( pch = 21pt.bg = 'white' ),然后将legend2()隐式返回的值分配给对象。 Note also the changes to pt.lwd and pt.cex . 还要注意对pt.lwdpt.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)

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提供x和y值。

points(myLegend$points$x, myLegend$points$y, pch = c('s','c','u'), cex = 1.5)

And this should get you something like: 这应该为您提供以下信息:

样例

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

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