0:01

So here I started up Bar, and I'm just going to do a little bit of

Â simple plotting just to kind show what the,

Â the basics are of the the plotting options.

Â So, I'm going to simulate a little, a little bit

Â of data here just so I can make a plot.

Â 0:13

A hundred normal random variables here.

Â And I'm going to call Hist to make a histogram, of the plot.

Â So the first thing you'll notice when I call Hist, is that a plot window opens up.

Â So let me just move it over here.

Â And the

Â histogram of the data is shown.

Â And so you can see that even though I didn't specify any arguments in

Â the Hist besides the data themselves a number of things appear on the plot.

Â 0:50

The label here is frequency which is the

Â default for histogram so it tells you the number of elements

Â within this range, so for example, between minus one and zero.

Â There's going to, there's a little over thirty elements of the vector in that

Â range, and you can see that the histogram is roughly like a normal distribution.

Â So let me just generate some more data here.

Â So we can make a little scatter plots I'm going to generate

Â some more data. And I'll plot xy here.

Â So now, the plotting window is already open, so when

Â I call plot it's not going to launch a new plotting window.

Â It's going to send the plot to the current plotting window, which is this one here.

Â So, I've speci-, I've called plot and you

Â can see the mixed scatter plot of the points.

Â 1:37

The default plotting symbol here is an open circle.

Â You can see. And again

Â the label, I didn't specify besides x and y but

Â that a number of things have occured in the plot.

Â For example the label here is specified as x on the

Â x-axis, and the label on the y-axis is specified as y.

Â If I had changed the name of the objects, so let's

Â say I say [UNKNOWN] here, and I plot x and z.

Â 2:14

And so a number of things on the plotting

Â region are important for example the margins here are.

Â There's four margins here, one for each side.

Â This is side one.

Â This is side two.

Â This is side three, and this is side four over here.

Â And you can see that the margin for the bottom is

Â 2:35

is, is the largest.

Â So there's, there's five lines of margin text available.

Â On the side two, there's four lines of margin text available.

Â On the top, there's also four, so, side three.

Â And on the right side, side four, there's

Â the smallest amount of a margin text available.

Â So you can adjust that using the Mar function.

Â So for example, I can just say par, and then I say Mar equals, let's say

Â I want two on every side. So two, two, two, two.

Â Excuse me. And then I can plot again.

Â 3:08

And you can see that now the plot the margins have gotten a

Â lot smaller and the plot kind of extends farther out into the window.

Â But the problem you can see is that on the x and the y axis, I lost my label.

Â And so even though I have the tick marks there I don't have my x and y labels.

Â So we probably

Â need to make them a little bit bigger than that.

Â So maybe I'll say four, four.

Â 3:31

Like that.

Â So now if I plot again now you can see there's

Â just enough room for the x and the y label there.

Â I'm just going to demo a couple of the other options that

Â may be of interest to you as you're cri, constructing your plot.

Â So first is the plotting symbol I can say plot, x,y then PCH equals, say 20.

Â That gives me a solid circle here.

Â I want a slightly solid symbol here, I would say PCH equals 19.

Â 3:56

Or I can, you know, specify PCH equals two, that gives me

Â triangles, three gives me little plus signs, four gives me x's, etcetera.

Â So you can see, so you can see that there's many

Â different plotting symbols to try, you might be wondering how I

Â know all the numbers for these plotting symbols, well, it just

Â comes after many years of use, I've memorize most of them all.

Â But, of course, if you haven't memorized them quite yet a

Â handy tool is the example file for points.

Â So if you just say points examples are points.

Â It'll go through a number of Demos so you

Â can see the capabilities that R can do with plotting.

Â But most important is their little plot

Â I'm sorry little chart of the symbols here.

Â 4:37

And so you can see that's for example one is

Â the circle, two is the triangle, three is the plus etcetera.

Â Here at 20 was the solid circle small, 19 was the larger circle if you wanted the

