How to plot multiple graphs on several different pages using ggplot

1

I have the following date frame (df):

        Subject Period Treatment   Time      Concentration
1         1      1         A      -1.000         0.000
2         1      1         A      -0.500         0.000
3         1      1         A      -0.250         0.000
4         1      1         A       0.000         0.000
5         1      1         A       0.167         1.147
6         1      1         A       0.333         4.993
7         1      1         A       0.500         4.324
8         1      1         A       0.667         6.623
9         1      1         A       0.833         4.945
10        1      1         A       1.000         3.446
11        1      1         A       1.250         2.280
12        1      1         A       1.500         2.796
13        1      1         A       1.750         1.666
14        1      1         A       2.000         1.105
15        1      2         B      -1.000         0.000
16        1      2         B      -0.500         0.000
17        1      2         B      -0.250         0.000
18        1      2         B       0.000         0.000
19        1      2         B       0.167         2.378
20        1      2         B       0.333        24.137
21        1      2         B       0.500        22.876
22        1      2         B       0.667        25.779
23        1      2         B       0.833        27.178
24        1      2         B       1.000        19.609
25        1      2         B       1.250        13.392
26        1      2         B       1.500        10.431
27        1      2         B       1.750         7.402
28        1      2         B       2.000         6.793
29        2      1         B      -1.000         0.000
30        2      1         B      -0.500         0.000
31        2      1         B      -0.250         0.000
32        2      1         B       0.000         0.000
33        2      1         B       0.167         0.097

This data frame may contain N subjects with M concentration curves per subject (which will depend on the number of treatments).

To generate the graphs of plasma concentration of each subject I am using the following code:

 p<- ggplot(Data, aes(x=Time, y=Concentration, group=Subject:Treatment:Period, shape=Treatment:Period,  color=Treatment)) + 
 geom_line() + geom_point(size=3) + facet_wrap(~ Subject,ncol = 2)+     scale_shape_manual(values=c(7,7,1,1)) + scale_colour_manual(values=c("red","darkblue"))+ xlab("Time (hr)")+
 ylab("Concentration (ng/mL)") + ggtitle("Individual Plasma Concentration - Drug X")

My problem is that when the number of subjects is large it is practically impossible to have a good visualization of all the graphics on the same page (the size reduces a lot). So I need help from "savvy" users to hone code to control the number of graphics that are printed per page. For example, if we have 10 subjects and if we want to print two graphics per page then we will have a total of 5 pages with 2 graphics per page. I've seen a lot on stackoverflow in English, but I recognize that I found none so trivial. Is it possible to do this in an elegant way with a few command lines?

Thank you very much and all your help will be very welcome.

    
asked by anonymous 14.11.2016 / 01:44

2 answers

1

I generated some random data to be able to make this chart. I think the code below will solve your problem.

library(ggplot2)

Data <- data.frame(
    Subject=factor(rep(c(1:10), each=32)),
    Period=factor(rep(c(1,2), each=16, times=20)),
    Treatment=rep(c("A","B"), each=8, times=40),
    Time=rep(seq(-1, 2, length.out=8), 40),
    Concentration=rgamma(320, shape=2)
)

# numero de sujeitos no experimento
m <- length(unique(Data$Subject))

# numero de graficos por janela
k <- 2

# loop que vai criar as 5 janelas graficas

for (j in 1:ceiling(m/k)){

    # define os id dos pacientes que serao plotados na iteracao j
    id <- factor(((j-1)*k+1):(j*k))

    # grafico que sera plotado para cada k pacientes
    g <- ggplot(subset(Data, Subject %in% id), aes(x=Time, y=Concentration,
    group=Subject:Treatment:Period, shape=Treatment:Period,  color=Treatment)) +
    facet_wrap(~ Subject, nrow=k) + geom_line() + geom_point(size=3) +
    scale_shape_manual(values=c(7,7,1,1)) + 
    scale_colour_manual(values=c("red","darkblue")) + 
    xlab("Time (hr)") + ylab("Concentration (ng/mL)") +   
    ggtitle("Individual Plasma Concentration - Drug     X")

    # plot acada uma das janelas graficas com k sujeitos
    print(g)
}

I just chose which subjects I want in my chart via the subset command. If they are few subjects, simply replace their numbers within the call of the dataset within the ggplot command.

    
14.11.2016 / 03:20
0
# número de sujeitos no experimento
m <- length(unique(Individual_Plasma_Concentration_BQL1$Subject))

# número de gráficos por janela
k <- 2

# loop que vai criar as 5 janelas gráficas

for (j in 1:ceiling(m/k)){

# define os id dos pacientes que serao plotados na iteração j
id <- factor(((j-1)*k+1):(j*k))

# gráfico que sera plotado para cada k pacientes
g <- ggplot(subset(Individual_Plasma_Concentration_BQL1, Subject %in% id),    aes(x=Time, y=Concentration, group=Subject:Treatment:Period,  shape=Treatment:Period,  color=Treatment:Period)) +
facet_wrap(~ Subject, nrow=k) + geom_line() + geom_point(size=3) +
scale_shape_manual(values=c(1,7,1,7)) +
scale_colour_manual(values=c("red","red","darkblue", "darkblue"))+ 
xlab("Time (hr)") + ylab("Concentration (ng/mL)") +   
ggtitle("Individual Plasma Concentration - Drug     X")

# plot acada uma das janelas graficas com k sujeitos
print(g)
}

See, I only changed 3 lines of your code to improve the visual aspect of the caption. Now to close with gold key, what would be the command line so we can export these 5 graphs (which can be "m" charts) to a new Microsoft Word document? Big hug and congratulations on the elegant solution ....

    
17.11.2016 / 00:59