 It's LinkedIn Learning author Monica Wahee with today's data science makeover. Watch while Monica Wahee demonstrates how to use the package ggplot2 to make a time series plot in R. Hi everyone, today I'm going to demonstrate how to use the ggplot2 package in R to make a time series plot. You will see the code here, which is actually a long file of code that you can get off of GitHub. Actually, I took a video of myself running this code just to have a video to put in the background of blog posts and things where I need a video running. I actually liked this time series plot I demonstrated, so I thought I'd make this video about just that plot. So let's scroll down to the code about that. You will see on GitHub that I gave you a data frame called time series underscore DF. This is the data frame I prepared from the raw data just for the plot. Let's read it in and then run the data frame and take a look at it. You will see the data frame has been reduced to just three columns. The first column is called year, but it has a whole time stamp in it. It's actually a time stamp for 7 o'clock PM on the last day of each year. Remember the state time thing because it will come up again when we run the plot. The second column is frequency. This was the number I was trying to graph. If you read my blog post, my colleague had collected data about articles in the scientific literature on the topic of deeper learning. You'll see for the first row, which is the year 1998, she found a frequency of three deeper learning articles about face-to-face learning. And if you look at the next two rows, that same year, 1998, she found zero articles about deeper learning in the hybrid modality and also in the online modality. You can see these categories in the third column called condition modality. Okay, so in 1998, there really wasn't much online learning, so there wouldn't have been much hybrid learning either. But recently, like since 2015, you'd think they'd have more studies on deeper learning in online environments. That was what her study was about, so we wanted to make a time series graph over the years and compare the three modalities. Okay, let's return to our code. So for this plot, I not only call up library ggplot2, but I need the scales package, and I think the scales package needs deplier, so I had to call up deplier as well. I honestly don't remember exactly why I needed deplier. Maybe someone will tell me in the comments. Let's run this. Oh, and when I was recording that video, I wanted to run this color vector later, but I actually have to run it now. See this color vector? It's a vector called line underscore colors, and it's just a character vector with three mapped colors in R. These are going to be our line colors. Let me run this. Okay, now we are ready for our ggplot2 code. Let's look at each line. On the first line, we see that we are declaring our time series underscore df data frame as our data. We are setting x to year and y to frequency. I already told you we were going to do that. Okay, now here's the trick. In ggplot2, it isn't until the second line that you declare a shape that's going to get put on the plot. And in our case, we are declaring a geome underscore line. Because our grouping variable is condition underscore modality, we set the color option equal to that. But later, we'll come back and tell R what colors to use with our color underscore line vector. So we have the lab statement. All this does is add a title to the legend, which I decided to keep. So the legend will print with this title, dl method modality. Then we have y lab and x lab to specify the labels for the y axis and the x axis. Okay, here's why I said for you to remember that funky formatting for that year variable. See the scale underscore x underscore date time? That's from the scales package. That's what I had to use to get ggplot2 to display the year on the x axis. See this date underscore breaks option? I had to set that to one year. I had to read the documentation. I also had to tell it how to format the label. That's this percent y thing. Then finally, the last thing I did was add a theme command. This was to turn the axis labels 90 degrees. They were in the wrong orientation for me. Okay, let's highlight and run this plot code. Oh, that's a little scrunched up. Let me stretch it out. Lovely. Very nice. A gorgeous data science makeover, don't you think? Thank you for watching this data science makeover with LinkedIn Learning author Monica Wahee. Remember to check out Monica's data science courses on LinkedIn Learning. Click on the link in the description. Thank you for watching. If you like the video, then please hit the like button. Also, I invite you to look around my channel and if you like what you see, subscribe. I hope this video finds you well and you are having a super day.