How to add arrow using grid

I want to add an arrow using a package gridthat will highlight an important part of my chart. I want the arrow to look like the image on the right side.

On the left side is my chart, created by the code below, and on the right is my chart with an arrow (I added it with Paint). It is my goal.

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()
+4
source share
2 answers

You can use the code that you have, but up to popViewport()add the code to add an arrow.

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

Also, tracking @alistaire grid graphics is a little tricky to track. What you are trying to do is basically simple in the base schedule.

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

+3

, @alistaire ggplot2 grid .

, ggplot2, annotate() .

data(Investment, package = "sandwich")

library(ggplot2)
ggplot(as.data.frame(Investment)) +
  aes(Investment, GNP) +
  geom_point(shape = 1L) +
  theme_bw() +
  annotate("segment", x = 250, xend = 380, y = 2600, yend = 2800, 
           arrow = arrow(length = unit(0.2, "inches"), ends = "last", type = "closed"))

enter image description here

, ggplot2 arrow() grid.

+1

Source: https://habr.com/ru/post/1692075/


All Articles