How to Create Animated Plots in R

Using Themes to Customize Plot Appearance

=============================================

When working with plots, it's essential to customize their appearance to make them more visually appealing and easier to read. One way to achieve this is by using themes. A theme defines the color scheme, font styles, and other visual elements of a plot.

In R, the `theme` function can be used to create custom themes for plots. The basic syntax is as follows:

```r

theme(theme = list(

# Add your theme settings here

))

```

By default, the `theme` function uses a set of predefined settings that include font sizes, colors, and other visual elements. However, you can modify these settings to create a custom theme.

For example, let's say we want to make our fonts bold and larger in size. We can do this by adding the following settings to our theme:

```r

theme(theme = list(

plot.title = element_text(face = "bold", size = 14),

axis.text.x = element_text(face = "bold", size = 12),

axis.text.y = element_text(face = "bold", size = 12)

))

```

This will make our plot title, x-axis text, and y-axis text bold and larger in size.

Using Themes to Customize Plot Appearance (Continued)

=====================================================

Another way to customize your plots is by using the `theme` function to change the font sizes. For example, let's say we want to make all of our fonts larger but keep them the same color as before. We can do this by adding the following settings to our theme:

```r

theme(theme = list(

plot.title = element_text(face = "bold", size = 14),

axis.text.x = element_text(size = 12),

axis.text.y = element_text(size = 12)

))

```

This will make all of our fonts larger but keep them the same color as before.

Creating Animated Plots

=====================

To create animated plots, we can use a combination of the `animate` and `lattice` packages in R. The basic syntax for creating an animated plot is as follows:

```r

library(lattice)

library(animate)

# Create your lattice plot here

p <- xyplot(y ~ x | z, data = mtcars, main = "Lattice Plot")

# Animate the plot

anim.save(p, animation.frame = 1:10, fps = 2.7, width = 16, height = 4)

```

This will create an animated GIF of our lattice plot.

Creating Animated Plots (Continued)

================================

To customize the appearance of your animated plots, you can use various settings within the `animate` package. For example, let's say we want to make our animated plots wider and taller. We can do this by adding the following settings:

```r

anim.save(p, animation.frame = 1:10, fps = 2.7, width = 1600, height = 400)

```

This will create an animated GIF of our lattice plot that is wider and taller than before.

Conclusion

----------

In conclusion, using themes in R can help you customize the appearance of your plots and make them more visually appealing. By modifying the default theme settings, you can change font sizes, colors, and other visual elements to suit your needs. Additionally, creating animated plots with R can add an extra layer of interactivity and engagement to your data visualizations.

Customizing Layout and Color

---------------------------

In addition to customizing font sizes and colors, you can also customize the layout of your plots by using settings such as `facet_wrap` or `grid.arrange`. For example:

```r

# Create a facet grid plot with 5 panels per row and 2 rows total

p <- ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +

geom_bar(stat = "identity") +

facet_wrap(~ group) +

theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Create a grid arrangement plot with 2 panels per row and 3 rows total

p <- ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +

geom_bar(stat = "identity") +

grid.arrange(p + theme(axis.text.x = element_text(angle = 45, hjust = 1)),

p + theme(axis.text.x = element_text(angle = 90, hjust = 1)),

nrow = 3)

```

You can also customize the color palette by using settings such as `theme` or `ggplot2`. For example:

```r

# Create a plot with a custom color palette

p <- ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +

geom_bar(stat = "identity") +

theme(

axis.text.x = element_text(angle = 45, hjust = 1),

axis.title.x = element_text(angle = 45, vjust = 1.5)

) +

scale_color_manual(values = c("blue", "red"))

```

