0
library(GGally)
f=data.frame(x=sin(2*pi*(1:100)/100), y=cos(2*pi*(1:100)/100))
ggpairs(f)

A time series can be plot like the above with the time axis collapsed (lower left). But it loses the directional information on this graph.

Is there an easy way to make such a pair graph with arrows added appropriately so that it can be seen where the flow goes?

Good solutions in lattice or other graphic systems are also welcome.

EDIT: I need the solution in the context of a pair plot instead of just a single scatter plot.

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • instead of arrows you could encode the direction with color or line width (see e.g. Claus Wilkes chapter on connected scatter plots here https://clauswilke.com/dataviz/time-series.html#fig:house-price-path) – tjebo Aug 07 '21 at 20:50
  • Is there any code available? I don't see it in the book. – user1424739 Aug 07 '21 at 21:19
  • Check the book's preface and the author's comment: _Importantly, because this is not an R book, I do not discuss code or programming techniques anywhere in this book. I want you to focus on the concepts and the figures, not on the code. If you are curious how any of the figures were made, you can check out the book’s source code at its GitHub repository, https://github.com/clauswilke/dataviz._ – tjebo Aug 08 '21 at 13:41

3 Answers3

1

As for other graphics systems, here is a short answer in base graphics (still R):

f <- data.frame(x=sin(2*pi*seq(1,100,8)/100), y=cos(2*pi*seq(1,100,8)/100))
with(f, plot(x, y))                                            
for(i in 1:(nrow(f)-1))
    with(f, arrows(x0 = x[i], y0 = y[i], x1 = x[i+1], y1 = y[i+1]))

enter image description here

Bernhard
  • 4,272
  • 1
  • 13
  • 23
1

If a single arrow head satisfies your needs:

f <- data.frame(x=sin(2*pi*seq(1,100,10)/100), y=cos(2*pi*seq(1,100,10)/100))

library(ggplot2)
ggplot(f, aes(x = x, y = y)) + 
  geom_point() +
  geom_path(arrow = arrow()))

enter image description here

Bernhard
  • 4,272
  • 1
  • 13
  • 23
  • I will need to make it work in the context of a pair plot instead of just a regular scatter plot. – user1424739 Aug 07 '21 at 21:38
  • Depending on the circumstances you could make your own pair plot with something like `ggpubr::ggarrange` or `cowplot::plot_grid`. https://wilkelab.org/cowplot/articles/plot_grid.html – Bernhard Aug 07 '21 at 22:06
0

You can use the group aesthetic to pair the arrows:

f <- data.frame(x=sin(2*pi*seq(1,100,10)/100), y=cos(2*pi*seq(1,100,10)/100), g=rep(1:5,each=2))
library(ggplot2)
ggplot(f,aes(x,y,group=g)) +
  geom_point() +
  geom_path(arrow = arrow())

geom_path with group aesthetic