0

I'm trying to make a scatterplot on two time-series variables. However, I want to "lag" one of them, meaning that I'd like to plot (x, y) as (x on t, y on t+1). I'm not sure whether I'm making myself clear, but whatever question please do let me know.

Here's the head of my data:

# A tibble: 6 × 6
  `Country Name` `Country Code` Year  CrecimientoPBI Inflación CrecimientoBaseMonetaria
  <chr>          <chr>          <chr>          <dbl>     <dbl>                    <dbl>
1 Estados Unidos USA            1961             2.3      1.07                     8.19
2 Estados Unidos USA            1962             6.1      1.2                      8.93
3 Estados Unidos USA            1963             4.4      1.24                     9.32
4 Estados Unidos USA            1964             5.8      1.28                     9.03
5 Estados Unidos USA            1965             6.4      1.59                     8.62
6 Estados Unidos USA            1966             6.5      3.02                     4.72

--with CrecimientoBaseMonetaria as my explanatory variable (x), and Inflación as my explained variable (y).

Gaspar
  • 49
  • 1
  • 8
  • 1
    you could make a lagged y column: `data %>% mutate(lag_y = lag(Inflación)) %>% ggplot(aes(CrecimientoBaseMonetaria, lag_y )) + geom_point()` – AndS. Nov 17 '21 at 21:01
  • With `dplyr` loaded, you can use `lag()` directly inside `ggplot`, something like `ggplot(aes(CrecimientoBaseMonetaria, lag(Inflación, default = NA)) + geom_point()` – Gregor Thomas Nov 17 '21 at 21:02
  • It's not clear whether your data is grouped or not, but the marked duplicate will handle the grouped case, and if you ignore the `group_by()` it should work for the non-grouped case as well. – Gregor Thomas Nov 17 '21 at 21:04
  • It worked! That was easy --was not aware of that function, but good to know now. Thanks to both of you! – Gaspar Nov 17 '21 at 22:09

0 Answers0