 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 with error bars in R. Hi everyone! I've noticed that time series data are popular, so I thought I'd share with you my experience of making a time series plot with error bars. I have another video where I show you a version without error bars, but this was a different use case, so I thought you might like to see it. I put the code and data frame on GitHub. You'll see here that I'm using a data frame called time series underscore error bars underscore df. Let's read this in and look at it. Oh, it's so cute. Look at this cute data set. The reason it's so cute is that it was a laboratory study where the researcher made measurements at three time points, as you can see in the time column. What were these measurements of? Well, they were these auger dishes with bacteria on them, and we had two kinds of antibiotics, ms and msn. See those values in the group column? So the researcher had all these auger dishes full of bacteria, and she put some ms on a few of them and some msn on the others. She put it in the middle, so the idea was it was supposed to kill the bacteria around where she put it, and that dead zone would be called the inhibition zone. She did not have that many plates per antibiotic, so I decided we better use the median inhibition zone rather than the mean inhibition zone. So here is the median column. I have this ll column for the lower limit and this ul column for the upper limit, and in those I use the 25th percentile and the 75th percentile. Go back to statistics class. Feels like we are back in class, doesn't it? Okay, let's look at the code. Okay, after we read in this data frame, we have to call the library ggplot2, and we have to set a vector of colors. See this vector called time series underscore error underscore colors? That vector holds the colors that the lines will be, the ms and msn lines. So I have two color values there, red for and navy blue. Good old red, white and blue. The whites in the background, yeah. Okay, well, let's run this library and vector code. Okie dokie, now it's time to dig into this ggplot code. So let's start with the first line where we declare our data set, and we set x to the time variable. Remember, time one, time two, or time three, and we set y to our median, which is our measurement. Now, because it's ggplot2, we don't get to see the data until we declare a shape on the next line, which is geome underscore line, because we are doing a time series plot. And notice we set the color to group, the group variable, because that has our ms and msn in it, and that's how we tell it to color code the lines by group. Now, the next line is a geome underscore text line. This actually puts the data values for median on the plot. This code is actually pretty cool. And the third line of the code is where we tell it what to put in the data label, which is rounding median to zero decimal places. We also tell it to color code the data label by group. But what I really want to show you here is the second line, the AES arguments. See how the x is set to time minus 0.1, and y is set to median plus 0.5? If you think about it, that's how to place the data label on the plot, so it is not right on top of the intersection of median and time on the plot. So this offsets the data label a little. The next line is scale underscore color underscore manual. That's where we get to tell it to use our vector of colors called time series underscore error underscore colors. Now we get to the line we have all been waiting for, the geome underscore error bar line. Actually, the coding is pretty straightforward. We set the x to time, the y min to ll, and the y max to ul. I like to have the limits hard coded in the data. Other people will just put an equation here for y min and y max, but I am not good enough at math for that. Most of the rest of the code is pretty typical. Labs for specifying the title to put on the legend, y lab and x lab for the axis labels, y lim for the y limits. Oh, but then we have this. I always seem to have so much trouble configuring the x axis on these ggplot2 time series graphs. Here I couldn't get r to put a point exactly at time one, time two, and time three. I had to use the scale underscore x underscore continuous command, and then inside it I put this breaks option and set it to a number vector that says one comma two comma three. Actually, I could have made a numeric vector and called it up there, but I was just happy to have figured out how to fix my problem. Then the next line is a theme command where I'm turning the axis labels 90 degrees. They came out vertical and I wanted them horizontal. Then here's the last command, theme underscore classic. I like to apply that theme if I really want the viewer to look at the shape of the plot rather than the actual numbers. Theme underscore classic applies a theme that suppresses a lot of the visual output from ggplot2 and gives a minimalist look and feel. Okay, enough talk. Let's highlight and run this plot. Okay, look at that. Thanks to our error bars, we might really believe that MSN is superior to MS at time two. But in the end, at time three, they are both the same. Oh well, sometimes the story ends like that. But the plot tells the whole story, doesn't it? Gorgeous. And that's today's data science makeover. 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 my video on how to make a time series plot with error bars. If you liked the video, then please hit the like button. Also, I invite you to look around my channel and if you like what you see, please subscribe. I hope this video finds you well and you are having a super day.