我想使用 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()
最佳答案
您可以使用您拥有的代码,但在 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"))
此外,跟进@alistaire 网格图形的评论有点难以使用。在基本图形中,您尝试做的事情大多很容易。
plot(GNP ~ Investment, data=Investment)
arrows(250, 2600, 380, 2750, code = 2, lwd=2)
唯一不太完美的是箭头的类型。底座
arrows
不会给你太多控制权。如果你不介意添加一个包,shape
包让您选择箭头的样式。library(shape)
plot(GNP ~ Investment, data=Investment)
Arrows(250, 2600, 380, 2750, code = 2,
arr.type="triangle", arr.width=0.4)
关于r - 如何使用网格包添加箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48218643/