我想使用grid包添加箭头,这将突出显示我的图表的重要部分.我希望箭头在右侧的图片上看起来像.
在左边的部分是我的图表由下面的代码创建,右边的部分是我的图表与箭头(我已经使用Paint添加它).这是我的目标.
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()
Run Code Online (Sandbox Code Playgroud)
您可以使用您拥有的代码,但在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"))
Run Code Online (Sandbox Code Playgroud)
此外,跟进@alistaire 网格图形的评论有点难以使用。在基本图形中,您尝试做的事情大多很容易。
plot(GNP ~ Investment, data=Investment)
arrows(250, 2600, 380, 2750, code = 2, lwd=2)
Run Code Online (Sandbox Code Playgroud)
唯一不太完美的是箭头的类型。基地arrows并没有给你太多的控制权。如果您不介意添加包,该shape包可以让您选择箭头的样式。
library(shape)
plot(GNP ~ Investment, data=Investment)
Arrows(250, 2600, 380, 2750, code = 2,
arr.type="triangle", arr.width=0.4)
Run Code Online (Sandbox Code Playgroud)