Â solid square that's 15. Solid triangle is 17 etcetera.

Â So you can

Â 4:56

specify what type of symbol you what and you just by, using the number here.

Â Another things, if you notice the symbols 21 through 25.

Â Those are actually symbols that are similar to say.

Â But to ones that are, have been previously shown, just for example, one thru six.

Â However the difference is in 21 through 25, those symbols have a

Â have have boundaries so you can see the boundary is red and they have a fill which

Â to this case is yellow.

Â So you can specify the two different colors, one for

Â the boundary of the outline and one for the color.

Â 5:28

And so the boundary color specified using the col col argument.

Â And the, the background color, the fill color, is specified using the bg argument.

Â So you can specify two different colors like that if you

Â 6:22

Excuse me.

Â I have to regenerate my data because it was overwritten by the example.

Â [NOISE] So here's my data again.

Â My little scatter plot.

Â Now, I can add a title to the plot by using the title.

Â Now I'll just say this is my scatter plot.

Â 6:56

Let's see, I'll give it the coordinates I'll say minus 2 and then minus 2.

Â See that the label appears there.

Â I can add a legend if I wanted to. Say the legend.

Â And the legend, you can give it, kind of location specifications.

Â So, for example, top-left will put the legend in the top-left.

Â And then I'll say

Â 7:30

So there's all kinds of annotations that you can

Â add to the data as you kind of go along

Â so for example if I want to plot a line to the data I could fit a linear model.

Â A lot that using the LN function. Then the AD line function

Â will add the linear model fit on top of the here and the data aren't related

Â to the other, each other so that the linear model is the line is pretty flat.

Â If I wanted to adjust the thick, thickness of that

Â line, I can use the AD line and specify the LWD.

Â To be let's say three.

Â And you see that.

Â Now, a new line is plotted over that, which is much thicker.

Â So you probably wouldn't want to do this

Â from the get go.

Â If you wanted to remake this plot, you'd probably just

Â specify from the get go, LWD go up to three.

Â You don't want to necessarily plot two lines on top of each other.

Â But, I'm just showing this for demonstra- Demonstration sake.

Â 8:39

So usually, you're going to want to create x labels and y

Â labels which are kind of, represent what the data are.

Â So you can plot, you can put a lot of these options in the plot function itself.

Â So I can say plot x,y and, and maybe x-lab is let's say, weight.

Â 9:23

And then I can add my little line here. So I can say, fit is,

Â [BLANK_AUDIO]

Â Or maybe I'll make the line red this time.

Â So, that's my plot, with the labels of the linear regression line.

Â And with the legend.

Â 9:46

Now let's see what happens when we try to put more than one plot on the page.

Â So for example, let's say I have another variable, which I'll call z, and

Â maybe it's a, I don't know, maybe I'll make us some Poisson data here.

Â 10:01

And let's say I want to plot z versus x and that

Â I also want to plot y versus x on the same canvas here.

Â So the first thing I can do is let's say I want to put,

Â let's say I want to put the plots right on top of each other.

Â So let's say par mf row equals so that it's going to happen, I

Â want to have two rows of plots and then one column of plots, right?

Â So that's what we want to see.

Â So now I'll apply the x and y on the top, and x and z on

Â the bottom. So x and y, so PCH equals 20.

Â So you can see that goes on the top, and on the bottom here,

Â I'll give x and z say it equals 19, and that goes on the bottom.

Â So now you can see that the margins are a

Â little bit large here, lar, probably larger than we would want.

Â 11:23

So like 20 again.

Â So that's how, now I put two plots on the screen.

Â I could have done it the other way.

Â I could have said instead of them having top

Â and bottom, I could have had them right and left.

Â So by saying par name of row, equals a 1, 2.

Â Now I can plot like this

Â 11:44

like that.

Â So you can see, I made the margins a little

Â bit too small, because I lost my y-axis label here.

Â So maybe I'll just say par.

