在R中为网格箭头指定gpar设置 [英] Specifying gpar settings for grid arrows in R

查看:828
本文介绍了在R中为网格箭头指定gpar设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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 size),mm),type ='closed'))+
scale_size(range = c(2,4))



箭头基于,并且做出以下改动,你将以可爱的尖头箭头结束:

1 require(proto)



2添加 ggplot ::: 在GeomSegment的第一行(当前是第51行),如下所示:



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



<3>在 segmentsGrob 中添加参数 linejoin ='miter' gpar (第23行):

lwd = size * .pt,lty = linetype,lineend = lineend,linejoin ='miter'),
$ b $ p
$ b

运行这两个函数,您将得到:


I'm developing a plot of proportionally sized arrows, and have a workflow for size, direction and arrow-head size, but there are a couple of issues with the arrowheads: 1. they have rounded line-join and 2. they don't close (see bottom right corner) even when type='closed'.

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

Arrows are based on grid graphics, but I cannot figure out how to specify the setting. get.gpar() yields:

$lineend
[1] "round"

$linejoin
[1] "round"

but gpar(linejoin = 'mitre', lineend = 'butt') does not change this. Is there any way to change these settings? Thanks in advance.


Edit

Plot image including grid.segments arrow added:

解决方案

Found a solution (credit to @baptiste for the pointer). If you use the geom_segment source code in github and make the following alterations you will end up with lovely pointy arrowheads:

1 require(proto)

2 add ggplot::: in first line of GeomSegment (currently line 51) as follows:

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

3 add argument linejoin = 'mitre' to gpar in segmentsGrob (line 23):

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

Run both functions and you'll end up with:

这篇关于在R中为网格箭头指定gpar设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