Ali Anis
Ali Anis

Reputation: 63

Specific graphic in R

I need some help to draw this specific graphic in R.

I would like to know if you have already worked with this kind of graph? It's a graph that draws three points for each variable (like an arrow)

If you have an idea about it, or a library doing it, tell me about it.

Thanks you for the help.

enter image description here

That's an exemple of data enter image description here

Upvotes: 0

Views: 73

Answers (2)

user13963867
user13963867

Reputation:

Here is a solution with an "old school" R plot.

db <- data.frame(id = rep(1:2, each = 3),
                 year = rep(2018:2020, times = 2),
                 x = c(1.2, 1.6, -1.1, 3.2, 3.8, 1.9),
                 y = c(3, -2, 6, -1, 2, 3))

with(reshape(db, direction = "wide", idvar = "id", timevar = "year"),
     {
       frame()
       plot.window(xlim = range(db$x), ylim = range(db$y))
       segments(x.2018, y.2018, x.2019, y.2019)
       arrows(x.2019, y.2019, x.2020, y.2020)
       axis(1, pos = 0, col = "gray")
       axis(2, pos = 0, col = "gray")
       box()
     })

enter image description here

Upvotes: 1

Andy Eggers
Andy Eggers

Reputation: 612

Here's a dplyr/ggplot version:

library(tidyverse)
tibble(ID = c(rep(1, 3), rep(2,3)), year = rep(2018:2020, 2), x = c(1.2, 1.6, -1.1, 3.2, 3.8, 1.9), y = c(3, -2, 6, -1, 2, 3)) %>% 
  group_by(ID) %>% 
  mutate(label = ifelse(row_number() == 1, ID, "")) %>% 
  ungroup() %>% 
  ggplot(aes(x = x, y = y, label = label, group = ID)) + 
  geom_path(arrow = arrow()) + 
  geom_text(nudge_x = .2, nudge_y = 0)

Created on 2021-07-19 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions