matrix.R 51.9 KB
Newer Older
Heraut Louis's avatar
Heraut Louis committed
                        mat = mat +
Heraut Louis's avatar
Heraut Louis committed
                            # Writes the unit of the averaged variable
                            annotate('text',
                                     x=Xm_mean[i], y=max(Y) + 0.63,
Heraut Louis's avatar
Heraut Louis committed
                                     label=unit_mean,
Heraut Louis's avatar
Heraut Louis committed
                                     hjust=0.5, vjust=0.5, 
Heraut Louis's avatar
Heraut Louis committed
                                     size=2, color='grey40') +
Heraut Louis's avatar
Heraut Louis committed
                            # Writes the type of the averaged variable
                            annotate('text',
                                     x=Xm_mean[i], y=max(Y) + 0.9,
                                     label=expr(bar(!!var)),
                                     hjust=0.5, vjust=0.5, 
Heraut Louis's avatar
Heraut Louis committed
                                     size=3.25, color='grey20')
Heraut Louis's avatar
Heraut Louis committed

                        # If this is not the first period
                        if (j > 1) {
                            mat = mat +
                                # Writes the unit of the breaking variable
                                annotate('text', x=Xr_mean[i],
                                         y=max(Y) + 0.63,
Heraut Louis's avatar
Heraut Louis committed
                                         label=unit_break,
Heraut Louis's avatar
Heraut Louis committed
                                         hjust=0.5, vjust=0.5, 
                                         size=2, color='grey40') +
                                # Writes the type of the breaking variable
                                annotate('text', x=Xr_mean[i],
                                         y=max(Y) + 0.9,
                                         label=paste("d", var, sep=''),
                                         hjust=0.5, vjust=0.5,
                                         size=3.25, color='grey20')
                        }
Heraut Louis's avatar
Heraut Louis committed
                    }

Heraut Louis's avatar
Heraut Louis committed
                    # For all the station on the page
                    for (k in 1:nsubCode) {
                        # Gets the code
                        code = subCode[k]
                        # Extracts label for the period dates
                        label = Periods_mean_per[Code_mean_per == code][1]
                        # Gets the start and end of the period
                        # for the station
                        periodStart = substr(label, 1, 4)
                        periodEnd = substr(label, 14, 17)

                        mat = mat +
                            # # Writes the starting value
                            annotate('text', x=Xc_mean, y=k + 0.13,
                                     label=bquote(bold(.(periodStart))),
                                     hjust=0.5, vjust=0.5, 
                                     size=3, color='grey40') + 
                            # Writes the ending value
                            annotate('text', x=Xc_mean, y=k - 0.13,
                                     label=bquote(bold(.(periodEnd))),
                                     hjust=0.5, vjust=0.5, 
                                     size=3, color='grey40')
                    }            
                }
                
                ### Code ###
                # For all the station
Heraut Louis's avatar
Heraut Louis committed
                for (k in 1:nsubCode) {
Heraut Louis's avatar
Heraut Louis committed
                    # Gets the code
Heraut Louis's avatar
Heraut Louis committed
                    code = subCode[k]
Heraut Louis's avatar
Heraut Louis committed
                    # Gets the name of the station
                    name = df_meta[df_meta$code == code,]$nom
                    # Fixes a limit for the max number
                    # of characters available
                    ncharMax = 38
                    # If the number of character of the name is greater
                    # than the limit
                    if (nchar(name) > ncharMax) {
                        # Cuts the name and add '...'
                        name = paste(substr(name, 1, ncharMax),
                                     '...', sep='')
                    }
Heraut Louis's avatar
Heraut Louis committed

                    mat = mat +
Heraut Louis's avatar
Heraut Louis committed
                        # Writes the code of the station
                        annotate('text', x=0.3, y=k + 0.14,
                                 label=bquote(bold(.(code))),
                                 hjust=1, vjust=0.5, 
                                 size=3.5, color="#00A3A8") +
                        # Writes the name of the station
                        annotate('text', x=0.3, y=k - 0.14,
                                 label=name,
                                 hjust=1, vjust=0.5, 
                                 size=3.5, color="#00A3A8")
Heraut Louis's avatar
Heraut Louis committed
                }

Heraut Louis's avatar
Heraut Louis committed
                ### Environment ###
Heraut Louis's avatar
Heraut Louis committed
                mat = mat +
Heraut Louis's avatar
Heraut Louis committed
                    # Fixed coordinate system
                    coord_fixed() +
                    # X axis
                    scale_x_continuous(limits=c(1 - rel(6), 
                                                width + rel(0.5)),
                                       expand=c(0, 0)) + 
                    # Y axis
                    scale_y_continuous(limits=c(1 - rel(0.5), 
                                                height + rel(2)),
                                       expand=c(0, 0))
                
                # Paper format in A3 if needed
                if (A3) {
                    width = 42
                    height = 29.7
                    dpi = 300
                    # Otherwise in A4
                } else {
                    width = 29.7
                    height = 21
                    dpi = 100
                }
Heraut Louis's avatar
Heraut Louis committed

Heraut Louis's avatar
Heraut Louis committed
                # If there is a foot note
                if (foot_note) {
                    foot = foot_panel('tableau récapitulatif',
                                      n_page, N_page,
                                      resources_path,
                                      AEAGlogo_file, INRAElogo_file,
                                      FRlogo_file, foot_height)
                    
                    # Stores the map, the title and the colorbar in a list
                    P = list(mat, foot)
                    LM = matrix(c(1,
                                  2),
                                nrow=2, byrow=TRUE)
                } else {
                    foot_height = 0
                    # Stores the map, the title and the colorbar in a list
                    P = list(mat)
                    LM = matrix(c(1),
                                nrow=1, byrow=TRUE)
                }
                id_foot = 2
                
                LMcol = ncol(LM)
                LMrow = nrow(LM)
                
                LM = rbind(rep(99, times=LMcol),
                           LM, rep(99, times=LMcol))
                LMrow = nrow(LM)
                LM = cbind(rep(99, times=LMrow),
                           LM, rep(99, times=LMrow))
                LMcol = ncol(LM)
                
                margin_height = 0.5

                row_height = (height - 2*margin_height - foot_height) / (LMrow - 3)

                Hcut = LM[, 2]
                heightLM = rep(row_height, times=LMrow)
                heightLM[Hcut == id_foot] = foot_height
                heightLM[Hcut == 99] = margin_height

                col_width = (width - 2*margin_height) / (LMcol - 2)
                
                Wcut = LM[(nrow(LM)-1),]
                widthLM = rep(col_width, times=LMcol)
                widthLM[Wcut == 99] = margin_height

                # Arranges the graphical object
                plot = grid.arrange(grobs=P, layout_matrix=LM,
                                    heights=heightLM, widths=widthLM)

Heraut Louis's avatar
Heraut Louis committed
                # Saving
Heraut Louis's avatar
Heraut Louis committed
                ggsave(plot=plot, 
Heraut Louis's avatar
Heraut Louis committed
                       path=outdirTmp,
                       filename=paste(outnameTmp,
                                      '_', type,
                                      '_', fL,
                                      iMat, sep=''),
                       device='pdf',
                       width=width, height=height,
                       units='cm', dpi=dpi)            
Heraut Louis's avatar
Heraut Louis committed
            }
Heraut Louis's avatar
Heraut Louis committed
        }           
    }
Heraut Louis's avatar
Heraut Louis committed
}