 Hi everyone, it's MJ and welcome back to this course on R. In this video, we're gonna be looking at graphs and data visualization. Now you might be asking yourself, well, what is a data visualization? Essentially, it is the graphical representation of data. So what you're essentially doing is you're producing an image that is based on our data. Now, why do we need it? Well, we'll see that it's quite useful to visualize our input data to quickly check if there are any abnormal patterns or outliers. And it's also very useful to visualize our output data because it can help us understand and communicate the results. So data visualization is crucial because we're using it at the beginning of an investigation as well as towards the end. So now we need to ask, well, how do we do it? And there's two steps. And the first part of this video is gonna be talking about following good data visualization principles. And then the second part of the video is gonna be showing you how to use our functions from various packages. So let's maybe start off with the good principles. Graphical representation needs to be complete. You wanna show all the data and so you wanna pick a graph that gives us a big picture overview. Now, you also want your graphs to be honest. You wanna avoid misleading the viewer. And this is important because starting points and the scale of your access, accesses can distort the message of the data. You also want your graphs to be clear. This means it's easy to see with headings and labels and all those kind of things. And you don't wanna distract the viewer with unnecessary visual elements. And also you want your graphs to be comparable. You know, it must facilitate comparisons with another data set. So for example, don't make a pie chart for investment A and then a bar chart for investment B. So let's maybe look at now how to do this in R. So if we wanna draw the following plot in R, we saw that in the previous video, we simply have plot and then the data frame. And what we can see is that we already have labels and this is because we're using a data frame where the vectors had some names. But if not, you can simply add them in by saying Y lab and then equals a string, X lab equals a string and you put this within the plot function. So you would say plot, you've got your data frame and then commas, you can then add in your various labels. Notes, if you look at the picture here, we are missing a few days. I think Tuesday and that are a little bit missing. Although Tuesday is there, but not only missing a few days, but also the vectors tend to order them alphabetically, which is something we're also gonna be addressing later on. But with a simple plot, you simply need to resize the pane and they will appear. So if you do look at the, oh my gosh, I'm missing some of the things at the bottom, just resizing will allow R to redraw it and then include that missing information. Now what we can also do is we can add titles to our plot. This is an important thing you always wanna put in as much heading and valuable decoration. Like I said, you wanna stay away from unnecessary visual elements, but sometimes adding in labels and headings and legends are a good thing to do. So to do this, we just say plot, remember you've started with the data frame and then comma and we can add in all these additional things. So for instance, you can say main equals weekly weather to get your title and other things that you can do in plot is you can change the color and then there's a whole bunch of different types that you can also use. However, what we're gonna see is that it's better to use something called GG plot two. This is a much more advanced thing to do. So let's maybe spend a little bit of time on GG plot two. Now the GG is coming from this idea that it's based on the grammar of graphics. And this is the idea that every graph consists of the following elements, a data set, a coordination system and a set of geomes. And these are visual marks that represent the data. And I guess you can also say that there are some decorations, you know, labels and legends and those kind of things. But how GG plot works is that you're building up layers to create a final graphic. Now, in order to use GG plot, you need to install and load the GG plot two package NR. And if you need help with how to install and load packages, make sure you've watched that earlier video where we've discussed that. Because what I wanna do now is just jump straight into GG plot two. So what we're gonna see is that the line of code at first is a little bit daunting because you can see it's a long line. It is a long line. Also one thing that you've probably noticing is I'm using F days. And that is because like I said, the default of the vector is to order the strings alphabetically and we don't necessarily want that to happen in days of the week. So I just factored it with a label in order to get the order back or to fix the order to how we want it and not to have it alphabetically. But let's come back to this GG plot. And the first component is this GG plot where the frame. So remember, everything needs a data set and what it will do with GG plot, it will give us a default coordinate system which is the Cartesian coordinate system. Remember, there are a few other ones that you can experiment and play around with. The next thing off, we've said GG plot where the frame. We go plus geome and then point. So let's look at this. We've got geome point. Then that AES stands for the aesthetics. So we're saying what vectors we want to show. And also here, we can change the color as well as the size. So I'm making the color red and I'm making our points a little bit bigger so it's easier to read. Remember, you want your graphs to be clear and so I'm making the size to 20. So but remember, these geomes, these are the visual marks that represent our data set. And the x-axis is f days, the y-axis is temperature, the color I've made it to red and the size I've just made a little bit bigger. And then right at the end, I've added in that decoration of GG title and then you simply put in the text that you want and this adds an extra layer. Because remember, we're building up layers to create these images and this is simply our title of our graph. So little bit of a before and after. So that's what we had using plot. This is what we can create with GG plot. Of course, this is very much an introduction to R and we'll maybe have an entire dedicated course on data visualization because there's so much more that you can do. I mean, just to have a little bit of a showcase of some of the other graphs people have created in R. I mean, you can, like this looks beautiful, but you can see it's got the different circles representing different sizes, the colors, how they're all interconnected. There's quite a lot of different job circle graphs that you can kind of make by changing the coordinates system. There's also some more, yes, you're interesting things that you can do. But like I said, this is just a quick little showcase. This course is very much an introduction for beginners and you can see that you can have an entire course on data visualization. But the basic idea is that by using GG plot two, the grammar of graphics, we're adding up layers and that's how we can create these beautiful images. Anyway, in the next video, we're gonna be looking at how to create functions in R. So I'll see you then. Cheers.