I am trying to calculate two things in R, Relative & Absolute Change, & plot 2-y axis scatter plots. I am still seeking inputs on creating a 2-y axis plot for this type of data.
set.seed(123)
df=expand.grid(PatientID=1:3,time=1:3, Group=1:2)
dat <- data.table(df,Outcome=as.integer(runif(9)*100))
Data Format df #sample
PatientID time Outcome Group
1 1 87 1
1 2 32 1
1 3 76 2
2 1 21 2
2 2 23 3
2 3 23 3
## Cont until 200 PatientID or volunteers and there are many outcome measure columns (33:290)
PatientID, time, Outcome, Group denote volunteers' identification number, time of visiting a hospital, outcome measure of interest and Group (whether they belong to a condition A or B) respectively. Data includes 3 visits by participants and two groups.
- Relative Change(%), i.e. expresses the absolute change as a percentage of the outcome from baseline time point, for Group 1 & 2.
[(F - B )/ B]*100, here B and F are baseline and follow up values of a outcome measure
Absolute Change, i.e. F-B
2-y axes scatter plots:
The prime purpose of this plot is to look at the changes in outcome measures with respect to baseline (time=1), and also determine if there are group differences. It is prudent to include respective relative/absolute change values in the plot as y1 and y2.
I had made several scatterplots in ggplot2 and ggvis to view the trends, but I did not find a direct option to calculate (& plot) relative & absolute change through the ggplot2 & ggvis packages. I really recommend using them for novice users, like myself. In addition, I am also planning to incorporate relative & absolute change values in one scatterplot itself for one outcome measure, i.e. 2-y axes plots.
Let me know if you require some more clarifications. Thanks, and looking forward!
Answers for 1 & 2 Ques #thought it might help others
This is how I finally did it:
library(dplyr)
dft1= filter(df, df$time==1)
dft2= filter(df, df$time==2)
dft3= filter(df, df$time==3)
To calculate absolute change from second to first time point & third to first time point:
abs1=dft2[33:290] - dft1[33:290]
abs2=dft3[33:290] - dft1[33:290]
To calculate relative change from second to first time point & third to first time point:
rel1=abs1/dft1[33:290]*100
rel2=abs2/dft1[33:290]*100
I will put absolute change and relative change on different y-axis axes. This link was handy to get me started: (How can I plot with 2 different y-axes?).
Nice resource for learning R: https://stackoverflow.com/tags/r/info