Hey guys, in this video I wanted to go over how to create
a word cloud in R. I'm basically going to go over
this short R code and before I get into the R code,
let me show you what the word cloud we're going to create looks like.
A word cloud basically looks like this. It looks like a cloud of
words and the words are different colors and different
sizes based on the frequency that they appear
in a data set that you're going to pass in.
For this particular video, I have different
community areas in Chicago. This is an old data set that I have
used in previous videos, so I'm just reusing it since i already have it
and it was from a gun violence video. So basically from this you could
interpret for example that the community of
Austin in Chicago has more gun violence incidents compared to
some of these communities because the community
name Austin is bigger. I'm going to go over
how we can create a word cloud like this in R. Let's go ahead and get started
and I will put a link to the R code in the description box below.
The first line of code just sets my working directory.
This is where I'm going to output the image
file. We're going to save the file as a PNG and I'm just going to set
the working directory folder for where I want the file to be placed at the end.
Then I'm going to load in the following R
libraries. If you don't have these libraries installed yet, you first want
to install these packages and then load in these libraries.
I'm going to load in the data table library to read
in a data frame or data set that i want to work with.
I'm going to load in dplyr to do some manipulation on the data set that I'm
loading in. I'm going to load in a library called
grDevices and this is going to help me save my image as a PNG. The actual
R package for creating word clouds is called wordcloud.
And then to get certain colors that I'm going to use
in my word cloud, I'm going to also load in a package
called Rcolorbrewer. I'm just going to go ahead and
load in all of these libraries with these lines of code.
And then let's get into importing the data set.
In this line of code I'm importing a CSV file that's going to have the data
table that I want to use-- whose values I want to use--in the word
cloud. In this particular word cloud example, we're going to want to have
two columns in our data table. We want to have a column of
words and then the second column should be frequencies.
That could be from whatever data set but basically you want to have at least
these two columns and basically the sizes of the words in the word cloud are
going to be determined by the frequency in our data
table. From this CSV, which has multiple
variables in it, I'm just going to select two and
basically do some cleanup of the data table.
I'm going to do that with the next couple lines of code.
I'm basically going to get two columns. I'm going to rank the communities by gun
violence incidents and I'm going to select the top 70
communities out of 77 in Chicago and basically put
that in a variable called
chi_table1. If I look at this final data table, it's going to have
two columns, like I said. It's going to have 70 rows.
These are 70 out of the 77 different communities in Chicago
and each row also has a column for incidents. This is going to be the
number of gun violence incidents in a certain period of time. I think
it was a couple of months-- I think it was I guess six months--I
don't remember though but whatever the data set it basically has
a column of words and then a column of numbers, in this case frequency.
You can see that when it's ranked, the number one community again is
Austin. So we're going to expect that in the word cloud
Austin is going to be the biggest and the size of the words
should decrease as the number of incidents per community decreases.
We would expect words like Oakland or Mckinley Park which only have one
incident to be very small in our word cloud as
well. Once we have our data table ready,
we're going to set the output as a PNG file in this
case and we're going to actually use the word cloud function from the word
cloud R package. This line of code says
to save the file--the image that we're going to create--
as wordcloud.png. It's going to save it in
the folder that I already set. And then I specify the height,
the width that I want in inches, and a resolution of 300. I'm just going to
set that for the output that I'm going to create
and then this next line of code is what's going to actually produce the
word cloud. I'm going to pass in the two columns from the data table.
I'm going to pass in the column of the
words, which is going to be in the community column. The community has
all the words that I want plotted. And then the
frequency parameter, I'm going to pass in the number of incidents, so
that's the second column. The other parameters in this function
are basically optional parameters for how
visually you want the word cloud to look. If you look up
question mark wordcloud, you can see information about what these optional
parameters mean. The scale for example indicates the
range of the sizes of the words. So I want them to be from
3.5 to 0.25 relative. The maximum number of words is going to
be 70 and then the colors is going to determine the color
palette that I want the different words to be. In this particular case,
I'm getting this from the R package Rcolorbrewer.
And then if I go to this website which has some information about
Rcolorbrewer palette, and I will put a link in the description box below,
you see that you have using this R package a couple different
color schemes or color palettes that you can pick from.
When I ran this I picked Dark2. So the word cloud, I'm going to have words
with these colors from this color palette but basically
you can just pass in any of these names and then it will have
the appropriate color palette using this R package. You can see I put for
colors parameter in wordcloud brewer.pal
and then that took two values dark2, which is the color palette, and then
dark2 has eight possible different colors for the words.
I just said to use eight, which is the maximum number of different colors.
Then when I finally run this line of code it's going to
produce the word cloud in my working directory. Then if I go
to my working directory, which is here, I'm going to find wordcloud.png
in the folder and if I click it, then I have
my word cloud which was created. So you see
that according to the frequencies corresponding to your data table,
you have different colors and different sizes for the different words
arranged in a cloud. And that's it for this video and thank you
guys for watching~!
*chiptune music*