My name is Ashish Mahabal and we'll be continuing with R. Today we are going to see more about ggplot. So let's take a look at the graphic packages that are available in R and do a quick comparison of them. The base graphic has been available right from the start because it came from S and Ross Ihaka was instrumental in that. You can do most of the things with the base graphics but it is not easy to modify that. It can be all low level things but if you want to do some complicated or complex plot, then you know, to really go to a lot of trouble. After that, in about 2000, Grid graphics came into the picture. These are low level but they do not support statistical plotting. And that is sort of against the, our principal because our is mostly about statistical methodology, statistical procedures and if you cannot do a lot of statistical graphics within a plotting package, that's not very useful. The Lattice package came into picture around 2008 and that is much more powerful. It is based on grid, it is detailed, but then it lacks a formal model. And so, a bit before that in 2005 ggplot2 that came on to the scene made with a very welcome attitude from everybody because that is where you can do lots of very interesting things and all the power of R could be brought to bear on radius analysis that one can do. So here, it is a layered structure and it is based on something called grammar of graphics. And we'll be seeing more of that soon. It is low level and you can build static plots with it. The other important thing, is that extensions to ggplot2 are very easy and that makes it another welcome addition. The most important however, is that the flow of ggplot2 is like that of your analysis itself. Because it is layered, you can think of one thing at a time. You can start with one piece. You can build it. And then on top of another, on top of that, you can build another layer, a third layer and so on. So, if you want to say, plot points, you know, plot points and then you say oh, but what we also need is to do some smoothing on this. So you can go ahead and do the smoothing on that plot as an additional layer without having to redo the earlier calculation, or without having to relay the earlier structure. On top of that, suppose you decide that you want error bars plotted with the smoothing, you can do that too. So, in a sense, we can have many, many layers added to a single plot and build your plot just like you would build your own analysis. The flow is the same. rgobi, ggobi, and rggo that came in 2007, 2008 also use the grammar of graphics, but they are more interactive. So you can have an interactive plot and make changes in that and so on. The latest kid on the block, just about one year old, is ggvis. And that is also an extremely powerful visualization tool. It is browser based, so that brings the game right to your browser. And because our studio has an elementary browser, you can also do the browsing and plot building within our studio itself which you should be using by now. So this is also interactive graphics and we won't be using more of that but I urge you to explore these other packages, especially ggvis also. And you can see more details on the URL that I have mentioned here. So, what is the grammar of graphics? Just like the grammar of a language has various concepts, and there is a structure to each sentence, there is a relationship between different words, the verbs, adjectives, noun and so on. So similarly, in the case of grammar of graphics, it looks at graphics as a picture built of various elements which have specific connections with each other. So, what are the key words here? We have a data, in case of are it's, in the form of a data frame, and the data frame has various columns. So on these columns, what you do is you define certain mappings and you associate geoms with them. You're geom could be a point, it could be a line, it could be some kind of 2D density. So, given certain columns from your data frame, you map them using these geoms. And then additionally you include some statistical transformation. And the statistical transformation again could be something like smoothing or benign, et cetera. Most geoms have a default associated stat with them. Similarly, most stats have a default associated genre with them. But you can do mix and match with different sets of those, and we'll see a partial list of each of them later. The, another interesting aspect is that you can do faceting on this data. By facet, what one means is that if one has a categorical variable, a variable which is not continuous and has some discreet values, then you can divide your data, or facet your data on that particular variable. And ggplot allows you to very easily plot each variable separately in a separate sub-plot, and that can be extremely useful. We'll be seeing an example of that later. And then the other two keywords that get used are scale and coord where you apply a certain scaling, you train a scale. And then you use a coordinate system with that. So given these different keywords in the grammar of graphics, you can layout in your mind how your plot should look like and then there're easily usable techniques, easily usable keywords within ggplot that allow you to layout your layers within the plot. So to start using ggplot, you would do that as you do any other package. So, you'd say install package ggplot2 and to load it you say library ggplot2. So, and the URL is up there where you can get it from. The data set that we'll be using is from astronomy. We have a, a transient survey, the transient survey, and for the transient survey, what we do is that we obtain lightcurves for many, many objects. In fact, it is using these lightcurves that we detect whether a particular object is a transient or not. A transient being an object that changes in brightness in a relatively short amount of time. Here we see three examples. We won't be going into detail here, but the description of the dataset will have a little bit more on the website. So, the data set that we'll use here is called crts_6class. That is because this data set has information about six different classes and that is what we'll be using. There are a little over 1600 rows. There are 20 useful variables. And there is one categorical variable, namely the class or the type of the object. This is how a typical light curve looks. So a light curve is a time series. On the X axis you have time, in this case it is a phase time, that is you use a particular wrap around at the same period again and again, so you get a phased light curve. And on the y-axis is the brightness. And a couple of parameters that can be derived from such a light curve are shown on the plot. Now, you derive many different parameters. Mean, median, a skew kurtosis, and so on. And it is exactly these kinds of numbers that are available in the data set that we have. So, the first ten lines and only the first few columns are shown here. Amplitude is the last column shown. That is the first use for variable that we can use because position parameters, we won't use them for trying to define any particular graphing here. So, if one wanted to simply take, say, the amplitude and standard deviation of all these points and plot them, in the base graphics it is a fairly straightforward thing. All you had to do is say plot amplitude comma STD, provided both those variables have been attached to your username space, and then you'll just get a plot that shows amplitude with a standard deviation. Now ggplot2, has two different techniques. Qplot, which is the simpler one, but not as powerful, and ggplot, which is the more powerful, and that's the one that you should be using. But we also should see, how qplot can be used. So in case of qplot 2, or which is essentially been written to do a mapping to the plot, base graphics, we can simply say again qplot amplitude was steady and you'll get the same functionality. In this case of course the data=crts_6class has to be attached at some point before. Otherwise, you are to stipulate that as an argument. And the plot looks slightly different, but it's exactly the same functionality. Now when it comes to ggplot, there, you have to understand that you had to provide the columns as aesthetics. And that is what is seen in the first example here. We provide to ggplot, a single argument, the aesthetics and within aesthetics, which itself is a function, which provide the column names, Amplitude and STD. But here, the data equal to crts_6class or whatever the data frame name is, you have to provide it. Unlike Qplot, it is not going to take it from an earlier invocation. So within the call, you have to give that. And the first statement there is going to just return an error saying, I don't know what data you're talking about. So the next one says that lets use data of the particular data frame and then we provide amplitude and STD as the aesthetics. Now it goes through, but it still doesn't do any plotting because that's what we didn't tell it to do, right? Because there is no rendering instruction yet. So the rendering instruction is separate. And that is what is shown in the last statement that you see here. So you invoke ggplot2, and you'll notice a plus after that. That is where the layering is coming in. You say that I want to use the point geom. And so the geom point here in this case need not take any argument. It can take arguments. We'll see a little bit about that. But when you combine the two, then finally you get a plot. And the plot looks the same as before. This is just ranting. So, what you can also do, and this is the important part, is that you can assign the first part that we did of ggplot and assign it to, say, an object called B. And that of course, does nothing on the screen, except for making that object B. And then now separately, you can add to that object P, your geom point with no arguments and viola, you'll get your plot. Now, I mentioned faceting earlier. So, that is something that you can easily do. Here, we see an example of that. Here besides amplitude and SDD, we give one more argument. Do the aesthetics function? That is the column called object. That is where the class of all different objects is present. So we can also name the documents by the way. Here we say that amplitude is to be used as x, and STD to be used as Y. That allows us to exchange the positions of the functions quite easily. And then we say that use shape for that object. So what happens now is that each of the different classes that we have, come with a different shape. So it's the same plot, but now you can see that you've in the, on the right hand side, you'll see a and the shapes are seen there. And in this case the shapes are not very easily distinguishable, so instead of shape, we could even say that oh, let me use colors instead. And so, if you say color equal to object. Sure enough, each type of object now has a different color, in this case it is much more much better visible. Then, once you have done that, you can think of additional things. Oh, how does the density of it look like if I were to smooth the data, how would that look? If I want to see as a function of x-axis, what is the centroid of each of the different layers and what do I see? So one can easily do that. All that one has to do is add yet another geom here. If you add a geom smooth, then you do see how that a function will look like. You see that on the right-hand most side where there is an outlier. There is large sigma there but in the center of the data you see that it's a very thin line which tells you that visually though you see a lot of spread, really there is not that much spread. So this is something that becomes then very easy to see with these simple parameters. So here in this case we have three different layers. We have used a geom point and geom smooth. Now we can get rid of the outlier and we can see that better and we can do lots of more things with this kind of data set. So what I'm going to do now here, we have gone back to using a single color for all objects together. We had earlier seen that one can have the geom point and geom smooth with all those points but what happens if we, now, use the faceting onto objects, and do the smoothing. So, in fact, we can see for each class, just for that class, how the smoothing looks like. So, you can immediately start seeing there the different populations, maybe even in this very dense cluster. So, if you have such a dense cluster, what one could try to do is ungittle a little better.word g/ them a little bit and see the spread better and there is another available for that called so this allows you to see how that one looks. Or you can add an alpha parameter to that, through another layer or give that as an argument to geom_jitter. So here we are saying that the alpha parameter 0.75. So you see that some of the points have become more transparent, that allows you to see, where the density is better than before. All that can be are the geoms like the boxplot in this case, so you see how boxplots for each of the objects look. And again, you can see that command is being only slightly being modified with some different geom and you get quite interesting and straightforward plots. Or you could look at the geomic density. And you can see how the density is as a function of amplitude, so different classes you can immediately see how they occupy different parts of the space. Now if you wanted to really see each of them in a separate sub-plot, you can do that using the facet that I mentioned earlier. So here we are telling GGPlot to use object as a faceting parameter of the categorical variable. And again, the original command is the same ggplot, geom point and just the faceted grid. And you see that the six types of objects as seen in a separate subplot. The important thing, is that all of them have the same Y scale. So it is not as if they are being separately scaled on Y, because that'll be confusing things here. We immediately see that the third column, that of CVs has a much larger standard deviation than say, in the first column, the agents. And that is very important for it to be done in an automated fashion. Or what you can then do is that whatever smoothing that you had done, you can apply it separately to these faceted grids also, and you can see how the smoothed diversions look on each of them. W we'll stop and continue with ggplot2 next time.