r - 如何使用网格包添加箭头

标签 r plot r-grid

我想使用 grid 添加箭头包将突出我的图表的重要部分。我希望箭头看起来像右侧的图片。

左边是我用下面的代码创建的图,右边是我的带箭头的图(我用 Paint 添加了它)。这是我的目标。

enter image description here

library(grid)
library(lattice)
library(sandwich)

data("Investment")
Investment <- as.data.frame(Investment)

pushViewport(plotViewport(c(5, 4, 2, 2)))
pushViewport(dataViewport(Investment$Investment, 
                      Investment$GNP,
                      name="Zaleznosc Investment od GNP"))

grid.points(Investment$Investment, Investment$GNP, gp=gpar(cex=0.5))

grid.rect()
grid.xaxis()
grid.yaxis()
grid.text("Investment", y=unit(-3, "line"))
grid.text("GNP", x=unit(-3, "line"), rot=90)
popViewport()

最佳答案

您可以使用您拥有的代码,但在 popViewport() 之前添加代码以添加箭头。

grid.lines(x = unit(c(0.42, 0.74), "npc"),
          y = unit(c(0.8, 0.86), "npc"),
        gp = gpar(fill="black"),
          arrow = arrow(length = unit(0.2, "inches"), 
            ends="last", type="closed"))

Arrow

此外,跟进@alistaire 网格图形的评论有点难以使用。在基本图形中,您尝试做的事情大多很容易。
plot(GNP ~ Investment, data=Investment)
arrows(250, 2600, 380, 2750, code = 2, lwd=2)

Base Graphics

唯一不太完美的是箭头的类型。底座arrows不会给你太多控制权。如果你不介意添加一个包,shape包让您选择箭头的样式。
library(shape)
plot(GNP ~ Investment, data=Investment)
Arrows(250, 2600, 380, 2750, code = 2, 
    arr.type="triangle", arr.width=0.4)

With shape

关于r - 如何使用网格包添加箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48218643/

相关文章:

RserveException : eval failed when running R on Databricks

c++ - Windows下编译RInside例子的问题

python - 如何显示直接从Figure类创建的matplotlib图形?

r - 在 R 中的绘图网格中添加边框

r - 如何在 R 中获取系统(Windows)内存?

r - 为什么 runif() 的唯一值比 rnorm() 少?

python - matplotlib/python - 如何绘制这样的图?平均值 ± 3* 标准偏差

r - 在 R 图形的图例中包含小表格

r - 相同的代码在 R 和 R studio 中的工作方式不同

r - 如何在同一页面上绘制网格图?