r - 在 R 中指定网格箭头的 gpar 设置

标签 r ggplot2

我正在开发一个按比例大小的箭头图,并且有一个大小、方向和箭头大小的工作流程,但箭头有几个问题:1.它们有圆角线连接,2.即使在 type='close' 时,它们也不会关闭(请参见右下角)。

require(ggplot2)
require(grid)
d = data.frame(x = 1:10, y = 0, size = 1:10)

ggplot(d, aes(x, y, size = size)) +
  geom_segment(aes(xend = x, yend = y + size), 
               arrow = arrow(length = unit(d$size, "mm"), type='closed')) +
  scale_size(range = c(2, 4))

enter image description here

箭头基于grid graphics ,但我不知道如何指定设置。 get.gpar() 产量:

$lineend
[1] "round"

$linejoin
[1] "round"

但是 gpar(linejoin = 'mitre', lineend = 'butt') 不会改变这一点。有什么办法可以改变这些设置吗?提前致谢。

<小时/>

编辑

添加了包含 grid.segments 箭头的绘图图像:

enter image description here

最佳答案

找到了一个解决方案(感谢 @baptiste 的指点)。如果您使用geom_segment source code在 github 中进行以下更改,您最终将得到可爱的尖箭头:

1 require(proto)

2添加ggplot:::在 GeomSegment 的第一行(当前为第 51 行)如下:

GeomSegment <- proto(ggplot:::Geom, {

3 添加参数 linejoin = 'mitre'gparsegmentsGrob (第 23 行):

lwd=size * .pt, lty=linetype, lineend = lineend, linejoin = 'mitre'),

运行这两个函数,您最终会得到:

enter image description here

关于r - 在 R 中指定网格箭头的 gpar 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658071/

相关文章:

java - R Selenium - 单击表单提交按钮

r - 将字符串作为 dplyr 动词中的参数传递

r - 在ggplot2中显示频率和barplot

r - 如何确保文本标题位于多边形对象内部?

r - 数据集上的cbind无效下标类型 'list'

r - 仅识别非重复行

r - 使用 Shiny 和 ggplot2 在单个图形上绘制多条线

r - 使用 ggplot2 facet_wrap 在地理点上绘制图形

r - 按行自动回归

python - 创建时间范围时间序列螺旋图