For part three of this introduction to ggplot, I will go over an example of a user-friendly library that can easily animate your plot: “gganimate”. It works with different types of graphs; I will apply it to the bar plot in order to visualize the variations in sensitivity indices. This would be helpful for time-varying sensitivity analysis, which is an option to prevent information loss and gain understanding of system behavior, compared to analyzing just the aggregated sensitivity indices. You can download the test-case data from here. These are first and total order sensitivity indices corresponded to annual crop yield for several parameters, categorized by different groups (such as parameters that are related to estimate crop phenology and biomass, or parameters that are related to capturing the effect of temperature or soil hydrological variables on the yield). We can create a subset of just one year of data to explore, using the general command line to make a bar plot based on different groups:
library(ggplot2)
library(gganimate)
annual_S1T<- read.csv("(your directory)/testCase_ST_S1.csv",sep="\t")
head(annual_S1T)
# S1_sobol ST_sobol Year Parameters Group_orig Group
#1 0.007892755 0.01102304 1990 a1 Hydrology A
#2 0.019468069 0.02543356 1998 a1 Hydrology A
#3 0.004058817 0.00962275 1999 a1 Hydrology A
one_year<- subset(annual_S1T,annual_S1T$Year==2000)
g1<- ggplot(one_year, aes(x=Parameters, y=ST_sobol,fill=Group))+
geom_bar(stat = "identity", position=position_dodge()) +
theme(panel.background = element_blank(), axis.line = element_line(size = 0.5, linetype = "solid",colour = "black"),
panel.grid.major.y = element_blank(),
axis.title=element_text(size=12,face="bold"),
plot.title = element_text(size=16),plot.subtitle=element_text(size=18, hjust=0.5, color="black"),
axis.text.y=element_text(size = 12,colour = "black"),
axis.text.x=element_text(size=12,colour = "black"),
legend.text=element_text(size=18),legend.title=element_text(size=20),legend.key.height=unit(3,"line"))+
labs(x ="Parameters",y="Total Order Indices",fill="Group")

We can flip Cartesian coordinates so that the horizontal becomes vertical and the vertical becomes horizontal, by adding coord_flip(). Next, we will use the whole dataset to visualize the annual variations in total order indices. By adding transition_time() and specifying the column corresponding to the variations (in our case, “Year”), we can animate our graph. We can also add a label for each time frame in the “labs/title” section.
g2<- ggplot(annual_S1T, aes(x=Parameters, y=ST_sobol,fill=Group)) +
geom_bar(stat = "identity", position=position_dodge()) +
theme(panel.background = element_blank(), axis.line = element_line(size = 0.5, linetype = "solid",colour = "black"),
panel.grid.major.y = element_blank(),
axis.title=element_text(size=12,face="bold"),
plot.title = element_text(size=16),plot.subtitle=element_text(size=18, hjust=0.5, color="black"),
axis.text.y=element_text(size = 12,colour = "black"),
axis.text.x=element_text(size=12,colour = "black",angle = 90),
legend.text=element_text(size=18),legend.title=element_text(size=20),legend.key.height=unit(2,"line"))+
labs(x ="Parameters",y="Total Order Indices",fill="Group",title = 'Year: {frame_time}')+
transition_time(Year)
With the animate() function, we can specify how long we want to wait to change the frame in the animated graph, and how long we want to pause between repetitions of the time series. The result is a gif file that can be saved using the animate() function.

It is also possible to compare the three different sensitivity indices. Total order and first order indices are provided in the dataset; by subtracting the first order indices from the total order, we can calculate the total interactions each parameter has with the rest.
annual_S1T$Total_Interaction<- annual_S1T$ST_sobol-annual_S1T$S1_sobol
One of the best ways to provide data to ggplot functions is the format that the melt function provides. This format is in fact the format that ggplot prefers. In these cases, we can reshape the data frame (using the reshape2 library) to be able to use a single ggplot command line with the few different layout panels, or different types of graph. We can us melt() function to perform this task, and reshape the data frame to have all the indices’ variables in a row instead of various columns, with the correct corresponding information about which year, group, and parameter label the data belong to. To use the melt function, we need to specify columns that identify data values and we use the id.vars argument to do that. More information about the reshape2 package can be found here.
library(reshape2)
annual_S1T_melted<- melt(annual_S1T, id.vars = c("Parameters","Group","Year"), measure.vars = c("Total_Interaction", "S1_sobol","ST_sobol"))
head(annual_S1T_melted)
# Parameters Group Year variable value
#1 a1 A 1990 Total_Interaction 0.003130285
#2 a1 A 1998 Total_Interaction 0.005965492
#3 a1 A 1999 Total_Interaction 0.005563933
#4 a1 A 1991 Total_Interaction 0.007473186
#5 a1 A 1995 Total_Interaction 0.006950200
#6 a1 A 1992 Total_Interaction 0.001914845
Now, we can create the same graph that we saw above for three variables. In this case, we can use facet_grid().
ggplot(annual_S1T_melted, aes(x=Parameters, y=value,fill=Group)) + facet_grid(~variable)+
geom_bar(stat = "identity", position=position_dodge()) +
coord_flip() +
theme(panel.background = element_blank(), axis.line = element_line(size = 0.5, linetype = "solid",colour = "black"),
panel.grid.major.y = element_blank(),
axis.title=element_text(size=12,face="bold"),
plot.title = element_text(size=16),plot.subtitle=element_text(size=18, hjust=0.5, color="black"),
axis.text.y=element_text(size = 12,colour = "black"),
axis.text.x=element_text(size=12,colour = "black",angle = 90),
legend.text=element_text(size=18),legend.title=element_text(size=20),legend.key.height=unit(2,"line"))+
labs(x ="Parameters",y="Total Order Indices",fill="Group",title = 'Year: {frame_time}')+
transition_time(Year)