"WEBVTTKind: captionsLanguage: enin this video we're going to be making an animated plot in r using the gt animate r package and we're starting right now so the animated plot that we're going to be making today will look like the one shown here and as you can see here this is the gapminder data set where by each subplot panel here you're going to see the different continents and the data is on a time series data and the animation that you see here are changing over time and so the x and y variables that we're plotting here in the scatter plot is the total population in the x-axis and life expectancy in the y-axis and so we're going to make this plot in this tutorial and so let's get started so the four libraries that you should install prior to beginning this tutorial is to install the gapminder library gtplot2 gg animate and also the gif ski so if you don't have it you could simply run this line and it will install the four libraries for you since i have already installed it we're going to skip it and i'm going to comment it out and so the first thing is to load the gap minder into the environment and you could do that by typing in library and then gapminder in the import argument and then we're going to run gt plot2 because the plot will be made in gta plot 2 and the gg animate will be adding the animated aspect to the plot and the gap minder will contain the data set that we're going to use today okay and so we have already loaded up three libraries and let's have a look at the data sets and you could click on the line that you would like to run and then on my mac i type in command enter and so you see the data set here it is in the form of a table and table is rs version of a data frame or essentially the tidy version of a data frame and so to make the static plot we could use this block of code here and so i'm going to dissect for you what each lines are doing but before that let's run it let's run line 17 until 23 here hit on enter command enter and then you're gonna see the static image shown in the plots panel in the bottom right hand corner here so notice that all of the data points from all of the years from 1952 until 2007 are in the same plot so you're gonna see that they're dumped into the same plot right and in order to save that into a file you're gonna use the ggsave function and then the input argument here will be the name of the file and then the width and the height is specified to be eight all right and so eight would be eight inches here and let's run that let me go to the folder files panel to run it again okay and now it ran and i get the file here and this is the file that was written out and so notice here that you could specify the dimension of the plot as i've shown you on the width and the height so this is 8x8 and if you would like to make it wider you could have 8 to become 12 for the whip and let's run again and it's a bit wider okay and the panels look a bit more square okay and let's say that we're happy with that then we're gonna see it further but notice that this is the bare minimum for generating the static plot and if i would like to customize the plot i would add the theme here and so the customization that i would like to make would be to add the plot title change the size of the font and making it bold or also changing the color as well so you could do that in the theme function here but before going there let me provide you a line by line explanation okay so here you can see that to make the static plot you're going to use the gzplot function and the input argument the first one is the gapminder and so that's the name of the variable that contains the data and as mentioned here when i type in gapminder i get this table data frame or i could also type in view if you would like to see it in a spreadsheet like manner you could type view and then get minder and then you see it like this like a spreadsheet okay otherwise you could simply type gapminder and then you see the entire data frame okay and notice that we're going to use in the aesthetic function gdp per capita as the x axis and the life expectancy as the y axis which are the two options here and the size will be made according to the total population which is the column here population so the size of each dot will be according to the population so each dot will be big if the population is large and the dot will be small if the population is small so it is shown here okay so if the dot is small population is small large dots will represent the bigger population and the color will be according to the country okay and notice that we add the plus sign here in order to add additional functions and there are additional functions in gt plot that are responsible for different aspects of the plots and so gg stands for the grammar of graphics and to make a plot using gt plot you're going to have to define it layer by layer okay so the first layer would be to define the the basic plot and we have here a basic scatter plot right you specify the x and the y and the data sets and you specify the size of each point and also you specify the color of each dots and now we're ready to specify the transparency of each dot okay so here we set the alpha transparency to be 0.7 if it's one it means that there's no alpha transparency and it will be solid color and if you use a lower number like alpha 0.3 then your dots will be very translucent if you use alpha of 0 then you won't see anything okay and here we specify the legend to be false because we don't want to show it and it should be noted here that the example of this particular scatter plot was obtained from the gg animate website and let's continue we're going to use the scale color manual so that we will use the country colors that are specified according to the gap minder package and scale size we're going to make the range between 2 and 12. and did you recall that i told you that each dot will range in size according to the population size so if the population is small it will have a small size of two and if the population is bigger then the dots that you see will have a size of 12. okay as you see right here the bigger dots are having a size of 12. and scale x log 10 will make the x axis to be in the logarithmic scale let me show you again okay right here it's one e plus zero four plus zero three so it is in the log scale and so log scaling will allow you to make the data distribution more uniform and then we're using the facet wrap function in order to split the graph and so if we don't use that option everything will be in the same plot let me show you okay you will see here that everything will be in the same plot but if we use the facet function notice that purple color is a continent orange is a continent and each continent will have its own separate subplot if we use the facet wrap and upon using it notice that they are split into five different subplots okay otherwise they're merged into one and the labs function will allow you to specify the title which is at the top here the year and the x-axis title gdp per capita and also the y-axis title life expectancy okay and in g-save you specify the name of the file you specify the width of the file and also the height of the file and it is in the unit of inch okay and let's say that the resulting graph that we have saved to a file you will notice that the text size is pretty small so let's do something about that all right so we're going to use the theme function here and then we're going to specify the color and the bold face of the font to be bigger size and we're going to keep it at that color all right and so let's run it again here and notice that i set the width to be 16 and 4 which is not here but to the file that has been written out let's have a look at it all right and now the written file gives you a wider image so each sub panel here are having a square shape and you could notice here that the fonts are now bigger and they're bold okay so better visibility all right so now this is the static plots and so how do we exactly make it animated let's scroll down here right here so the magic lies in this three lines of code so the upper part here are the same okay the same as the one i mentioned above and so we have these three lines and so line number 66 here will allow us to define the label so almost the same except for the title and so the title will have the animated part would be the year and so over time the year will change right 1952 53 54 and so the transition time function will allow you to specify that the year will be animated and then the ease aesthetic function will allow you to specify that each snapshot in time will be linear okay so if it's not linear it might mean that some aspect of the animation will be slow and some aspect will be faster we'll gain momentum and be faster but we don't need we don't need that so we're just going to have it plain linear and then the animate function will take an input argument to be p1 and p1 is the name of the variable which we assign the entire plot here to be p1 okay and then finally we use the anim save function to save the animation to a file all right and so let's run the block of code so this should take some time because the rendering of each animated screen will be proceeded one by one okay so one snapshot after the other and so here they provide you an estimated time so it was probably about 30 seconds and the frame per second here is at 2.7 and the frame per second is also a parameter which you can set you can make frame per second to be 10 and the animation will occur much faster all right and now it is weaving together all of the images together and saving it into an animated gif file let's open it up but if you have a mac and you open it up like this you will see a lot of screens or snapshots here and it's not animated right and you'll notice that it has 100 images and each images will be like slightly animated if you look here to the right you scroll up one by one you will see the image slowly animating okay but this is manually animating it but then you could drag and drop it into a google chrome and there you go you have the animated gif right so you could open it up in a google chrome right so you could also try out customizing your image as well let me run this block of code and so the customization is bigger font size making the text to be bold and black and also making the plot to be wide with a width of 1600 by 400 and so notice here that it is not in unit of inch but the unit now is the pixel all right and so now the image has now been written to a file and you can see here in the preview that is now animated let me show you and so this is the resulting animation that we have created okay and so you could further customize this for example in the facet wrap right here i specified to be having five columns so if you modify it to be one you would have one column and then therefore your panel plot will be vertical so you would have five plot vertically stacked on top of one another and if you have two columns then you will have two by two and then one if you have three column you would be having three on the first row and two on the second row okay so you can customize the layout of your graph and you can customize the color and also whether you want it to be bold or in italic or the font size they would like to have alright and so i hope that you found this video helpful and please support the channel by smashing the like button subscribing if you haven't already make sure to hit on the notification bell so that you will be notified of the next video and as always the best way to learn data science is to do data science and please enjoy the journeyin this video we're going to be making an animated plot in r using the gt animate r package and we're starting right now so the animated plot that we're going to be making today will look like the one shown here and as you can see here this is the gapminder data set where by each subplot panel here you're going to see the different continents and the data is on a time series data and the animation that you see here are changing over time and so the x and y variables that we're plotting here in the scatter plot is the total population in the x-axis and life expectancy in the y-axis and so we're going to make this plot in this tutorial and so let's get started so the four libraries that you should install prior to beginning this tutorial is to install the gapminder library gtplot2 gg animate and also the gif ski so if you don't have it you could simply run this line and it will install the four libraries for you since i have already installed it we're going to skip it and i'm going to comment it out and so the first thing is to load the gap minder into the environment and you could do that by typing in library and then gapminder in the import argument and then we're going to run gt plot2 because the plot will be made in gta plot 2 and the gg animate will be adding the animated aspect to the plot and the gap minder will contain the data set that we're going to use today okay and so we have already loaded up three libraries and let's have a look at the data sets and you could click on the line that you would like to run and then on my mac i type in command enter and so you see the data set here it is in the form of a table and table is rs version of a data frame or essentially the tidy version of a data frame and so to make the static plot we could use this block of code here and so i'm going to dissect for you what each lines are doing but before that let's run it let's run line 17 until 23 here hit on enter command enter and then you're gonna see the static image shown in the plots panel in the bottom right hand corner here so notice that all of the data points from all of the years from 1952 until 2007 are in the same plot so you're gonna see that they're dumped into the same plot right and in order to save that into a file you're gonna use the ggsave function and then the input argument here will be the name of the file and then the width and the height is specified to be eight all right and so eight would be eight inches here and let's run that let me go to the folder files panel to run it again okay and now it ran and i get the file here and this is the file that was written out and so notice here that you could specify the dimension of the plot as i've shown you on the width and the height so this is 8x8 and if you would like to make it wider you could have 8 to become 12 for the whip and let's run again and it's a bit wider okay and the panels look a bit more square okay and let's say that we're happy with that then we're gonna see it further but notice that this is the bare minimum for generating the static plot and if i would like to customize the plot i would add the theme here and so the customization that i would like to make would be to add the plot title change the size of the font and making it bold or also changing the color as well so you could do that in the theme function here but before going there let me provide you a line by line explanation okay so here you can see that to make the static plot you're going to use the gzplot function and the input argument the first one is the gapminder and so that's the name of the variable that contains the data and as mentioned here when i type in gapminder i get this table data frame or i could also type in view if you would like to see it in a spreadsheet like manner you could type view and then get minder and then you see it like this like a spreadsheet okay otherwise you could simply type gapminder and then you see the entire data frame okay and notice that we're going to use in the aesthetic function gdp per capita as the x axis and the life expectancy as the y axis which are the two options here and the size will be made according to the total population which is the column here population so the size of each dot will be according to the population so each dot will be big if the population is large and the dot will be small if the population is small so it is shown here okay so if the dot is small population is small large dots will represent the bigger population and the color will be according to the country okay and notice that we add the plus sign here in order to add additional functions and there are additional functions in gt plot that are responsible for different aspects of the plots and so gg stands for the grammar of graphics and to make a plot using gt plot you're going to have to define it layer by layer okay so the first layer would be to define the the basic plot and we have here a basic scatter plot right you specify the x and the y and the data sets and you specify the size of each point and also you specify the color of each dots and now we're ready to specify the transparency of each dot okay so here we set the alpha transparency to be 0.7 if it's one it means that there's no alpha transparency and it will be solid color and if you use a lower number like alpha 0.3 then your dots will be very translucent if you use alpha of 0 then you won't see anything okay and here we specify the legend to be false because we don't want to show it and it should be noted here that the example of this particular scatter plot was obtained from the gg animate website and let's continue we're going to use the scale color manual so that we will use the country colors that are specified according to the gap minder package and scale size we're going to make the range between 2 and 12. and did you recall that i told you that each dot will range in size according to the population size so if the population is small it will have a small size of two and if the population is bigger then the dots that you see will have a size of 12. okay as you see right here the bigger dots are having a size of 12. and scale x log 10 will make the x axis to be in the logarithmic scale let me show you again okay right here it's one e plus zero four plus zero three so it is in the log scale and so log scaling will allow you to make the data distribution more uniform and then we're using the facet wrap function in order to split the graph and so if we don't use that option everything will be in the same plot let me show you okay you will see here that everything will be in the same plot but if we use the facet function notice that purple color is a continent orange is a continent and each continent will have its own separate subplot if we use the facet wrap and upon using it notice that they are split into five different subplots okay otherwise they're merged into one and the labs function will allow you to specify the title which is at the top here the year and the x-axis title gdp per capita and also the y-axis title life expectancy okay and in g-save you specify the name of the file you specify the width of the file and also the height of the file and it is in the unit of inch okay and let's say that the resulting graph that we have saved to a file you will notice that the text size is pretty small so let's do something about that all right so we're going to use the theme function here and then we're going to specify the color and the bold face of the font to be bigger size and we're going to keep it at that color all right and so let's run it again here and notice that i set the width to be 16 and 4 which is not here but to the file that has been written out let's have a look at it all right and now the written file gives you a wider image so each sub panel here are having a square shape and you could notice here that the fonts are now bigger and they're bold okay so better visibility all right so now this is the static plots and so how do we exactly make it animated let's scroll down here right here so the magic lies in this three lines of code so the upper part here are the same okay the same as the one i mentioned above and so we have these three lines and so line number 66 here will allow us to define the label so almost the same except for the title and so the title will have the animated part would be the year and so over time the year will change right 1952 53 54 and so the transition time function will allow you to specify that the year will be animated and then the ease aesthetic function will allow you to specify that each snapshot in time will be linear okay so if it's not linear it might mean that some aspect of the animation will be slow and some aspect will be faster we'll gain momentum and be faster but we don't need we don't need that so we're just going to have it plain linear and then the animate function will take an input argument to be p1 and p1 is the name of the variable which we assign the entire plot here to be p1 okay and then finally we use the anim save function to save the animation to a file all right and so let's run the block of code so this should take some time because the rendering of each animated screen will be proceeded one by one okay so one snapshot after the other and so here they provide you an estimated time so it was probably about 30 seconds and the frame per second here is at 2.7 and the frame per second is also a parameter which you can set you can make frame per second to be 10 and the animation will occur much faster all right and now it is weaving together all of the images together and saving it into an animated gif file let's open it up but if you have a mac and you open it up like this you will see a lot of screens or snapshots here and it's not animated right and you'll notice that it has 100 images and each images will be like slightly animated if you look here to the right you scroll up one by one you will see the image slowly animating okay but this is manually animating it but then you could drag and drop it into a google chrome and there you go you have the animated gif right so you could open it up in a google chrome right so you could also try out customizing your image as well let me run this block of code and so the customization is bigger font size making the text to be bold and black and also making the plot to be wide with a width of 1600 by 400 and so notice here that it is not in unit of inch but the unit now is the pixel all right and so now the image has now been written to a file and you can see here in the preview that is now animated let me show you and so this is the resulting animation that we have created okay and so you could further customize this for example in the facet wrap right here i specified to be having five columns so if you modify it to be one you would have one column and then therefore your panel plot will be vertical so you would have five plot vertically stacked on top of one another and if you have two columns then you will have two by two and then one if you have three column you would be having three on the first row and two on the second row okay so you can customize the layout of your graph and you can customize the color and also whether you want it to be bold or in italic or the font size they would like to have alright and so i hope that you found this video helpful and please support the channel by smashing the like button subscribing if you haven't already make sure to hit on the notification bell so that you will be notified of the next video and as always the best way to learn data science is to do data science and please enjoy the journey\n"