Â I'll go back to four, four, two, two like this.

Â 12:00

again.

Â So you can see that when you've rearranged the plotting layout, you might want

Â to rearrange the canvas itself to kind of remove some of the white space.

Â I won't do that for the moment, just so I can continue with the demo here.

Â But for example, you can put four plots on a page.

Â Like say, mar equals, sorry, mf row. Equals 2, 2.

Â That means two rows, two columns, so I can say plot x and y.

Â 12:26

That'll go in the upper left. And you see, now, I can plot x and z.

Â You might wonder, well, where's the next plot going to go?

Â Well, because I specified mf row, the plots are going to go across the row.

Â So the next plot's going to be in the upper right.

Â 12:49

So that, now I've got four plots on

Â a page by specifies, specifying the mf row option.

Â If I'd specified mf call the same thing would have happened but the order in which

Â the plots occurred Would have been different, so now I can say plot x, y, and

Â 13:06

that appears in the same place, but the next

Â plot now is going to appear on the lower left, and

Â the next plot's going to be in the upper right,

Â and the last plot's going to be in the lower right.

Â 13:22

The last option I'll talk about here is the points function, just as

Â a demonstration to how you can annotate a plot by adding things to it.

Â So, let me just reset the plot region so

Â that I'm only doing one plot at a time here.

Â 13:39

Now suppose I generate some data and, and

Â suppose the data consists of say, men and women.

Â So there's two groups of people, here.

Â So I'm just going to generate some data here.

Â 14:14

So, I got males and females in this group of people here.

Â You can see it is a factor variable of two levels.

Â So, suppose I wanted to plot the data.

Â If I just plot the data X and Y you can't

Â tell who are the males and who are the females, right?

Â Because they're all the same color for example and So, suppose I want to plot the

Â data and plot the, make the males

Â one color and the females another Another color.

Â So how do I do that?

Â So the first thing you want to do, the basic idea is

Â you're going to set up the plot region, but

Â you're not going to plot any of the data.

Â 14:44

And then you're going to add the data by gender, so you're going to

Â maybe add the females first, and then add the males, and the

Â idea is that each time you add the data points, they'll be

Â of a different color, or perhaps a different plotting symbol, or whatever.

Â So first, let's set up a plotting region, so I'm

Â going to say plot xy So I'm going to pass at the data.

Â But I'm going to say type = N.

Â So this means make the plot, but don't actually put the data in there.

Â So you can see that when I hit,

Â execute this function, everything happens just like before.

Â The labels are put in.

Â The tick marks are put in. The margins are specified.

Â Everything is there, except for the data.

Â So the only thing that's missing is the data.

Â And so what I'm going to do is, I'm going to add the

Â data, but I'm going to add them one group at a time.

Â So let's say I add the males first. So I can say points x, and then

Â I'm going to subset the vector so that the g, I

Â only take the points where g is equal to male.

Â Right, so that's a subset.

Â And then I'm going to say y, and then g is equal to male.

Â So this is only going to plot, plot the points, so where where the values of the

Â g variable are equal to male, let say I'm going to set, make the, that color green.

Â 15:53

Okay.

Â So next I see the points on the, on the screen, are green.

Â Those are, those represent the male points only.

Â So I can do the same thing for the female, so

Â I can say points x and then g is equal to female.

Â 16:15

So now you can see that there are blue circles

Â for the females, and there's green circles for the males.

Â And so you can see the two groups, separately within the scatter plot.

Â And so, sub-setting based on a grouping variable is very common when making plots

Â and the points function can be used to kind of add points sequentially by group.

Â So that you can specify different types of properties for each group.

Â You can also, in addition to varying the

Â color I could have changed the plotting symbol.

Â So I could have said pch equal to let's say 19.

Â So this is a kind of solid circle here.

Â And that would have given me a solid blue circle

Â for the females and an open green circle for the males.

Â So that's one way to separate out.

Â Groups of data points on a single scatter plot.

Â