Noosh
Noosh

Reputation: 687

R: Adding arrows to grids of layout

I'm using layout to plot multiple things and I'd like to add arrow between some of these plots. I tried grid.curve and grid.lines() but no luck so far. Here's an example of what I'd like to do:

mat <- cbind(matrix(c(1:3,0,4:5,0,6,7),3,3,byrow=T), 8:10)
m<-layout(mat)
layout.show(m)
cars <- c(1, 3, 6, 4, 9)
trucks <- c(2, 5, 4, 5, 12)
plot(cars, type="o", col="blue")
plot(trucks, type="o", pch=22, lty=2, col="red")
plot(cars, type="o", col="blue", ylim=c(0,12))
lines(trucks, type="o", pch=22, lty=2, col="red")
barplot(cars)
barplot(trucks)
hist(cars)
pie(cars)
library(gplots)
textplot(mat)

I'd like to add an arrow from plot (layout.pos.col=2, layout.pos.row=1) to plot (layout.pos.col=2, layout.pos.row=2) and one from (layout.pos.col=2, layout.pos.row=2) to (layout.pos.col=3, layout.pos.row=3). Is there any simple way to add these?

Thanks in advance!

Upvotes: 2

Views: 715

Answers (2)

cuttlefish44
cuttlefish44

Reputation: 6786

I tried to solve it in an elemental fashion.

:
textplot(mat)

par.old <- par(no.readonly=T)            # preserve old par
par(new=T, mfrow=c(1,1), mar=c(0,0,0,0))
plot(0,0, type="n", axes=F, ann=F)       # make empty plot

a <- locator(4)                          # locator() returns xy coordinates at a click point
# click on the plot four times (first arrow's start point, end point, second arrow's …)

arrows(a$x[1], a$y[1], a$x[1], a$y[2], length=0.1)  # use same x ( a$x[2] is unnecessary ).
arrows(a$x[3], a$y[3], a$x[4], a$y[4], length=0.1)

par(par.old)

Upvotes: 0

AkselA
AkselA

Reputation: 8846

you could give arrows a go, if you don't require anything super fancy. Two simple black straight arrows:

x <- -2
y <- -0.1
arrows(x0=x, y0=y, x1=x, y1=y-0.2, xpd=NA, length=0.05)

x <- -1.55
y <- -1.3
arrows(x0=x, y0=y, x1=x+0.5, y1=y-0.6, xpd=NA, length=0.05)

I figured out the coordinates entirely throught trial and error, there might be smarter ways. xpd=NA is important to make drawing between plotting areas possible.

enter image description here

Upvotes: 3

Related Questions