0

Let's assume that I have the following dataset:

a   b   a_lag  b_lag   
10  30  0       0      
12  25  1       0      
20  55  2       0      
16  37  1       1     
24  60  2       1      
19  50  2       2       

I wish to plot both A an B (different color for each) against the respective axes A_lags and B_lags on the same figure. I found this quite simple with ggplot if we had only one x-axis(lag in the above example), common to both A and B. However, there are different lags which respectively for A(A_lags) and B(B_lags). But the lags are very similar with integers 0,1, and 2. Can anyone show me how to plot A and B against a "common lag" - 0, 1 and 2 on Rstudio?

Axeman
  • 32,068
  • 8
  • 81
  • 94

1 Answers1

1

The first step is to get your data into Tidy Data format so that it works well with plotting with ggplot2. It seems in the end, you are just looking to plot A and B values (in the a and b columns, respectively) against a single "common lag" axis. As such, you can realize that your dataset is actually composed of a dataframe where you have the respective x, and y columns named differently for your two series, "A" and "B". It's going to be easiest to separate the two datasets out, relabel the columns, and then recombine again. I'll do this using dplyr and tidyr, but there are a few packages that do similar functions.

(Note: assuming your dataframe = df).

library(dplyr)
library(tidyr)
library(ggplot2)

# separate out the data
df_a <- df %>% select(a, a_lag)
df_b <- df %>% select(b, b_lag)

# rename the columns to correspond to y and x axis
names(df_a) <- c('value', 'lag')
names(df_b) <- c('value', 'lag')

# include a new column to identify the series for the data
df_a$series <- 'A'
df_b$series <- 'B'

# combine and plot
new_df <- rbind(df_a, df_b)
ggplot(new_df,aes(x=lag, y=value, color=series)) + geom_point(size=2) + theme_bw()

enter image description here

I hope it's pretty obvious what's going on with the code via the comments. OP was not specific on the type of plot wanted, but you should be able to create any plot from the new_df dataset.

chemdork123
  • 12,369
  • 2
  • 16
  • 32
  • 1
    Thank you very much. Apologies for not being clear about the kind of plot I was asking for. But this is exactly what I was looking for. Before having a look at your answer, I felt I had to organize it in as either a factor or use group by. But your answer is more intuitive. Thank you so much. – Mr leconomiste Aug 12 '20 at 19:09