Plot multiple graphs in rstudio using ggplot2

Plot multiple graphs in rstudio using ggplot2


In this course, you will learn how to create graphs using ggplot2 to meet the publication standards of a scientific journal. This involves following best practices in data visualization to effectively communicate the results of your research.
To create multiple graphs using ggplot2 in R, you can use the functions facet_wrap() or facet_grid() to divide your data based on one or more variables. In this example, we have a dataset of precipitation with two variables x (Rain_IMERG) and y (Rain_Gauge), and a grouping variable (Stations). In this course, we will use facet_wrap() to create a faceted set of graphs.
You can further customize the appearance of the graphs by adding additional layers or modifying the theme within the ggplot() function. In the following example, we will use data from measured (Stations) and satellite-estimated (IMERG) precipitation to create visually appealing faceted graphs. The steps we have followed are listed below.

The use of this article and the video (link below) is highly recommended for mastering the creation of multiple graphs in RStudio using ggplot2.

1. Run the packages already installed

We used: library(ggplot2), library(openxlsx): to open . xlsx files, library(viridis): scale_color/fill, library(ggpubr): to add stat_cor, library(tidyr): to gather data.

2. Import data from Excel file

    sheet = "Rain_Gauge",
    startRow = 1,
    colNames = TRUE,
    detectDates = TRUE)

3. Gather data and create data frame (df) (Watch the video)

> Rain gauge data
Gather_Rain_gauge <- Data_Gauge %>% gather(month, p, Jan:Dec)
head(Gather_Rain_gauge, 24)
write.csv(Gather_Rain_gauge, "D:/RStudio/Multiple_Graphs/Rain_Gauge.csv", row.names=FALSE)
> IMERG data
Gather_Rain_IMERG <- Data_IMERG %>% gather(month, p, Jan:Dec)
head(Gather_Rain_IMERG, 24)
> Import Data frame (df)
    sheet = "Data",
    startRow = 1,
    colNames = TRUE,
    detectDates = TRUE)
head(Data, 24)

4. Plot multiple graphs using ggplot2 and custom the theme (Watch the video)

ggplot(Data, aes(x=P_IMERG, y=P_Gauge)) +
    geom_point(mapping = aes(color = P_IMERG), size=1.3, shape=16) +
    scale_color_viridis(direction = -1, option = "turbo",)+
    facet_wrap(~ Stations, ncol=3)+
    labs(color= "Pmm", y="GPM IMERG (mm)", x="Rain Gauge (mm)")+
    scale_x_continuous(breaks=seq(0, 350, 100))+
    scale_y_continuous(breaks=seq(0, 350, 100))+
    coord_cartesian(ylim=c(0,350), xlim= c(0,350))+
        axis.title.x = element_text(face="bold", size=8, hjust=0.5),
        axis.title.y = element_text(face="bold", size=8, hjust=0.5),
        axis.text.x = element_text(face = "bold", size = 7.5),
        axis.text.y = element_text(face="bold",size =7.5,angle=90,hjust=0.5),
        axis.line = element_line(size = 0.4, linetype = "solid"),
        legend.position = "none",
        strip.text.x = element_text(size =8, face="bold"))+
        stat_cor(aes(label= paste(..r.label..)),label.x =3, label.y =340, size=2.5)+
        stat_cor(aes(label= paste(..rr.label..)),label.x =3, label.y =300, size=2.5)+
        annotate("segment",x =0,xend= 350,y= 0,yend= 350,colour ="black",size=0.17,alpha=0.7)

5. Save graph

ggsave("D:/RStudio/Multiple_Graphs/Multiple_Plots.png", width = 16, height = 14, units = "cm", dpi=500)
Watch the video on YouTube
Plot multiple graphs in rstudio using ggplot2


Font Size
lines height