Welcome everybody, you're watching Mr. Fugu Data Science.
We're doing a continuation of our online sales data project. Today, we will do some
formatting of date/time and do multiple figures of ggplot2.
As always, the code is in the link in the description below. You need to import
libraries knitter, markdown, tidy verse, GG plot two. Here's what our data look like.
Of interest today; is the country column unit price, the invoice date for
manipulation and quantity. We need to do some formatting of our invoice date.
Since, it's stored as a character currently. We take the formatting of how
it's set up as month/day/year hour and minute and we use the function called
STRP time, strip time. Here's what it looks like. From there we use a function
called POSIX CT, POS I X C T. And what you're doing is you're converting an
object to a class. That's what this is right here. So then what we need to do is
call our data that we just formatted here, and I'm extracting the hour, date
month, year and then month year together. Now, I decided I wanted to order
this. So I could print it out and show you what it looks like real quick. I
thought this would be helpful for the plots i'll show below. But I'll show you
where there is a hiccup. Now, what I needed to do: is convert my month from a
numeric into the actual abbreviation name. There's a stored procedure that's
called month dot ABB. So basically your month abbreviation. So I had to convert
my character into a numeric for that column. If you see right here, and then
run the name abbreviation. Then store it as a new column. I did this month
underscore abbreviation, which is this right here.
From there, we need to do a little processing. So what I did was I did a
group by, and I called the country in the quantity from our data that is extracted.
And here if you remember, we took out all the canceled orders. We got rid of those
so that's what this data is now. I stored it as a data frame. where I'm calling the
new columns country and quantity, and I'm doing an aggregate. Where I'm taking these
two columns and I'm summing the quantity based on each individual country. And
that's what you see here. So we have multiple rows of each country with a
quantity and I lump them all together into one. So we can do some plotting. For
our first plot I am calling our ordered frequencies. That I just stored above, and
I have to call it reorder. And the reason I have to call this is because, when you
print this out: this right here will print out relative to the names in some
kind of alphabetical order. Instead of by descending quantities. So then I said
reorder country and quantity for my x and y it's just my quantity. Then I call
this Geom bar, uses that identity and then I flip my coordinates. So that's why
it's stacked like this. From there I had to increase my text size; using this
parameter like this increases everything. All the text at once, instead of each
individual layer. I wanted to get rid of all of my quantities, which were stored
as scientific notation and that's what I did right here. And then I just called my
label names and printed it off. You could also store these and save these plots
which I will do later for my final write-up for this paper.
Okay, so here we are, and if you notice it's
kind of an issue because the dominating factor is the United Kingdom, here and it
basically Dwarfs any of our other data. So we can't get a real depiction of
what's going on. So what I did was create a new graph, and just exclude the country
column of anything with United Kingdom. And ran the same code again. Let's
just cut and paste with this new line here that was subsetting our data and
then you just call the new data here and that's what this prints off.
Then what we can do is say we just looked at countries by quantity sold of
items. Let' s look at currency. From there we can do apply instead of buy quantity
we can look at buy British pound. So basically the dollar amount was sold
through each country. We'll do the same thing here but, we need to get the dollar
amounts or British pounds I should say. We need to create a new variable. So we
need the total that was sold which is the unit price times the quantity. And
I'm using a general function that's called transform. It's taking my data and
I'm creating a new column called total. From there I'm doing a group by the
country and our total. Storing it as group sales throwing that into a new
data frame with the columns called country and totals, where I will now
aggregate my total and the country and sum the values. Just like we did above in
the last example. So we're taking our country here; each time you see each one
of those countries add it together. So that's what you end up here. And I just
showed you visually. I just it an arrange and then descending order. So
the plotting is pretty much the same. Where you're doing your new data. But,
you're throwing in there and you're still doing the reorder again. But,
instead of quantity just throw in your totals here and that's the only thing
that's different. So as you notice here looks almost the same plot that we had.
And you say oh no what's going on? Is there a mistake ?Well no, as an artifact
a lot of the items are around $1 or 1 pound. So even though you start
multiplying my quantities, that's why this plot looks very similar. So we need
to negate this United Kingdom once again. So do the same thing as subset our data
and throw in the exact same information into our plot with a new subset data
here. And just remember to put totals here instead of quantities, and you're
good to go. And it's the same thing for your sales. So say ok this is very
informative what else can we do? We could look at sales by month, and this
gets a little interesting. So I create a table from the column that I stored the
dates for month abbreviations for all January, February, March, whatever. And I
have to call this new vector here and put it in this order. Because, if you
print it off without storing it as a factor here. It won't put it in the order
let you think. Even though the column name will be in the same order.
Let's go to the top and I'll show you again to refresh to refresh real quick.
Remember I put these in order right. And you say well what's going on then? Okay,
well let's go back. When you plot this what happens is; it'll plot it based on
not the order of how the month actually goes. But it'll go on they like an
alphabetical situation which is an issue. So I create a table. Because, it's
basically doing. What I'm doing is basically creating this table that
allows me to get the frequencies by each month and store that. Then I'm converting
it into a data frame. And I'm calling the first column which will get stored as
var1 renaming it as month. I create this factor; where I'm taking the month and my
new ordered columns or label names and now the frequency. And just print this off
as usual. And here we go, it's in the correct order. So that's one thing to
note. Then to plot by a day of the month. We do almost the same thing we did
before. We're taking the day of each month storing it as a table. The table
creates a frequency count for each day. Store that as a data frame. Your first
column gets stored as variable one. You rename it to what you would like. Your
other column gets stored as a frequency. You call your data in where you have
your x-axis y-axis and your data. And then what I did here was create spacing
between each one of these bars. And depending on how you set this up, the
smaller this number goes the smaller each one of these bars is. The larger
goes the larger it is. And then depending on how big your text is or how
close they are together you should do a rotation and that's what I did here.
Which allows me to rotate the axes of my data. So you can see that by each day of
the month this is the frequency count or quantity that was sold, okay.
I do the same thing for hourly sales. Where it looks like the very exact same
thing. Just repeated cut and paste for hours. Restore a new variable and plug
and chug the same stuff that you do. You just changed the X and this variable
name and everything else is the same. And then you just change this and this. So we
see what's going on by hour so obviously from lunchtime till 2:00 in the
afternoon are the most sales and people are going
on the internet looking for things to purchase during the lunch break. We could
do also plotting by the day of the week. But, here you have to do another
formatting. So since we had just the regular dates before. Now, you use this
special operator for your formatting and don't print out Monday, Tuesday Wednesday,
Thursday, etc. If it's giving a date like 1/20/2010/ it'll know where to find it.
So then I store that as a table. Get my frequency counts by each day. Create a
data frame as usual. Then I call it just day of the week. Then I have to also do
my factor situation. So I get these in the correct order. If you know this
company didn't have sales on Saturdays. And then everything else is the same and
you just plug in your level. Which is your vector of Monday through Friday
plus Sunday and put everything else and you're good to go. If you notice we have
everything put together correctly lastly we have our month and year put
together. And I have to do the same thing here. Where we need to call a factor, we
need to call a factor and store that in here. And as you see everything else once
again is the same. Except, I needed to do an additional step here. I needed to call
it the tick marks and change the sizing of the tick marks, all right.
And I also had to do a rotation again of my text. Because, it was kind of jumbled
around, all right. And you'll see left from December 2010
to December 2011 by month and year. Also, I could show you something real quick. I
did this example last time where I created a range of prices from less than
a dollar to a dollar, one to three dollars, three to five dollars, five to
ten dollars, greater than ten dollars. Alright, you could do that with nested
if-else statements. With the second statement instead of having less than a
dollar you through another if-else statement in there. Then at the end you have
the last two statements. If it's greater than 5 and less than 10 then it's 5 to
10 otherwise it's over 10. Then we take
that price range column, create a table get our frequencies stored as a data
frame. Create a new labelled column name then we need to do a vector of our names
so they're in order. Use the factor again do everything else. As well as change
tics and change the rotation of our labels and there we go. Like I said
before the reason the quantity sold versus the prices of items that were
sold by country looked the same; was because you a lot of items that are
between one and three dollars. If you did market this and put it you'll notice
that the mean of the items is around one and a quarter dollar, or one and
a half dollars something like that for the average price per item. Or the mode?
Might have been the mode; can't remember. Nevertheless, went kind of fast because I
wanted you to see multiple steps so you could do this at home and pause the
video and just copy the code and get an idea. Thank you for watching. I greatly
appreciate it and throw a like if you thought this was useful content. Please,
subscribe and turn on that notification bell and leave a comment to tell me what
you thought or how I could improve this channel for all of us. As always. I'll see
you in the next video. Bye