Ggplot - using geometries

R Programming 101
55 followers
1 Video View·May 18, 2023
Welcome back to another exciting video about R programming.
My name is Greg Martin. In this video,
we're talking about ggplot and we're going to be specifically talking about the
geometries, right? So in previous videos,
and if you haven't seen them go and watch them,
we've talked about the fact that this is a ggplot really represents an exciting
way of creating data visualization. We have to define the data,
we have to map the aesthetics against the variables,
and then we have to define the geometries.
And one of the very powerful aspects of ggplot is that,
and you're going to see this in some of the examples I give today,
you can define some of the aesthetic mapping within the arguments of the
geometries.
Now, if you don't know what I'm talking about, that's fine.
I'm going to teach this to you. It's very exciting.
We're going to go through it step-by-step.
I'm actually developing a completely new way of teaching,
and this is going to be the first of these kinds of videos.
And I'll tell you what's exciting about this. First of all,
I'm working in studio now has this thing called Quarto,
which is kind of like R Markdown where you can write code and have the
outputs of the code pop in underneath it.
But what's really nice about this is I can also push render this little button
over here and this entire document that I'm going to be using to do the
teaching, everything, this entire document with the code,
with the outputs is all going to be outputted as a PDF.
And you will have access to this PDF at the end of this video.
I'm going to put a little card on the screen. You can click on the card.
You can get this PDF, which has absolutely everything about this lesson,
the code, the text, and the really nice thing.
And this is what you're going to love. At the end of the PDF,
we go right down to the bottom.
I've put a few and I'm actually going to put a few more QR codes.
You can either click on these and they're going to take you to more resources or
scan your phone at them. And on your phone,
it'll go straight to these additional resources are going to be courses.
You can do other videos you can watch.
I'll try and have some other cheat sheets that you can get through these QR codes.
Okay. So very, I'm super excited about this.
So you're going to have access to this PDF at the end of the video.
And the PDF will have literally everything that I've been talking about.
If I zoom in here, you'll see it's got the code and everything. Okay.
So let's get into the lesson itself. This is where we ending.
This is one of the final visualizations, a slightly more complex thing.
Let's go all the way to the beginning.
I'm not going to go through a lot of the basics of GG plot in this video.
I'm assuming that you've kind of got your head around the sort of basics.
If you haven't, you might need to go and watch another video,
but essentially what are geometries? These are the, you know, is it a bar plot?
Is it a line graph? Is it a scatter scatter plot?
What kind of graph are you asking GG plot to create for you?
The most common ones, a geom point, line bar, histogram, density plot,
box plot, area, and raster.
And we're going to talk about all of these in this lesson.
So we're going to go quite quickly.
I'm going to go through the first few quite fast because some of the more
interesting aspects of GG plot are more apparent as we go toward the last few.
All right. The first one, you guys know this, it's a scatter plot.
We've got two continuous variables or numeric variables,
and we plot them one against the other. Easy peasy, lemon squeezy.
Here's the code. And by the way, in all of these examples,
I'm using data sets that you have access to on your computer right now. Okay.
So you can replicate this. This data is on your computer.
You can replicate an MT cars data set. A lot of you will note very well here.
We define the mapping aesthetics X equals weight Y equals MPG miles per gallon
here inside the geom point, right?
So geom point is the geometry here.
And these are the points in the graph here we've defined.
We've got a few arguments that we've used.
And so this is maybe the first lesson about geometries is you can define a
couple of things. Some of these arguments size,
we've made the dots a bit bigger color. We've called it blue steel.
You can do a lot of different things with color, but this is kind of,
and then alpha is how transparent it is.
And I've said 0.60 is completely transparent. One is completely opaque.
I like a steel blue and slightly transparent.
I think it gives kind of quite a nice color and then labels labs,
the labs function and the arguments of title X and Y and theme black and white.
I like that theme. I think it gives everything quite a nice look feel. All right.
So this is where we land with our first plot. Super duper easy. Boom shakalaka.
Let's keep going. Next one. Line graph. Here's where we land up similar ideas.
Aesthetics. We are plotting X's age. Y is circumference.
This is another data set, by the way, you'll like this.
It's called orange and it's,
I think it's these orange trees and it's got the different kinds of trees,
tree one, two, three, four, five. And for each tree,
we've got their age and we've got the circumference of the trees at different
ages. And we've plotted this with a line graph, super duper easy,
nothing too exciting to see here,
but I'm just showing you GM line is the function that gives us that.
Otherwise everything about this code is exactly the same.
Let's move right along bar chart. You guys know all about bar charts.
You should do.
Basically we've got a categorical variable or a factor and we want to count the
number of times the number of observations in which that factor occurs.
In this case, what are we talking about here? We've got the MPG data set.
It's a lovely data set to practice with miles per gallon.
It's different from empty cars, by the way, it's a different data set,
but it has a lot of the same characteristics.
And from that data set, we've looked at the different kinds of cars,
two seaters, compact, mid size,
and the number of times those cars are referred to in that data set.
So super duper easy. What have we got here? Is there a lesson here?
The first is when you're doing a GM bar,
you don't have to define the Y axis, right? So you're in your aesthetic,
you're mapping, you just define the X axis. In this case class,
that's the class of car. The Y axis is going to be the count.
So you don't need to define it. So keep that in mind.
And something that I've done here just to show you, well, actually, you know,
these are the same thing, fills, nothing about this is spectacular.
Let's move right along because we want to get to the interesting stuff.
Histogram, you guys know about histograms. It's a continuous variable.
The thing to remember with a histogram is it's often important to define the
bins. Okay.
Our will looking at the distribution of your data,
choose the bin width that it thinks is most appropriate.
And just so that you know, in a histogram,
it's a continuous variable and it counts up the number of observations within a
range. So between 10, and in this case,
I've got bins of a width of about two.
So one of the arguments in the GM histogram function is bin width and you can
define it. Okay. If you leave that out, I will take a guess.
It usually gets it right.
I find that sometimes if you want the graph to look exactly the way you want it
to, it's best to define the bin width yourself. Again,
aesthetics or the mapping, you know, you don't have to say mapping by the way.
I sometimes do. I sometimes don't. But in the aesthetics, X equals city.
So we're looking at the city here, cars driving in the city.
We don't have to define a Y axis because again, it's a count.
So we just define the X axis in the aesthetics.
Otherwise everything about this is pretty much like all the graphs we've had to
look at so far. Okay. So boom shakalaka, let's keep going. Density plots.
I love density plots. Again, it's looking at a continuous variable.
It's very much like a histogram, except the, uh,
the Y axis is a probability and not a count.
So it's the probability of at any of these X values,
what is the probability of an observation having that value? Um, and okay,
so that's a density plot. I'll tell you what I like about,
about density plots is it's quite nice to give them a bit of alpha,
make them a little bit see-through and then plot multiple and you'll see it
right here, multiple different categories of the same numeric variable. All
right. So we've got, in this case, let's look at what we've got here on this
plot.
We've got fuel efficiency in cars driving in the city and the X axis is miles
per gallon.
The Y axis is the probability at any given miles per gallon that a car will have
that value. Now we've divided the data into a front wheel and rear wheel drives
and we can, and we've overlapped them. You can see the difference between them.
So that's kind of quite, that's quite a nice feature of a density plot.
I like it. Okay. Now let's look at the code. How did we get there?
The first thing I did is, and again,
this is something I love about ggplot is that you can,
your data can be filtered and altered and wrangled before it even gets to your
plotting. So I always use pipe operators and pipe the data in, right?
So this is a pipe operator that essentially says means,
and then it takes whatever is on the left-hand side of the pipe operator and it
makes it the first argument in the next function. In this case, MPG,
that's the data set and then filter.
We don't have to define the data set because it's, it's piped in and we say,
now this is nice. This is a nice little feature, uh,
drive in then concatenation front and rear wheel.
Basically it's going to filter the data for rows that meet this set of criteria
front and rear wheel drive.
Cause there are other categories in this, uh,
in this particular data set and then pipe that into ggplot.
And then once again, everything's pretty much the same here.
We've got X is a city fill is drive color is drive.
And keeping in mind color is the line on the outside and fill is what's filled in
the middle. Okay. We've said alpha equals 0.3,
and that's going to net is being applied to the fill.
And then everything else is the same as other plots.
So this fill equals drive is what told fill equals drive and color equals drive
is what told ggplot to divide the plot up in terms of the character in terms of
the factors that exist in that variable. So we know drive has two variables.
If we said fill equals drive,
it's going to do the plot twice once for each of those factors,
front wheel and rear wheel. Okay. Another little thing,
I'll just show you quickly. We've got, we've defined the title,
we've defined the X axis, we've defined the Y axis.
We can also say, we know that fill, if I'm just going to basically,
I'm going to put a comma here.
Basically I wanted to show you that you can change the title of your legend over
here in your labels over there. Okay. It's done fill,
which is the inside of the graph and it's left the outside differently.
I'm not going to fuss around this right now.
I'll figure that out another time basically, but it is, it is a feature of,
of ggplot. Okay. Let's keep going. I wonder if I should edit that out.
Probably leave it in. I don't mind leaving in crap. Okay. Box plots.
You guys know about box plots. Easy peasy lemon squeezy. Once again,
we're using fill equals drive to divide up the box plots.
It's not going to produce one box plot.
It's going to produce multiple of them depending on what factors exist within
the drive variable. And in this case, we know that there's three,
there's rear, front and four wheel drives.
Box plot basically is showing you the distribution.
It's very much like histogram and density plot.
It's the distribution of a numeric variable where the box itself is the
interportal range or 50% of the data sits within the box.
The line in the middle, right? That is the median, not the mean,
not the average, the median,
the central most value in it's a good way of comparing numeric variables by a
categorical variable if you do it this way. Okay. Again, moving right along. Okay.
Here's where things start getting a little bit more interesting.
Geo area. You might not have used that before.
It basically fills up between the X axis and the Y value for a particular
particular variable.
And the best way to understand it really is to look at an example here.
We've got highway versus city driving miles per gallon on the Y axis engine
displacement on, on the X axis.
And we're dividing the data by city and highway driving now.
And by filling them both up, it's just visually a nice way. Once again,
similar to the density plot of comparing two categorical variables.
And it's easy to come in to visually see how they might be a little bit
different. Now this is where interesting,
you just kind of need to kind of lean in and pay attention here a little bit.
Look at what I've done here in the original GG plot function, right?
In the arguments there with him, I've defined just the X axis.
So I've, I've defined only that the X axis is displacement or engine
displacement, engine size. I haven't defined the Y axis. Okay.
Now that's important. And the reason is I'm wanting two plots to be basically
wanting two plots to be layered one upon another.
And I want each of them to be using a different variable to define the value
on the Y axis. Okay. That's, this is why GG plot is so exciting.
This is why GG plot is so powerful. Okay. So let's have a look at how we did it.
We've got GG plot MPG. Again, we could have done MP, MPG pipe operator.
It doesn't matter. This is, this is just another way of doing it.
The first argument in GG plot is the data. Next mapping aesthetic X equals
displacement. So we've just defined the X axis. Then we've said, plus,
it's we've layered two graphs on top of each other.
We've created two layers with GM area, GM area.
And then we've defined the aesthetic for the first layer.
Aesthetics Y is equal to highway. And then fill is equal to highway.
This is basically just giving it the,
telling it to use a different color and give it a label.
And you can see the labels over there.
So this fill is not being connected with a particular variable, by the way.
So that's also quite interesting. You can ignore that. I mean,
I'll delete that just now and show you what happens.
It's just really a way of creating a label. The important part is here.
And we, then we say plus, and we add another layer, GM area. And again, we,
the X axis is still defined.
If you define anything up here as arguments in the original GG plot function,
that applies to every single layer after that.
So the X equals displacement applies to every layer.
We've left the Y axis blank,
which means we can define the Y axis inside the particular geomes, right?
So we've got a geome area creating one layer. And in that layer,
the Y is defined as highway. We create another layer, GM area.
And now we redefine the mapping of the aesthetics and we say, Y is equal to city.
So we're creating two layers, two plots.
And in each occasion we are using a different aesthetic for the Y axis. Okay.
And then everything else down here is pretty much the same. This here,
fill equals inverted commas, inverted commas just says,
we don't have a heading over there and we've defined the colors of city and
highway. Okay. Let's move on raster plots. These are quite nice.
These are basically like, it's like a heat map.
Now the nice thing about a raster plot is this,
and let's have a look at the one we've got here.
You can look at three variables at the same time, all in one plot, right?
So we've got the old faithful Giza and we've got the duration of eruptions,
waiting time between eruptions. And then the density, which is some sort of,
I don't really understand the density in this case,
but it's some sort of mathematical function of how long or how
dramatic these eruptions are or something like that. It doesn't really,
for our purposes, it doesn't really matter what that means.
The point is you've got three variables and we can see that we've got an X and a
Y axis. And then the color inside is a function of a third variable.
So I think it's a lovely kind of plot and there's lots of context in which it
produces, I think a very exciting kind of visualization. Okay.
And then more help at the bottom. We've got these, we've got these QR,
QR codes that you can either click on or point your phone out and it's going to
take you to additional resources. Okay. I hope you found that useful.
Don't do drugs, always do your best, don't ever change.
There will be a link as I'm speaking right now,
there will be a card on the screen.
I want you to click on that card and it's going to give you the PDF.
This PDF is going to be available to you. You can use it at your pleasure,
leisure and your pleasure and your leisure. Okay. Take care.
Don't do drugs, always do your best, don't ever change. Take care. Bye.
