Welcome everybody, you're watching Mr Fugu Data Science. We're going to do some
gg plot 2 basics today. Here's some of the sales data we will be
dealing with. Description, quantity, unit price those
would be combined to make a separate column called total price or
total sales in countries. We will take the countries with their
combined sales; create a plot as well as
taking the top 12 items that were sold with their sales totals. Think of ggplot
taking in data frames ,whereas base r utilizes vectors for plotting. And you're
using layers with ggplot which is a
distinction. You initialize your plot by calling
ggplot and you map your variables with
something called an aesthetic: labeled aes
which is your mapper for the variables/ And instead of usually calling your data
frame with the dollar sign and variable name.
You'll just call the variable name let your mapping. You also have things
that are called themes, and a theme is basically dealing
with anything else that's not your data that you're trying to map or plot
and you'll you'll see that shortly. So let's get our first bit
of data that we're going to deal with. Let's call in what we're going to map
for x and y. Which would be the description
and our sales total. Scroll down and you see
this is blank and it looks kind of small so there's a few things that we're going
to need to also do and this x label looks really atrocious.
Let's work piece by piece. First thing we need to do: is figure out
now how do we fix this so it's not barren. So let's copy this
let's paste it in; and now we have to add something in so you just use a plus.
Ggplot doesn't know what you're trying to plot.
Are you trying to plot a bar chart, a histogram, scatter plot,
nested plots? You have to actually tell it and that's where you're basically
creating each layer. So now we got our points. This is
looking a little promising. And before any trolls in the distant future far
far and away from now try to give me trouble: because, i'm doing
a scatter plot currently. I will do a bar chart considering we're going to deal
with categorical variables that are down here,
okay. Instead of something that was like discrete for instance so don't give me a
heartache i'm just showing this for illustrative purposes.
Now, since we're adding some layers let's increase the size of this plot so it
could be a little better in terms of illustrative purposes
and basically allowing us to see what's
going on make it more legible. You have to call
this thing called options but, we're going to change the width.
I arbitrarily decided this earlier, and then
i'm going to also call in the height as well and change that.
So let's see what this looks like. It looks bigger right?
We can make this plus the height so
see it's getting wider. So it's looking a little bit better.
If you see this representation of the plot width.
Now, what else can we do with this to make it a little better. We need to
size up our labels for one. The ticker we need to make those bigger on the
y-axis. Figure out how to achieve something with this x-axis.
So it's actually legible. Let's do that by step, okay.
To increase the label sizes; let's do that now
and if you notice everything else from above is just staying the same. We're
just chaining everything together with a plus sign. So to increase our labels
we're going to use our first little theme.
And i'm going to call in my axis text: where
we say it equals the element text. And i'm going to change the size of
this to 14. So we can see this. and what other option do i have here?
Uh i'm gonna call it bold.
And there we are. I forgot also, the axis title.
We're going to use a little element friend, element text
and then let's make that equal to 14 as well.
And let's call the face equals bold again.
You can see that these are now bold and you'll see that these are
increased in size and this is still legible, which is wonderful. But, we're
getting close to what we need. So let's change this ticker here for the
x axis and throw them at an angle so we
can actually read this. Let's do another theme :where we're
taking the axis text of x.
And we're going to change the angle of it.
I decided for this example the angle of seventy looks good. and i'm doing a height
adjustment of one. And i'll explain shortly what it means when you're doing
an h just. Now we got it adjusted at an
angle so it makes sense. We're doing pretty good. We still need
a title which would make this a little more
appropriate: so we understand what we're looking at.
Probably, consider changing this label on the y-axis
too. So it looks a little more professional.
Okay, if you're wanting you can increase the size of this too.
I aliased this with g scat from what we just did. So i don't have to
basically copy cut and paste anything. and when you're dealing with the h
adjustment like i said i would explain. Think of using this to shift from left
to right horizontally, all right. Let's deal with title and
subtitle. I'm going to call something called
labs for labels. And let's call in our title and i'm
going to call that sales by top 12 items.
And then, i'm going to also have a subtitle. Which will be by description
name.
Then i need to also call on something else.
I need to now; let's look at this and you'll see why.
So you see that it's on the left hand side. We need to shift it over and
center it. We can consider changing the color or
increasing the size. Let's do with this right now. Oh let's
use our trusty old theme once again. And i'm going to say plot that title and
then a little element text. And what do i do
with this? I want to change the color of my text
for my title and i'm going to arbitrarily just make
it red, okay. I'm going to increase the size of this
as well as make it bold. And i'm going to also adjust the
horizontal direction to center this. What else can we do with
this? We can play with the plot subtitle now;
and do basically the same thing. Except i'm going to adjust it a little
bit.
And i'm going to call this one 16 instead. And instead of having it red
let's just make it blue for no real reason.
So now we're centered we have it increased in size and
changed colors. So we're doing pretty good what else can we do we could also
change these y-axis labels or ticks so it's not so big if we want to do that.
We could even adjust any of the range values.
We could also do some highlighting on the axis.
So let's do that. Let's think about how we're going to
play with this. Let's calling our theme; and we're going to do an axis line.
And that axis line will have: what is it? It's going to have
our element. but, this time it's a line. And this axis line will have a color
equal to trusty old red. But, it's gonna have a
really small size of two.
And that's where you can do a little customizing and
make this pop. You don't need to do a red of course. But, i just wanted to
illustrate that you can do this. What else can we do? Change that scaling
of the y-axis. There's a lot going on here. So let's
do something real quick. We need to figure out how we're going
to scale this. We're starting at 30 000 that's our lowest value
and we're over 120 so this is probably 130 or 140.
And since we have an idea: let's figure out how we're going to break this
up. I'm going to start at 30000 for my lowest value
i'm going to go up to 135 000. And i'm going to increment by
fifteen thousand. But what else do i need? Well
there's a couple things that i need in this.
I need labels; and i'm going to change this, instead
to something else and you're going to see.
We're going to change instead of 120 000
it's gonna be 120k for example. Paste in divide this by 1000 and replace it by k
maybe oh maybe it's like that.
There we are now: after all that fiddling around we got this going. So we replaced
that with 135k so that looks pretty good. We can do that
y label and what are we going to call that
this just name is something simple. Let's call it sales
total. and then this would be a great British pounds.
So we changed that up. We can add a horizontal line for where the mean would
be. That's going to be pretty simple. So do
geom h line. It's going to be a horizontal line on the y-intercept: where
we're taking in the mean of our data. Which will be
the sales totals. And then i'm going to color this our trusty old
red. Now we have a horizontal line of
where the mean would be. If you wanted that for instance.
So for any trolls that are still watching in the future.
If you're saying why didn't he create a bar chart? Why do you use a scatter plot?
Because i was showing the basics of how you use a scatter plot. If you're calling
in a bar chart you would call in the same
data. But, you would instead call this right here geom bar
stat identity and this will give you your bar chart, right here.
Big old giant thing, right. And if you notice something when i call in the
increased plot size before from the previous cells. It's doing it for all the
continuous cells now. Now these data are interesting becausem
you would use the options sci pen to remove
all of the scientific notation. Let's look is something real quick.
I think it might have; since i've already ran this on mine. it might have.. Nope
let's zoom in: there's your scientific notation
right ,here okay. Now
should be taken care of unless you want something like that. But,
you take care of it using something like this. And you call in your
geom bar chart, to create your bar chart. And then everything else that we did
from the previous example could still be cut and pasted
and placed in here. After you change this. But, since you're using
by country instead of by description item: you just need to change this.
We could run that. And then you notice, okay
this is plotting pretty nicely. But, it's doing it in alphabetical order.
Maybe you don't want that. Maybe you want this in ascending or descending order
for each one of these bars. So we can do an adjustment to this. You just cut
and paste everything else. But you have an addendum here,
okay. And that's this line right here. You're taking in and you're
reordering. And if you notice this we're doing
descending order, all right. We're taking in our
category, and our category here.. let me zoom out some; is these.
Alright, that's the only thing you do different. So you can reorder it
in descending order, okay. And everything else is the same
cut and paste. I would like to say thank you for watching. Please like and
subscribe, And if you subscribe turn on that notification bell. I hope this
brought utility to someone. Thank you for watching: i'll see you in
the next video. Bye. Also i'm considering doing a small video
series for plotting in ggplot and possibly some
plotting videos using python. So let me know how you feel about. Thank
you