Your task was to create three histograms on one output
plot. And to do that you needed to install the grid
extra package and load it into our studio. Once you've got
that, now it's just a matter of creating each of the
three histograms. And saving them to three variables. I'll use p1,
p2 and p3 for each of the plots that I'm going to
create. Now the first histogram is the same as before. We
just have friend_count, pass to x, and pf, pass to data.
For the second histogram, we'll take the log10 of friend_count. And remember
to add 1 to it first, so that we don't get any
undefined results. And finally for our third histogram, we'll simply take the
square root of friend count. Now, if I run all these lines
of code, I can see that I create three new variables that
saves each of my plots. And now I just need to pass
each of these plots to grid dot arrange. And set end col
equal to 1, since I just want one column with all my histograms.
And running my code, I can see that I get all
three histograms. And here's a closer view of the three. Notice that
our second plot is much better. Since we have a normalish
kind of distribution. The square root transform
is also better than no transformation
at all, since we don't have as much of a long
tail. We do still have a long tail, it's just that much
lower friend count since we transformed the variable. The tilt's just
not as bad as before. Now there was another way to create
these same three histograms using a different type of syntax.
Now the syntax is a little more complicated at first. But
I want to give you a preview right now since you'll
see it in upcoming lessons. This time, we're going to use
the ggplot syntax to create our histogram. Ggplot is very
similar to the q plot function, since it still takes the
same parameters, x and data. The key difference is that we
need to include any x or y variables inside this aesthetic
wrapper or AES. The other thing we need to do is
we need to tell ggplot what type of plot we want to
create. Or what kind of geom we want. The geom that we
want is geom histogram. Running this bit of code I can see
that I get my original histogram. Which is the first one we
created on friend count. Running the entire line I can save this
histogram into p1. Now I've got a plot that I can just
alter using scales. So for the second plot, I'm going to take my
first plot and just add a scale_x_log10 to it. This
is going to transform the x-axis, or the x variable, using log
base 10. Similarly, for the third graph, I'm going to add scale
x square root. Now, I don't want to use p2 here, since
I've already got a scale log 10 on it. I
want to use my original graph, p1. And add scale x square
root. Now saving all these plots and writing our grid dot
arrange function, we get the same output as we had before.
Now there is a slight difference here based on the
x-axis labeling. And I want you to see if you
can figure that out. If you're not sure of the
differences, don't worry. I'll explain it in the next video.