 It's LinkedIn Learning author Monica Wahee with today's data science makeover. Watch while Monica Wahee demonstrates making a Likert scale plot in R. Hi everyone, see this sexy plot? It's at the top of a blog post I made about how to make these sexy plots, which I will link you to at the bottom of this video. Also you can get the code and this example data I'm using from GitHub. There's a link for that too. This is a Likert scale plot. So pretend you did a survey and asked a bunch of Likert scale items and you got responses of strongly disagree, somewhat disagree, neither agree nor disagree, somewhat agree and strongly agree. How do you visualize that? Well this is how I do it in R with the Likert package. So each horizontal bar represents an item. See on the right where it's green? This shows the proportion of strongly agree and somewhat agree answers. And the left shows the disagree answers. The somewhats are lighter colored than the stronglies. And here is the proportion of gray neutrals in the middle. Now I wrote a whole blog post on how to get this plot to look exactly right. It has nine steps and I call it a hack because you have to kind of do acrobatics to get it to look really good. So now let's go through each step together in R. Okay let's start at step one. This is just to read in our fake dataset. So let's run this. Okay before we go to step two, let me say something. The class of the variable in R that you use for Likert scale items, which are ordinal items, is factor. We don't have factor in SAS. That's specific to R. So R sees a factor variable as a categorical variable. So whatever value you store in it, it thinks of it as a code. So if you store a one as a factor, R thinks it's a code for something, like yes. So it's not ever going to do one plus one if it's a factor. You would have to change it into numeric or integer or some non-categorical class. In R, if you have a factor variable, you can assign a label to each level of the variable. This is similar to using PROC format in SAS. So you could attach character descriptions to each level of a categorical variable. Now one of the reasons you do that in SAS is because you want the values of those formats to come out on a report or a plot you want to print. And that's exactly why we are doing it in this operation. We want to label the levels so they come out labeled on the Likert plot. Oh, I forgot. I have to tell you another thing. If you have missing levels in any item, then you need to do this Cluj. I'm going to show you to a P's R. Let's say you have a satisfaction survey and there is an item about something everybody loved. So they all either said four, somewhat agree, or five strongly agree. That's going to give our problems when you try to set five levels because it's only going to see those two. So here's the Cluj. First we make fake rows of data that represent all values of the Likert item. Then we attach the levels so that R sees five levels. And then before we actually make the plot, we quick remove the fake data. We keep track of which rows we added that are fake by identifying them with a unique set of ID numbers. I'll show you. So here's us doing steps two and three, designing and making the fake data we are going to attach and then remove. I want to show you how I make this data frame. My source data has five columns, Q1, Q2, Q3, Q4, and Q5. Oh, and also a study ID column. And my Likert item has five levels, strongly disagree, somewhat disagree, neither agree nor disagree, somewhat agree, and strongly agree. So the idea is to make five fake people who answer each of these questions with a level. So we have a person who answers all the questions with a one strongly disagree, probably a very negative person. And then we have another person who is the two disagree person and so on. You'll see I made a bunch of vectors to do this. Each vector is a column, and then I'm going to bind those vectors together as columns into a data frame. Here's my first column, study ID. This is where I control which special IDs I assign. So I remember these rows are fake people, and I should remove them later. Then I have a column for each item, Q1 through Q5, which I make vectors for. You will see that they say one, two, three, four, five, because when I bind them together into a data frame, then you'll have a one person, a two person, and so on. And here's the last line. Here's where I use the data dot frame command to bind the vectors together into a data set I called fake. Let's run all this. Okay, now we made a little fake data set. Let's move on to step four. Oh, one thing I forgot to tell you is that all these variables are not in factor format. If you use the class command, you'll see that Q1 and actually all the variables, both our fake data and the data set we read in, they're all integers. So in the step step four, we convert them to factor. Let's look at the code. In the first line, we take the data we read in, which we called survey one, and we are bind it, meaning we row bind it to the fake data we just made and make a new data set called survey two. Next, we create a set of new variables named with the suffix underscore f, which are in factor format by using the as dot factor command on the original variables. So now we have Q1 underscore f, Q2 underscore f, and so on. And those are in factor format. Let's run this. Okay, now on to step five. Okay, here is where we attach the factor levels. So first we make a vector with the labels for each level. And we call the vector factor underscore levels. Since our data are coded one through five, we just put the levels in the same order as the factor. So strongly disagree up to strongly agree. Now we take each of the factor variables, and we use the levels command to shove the factor underscore levels vector into the variable. And that's how the levels get on. Let's run it. Okay, now we start to prepare our data frame for being plotted. First, in step six, we remove the rows we don't want. See, this is us sub-setting the survey two data set into survey three, leaving off those fake rows we attached. I use the n row command to verify the rows are actually dropped. Let's scroll down so we can see step seven code. For the like it plot, you want to only include the columns you intend to plot in your data set. It just makes it easier. So this code transforms data frame survey three into survey four and drops the study ID column and all the integer formatted columns we are not going to use. I use the call name command to verify the columns actually got dropped. Okay, let's run code from step six and seven. Now in step eight, we replace the column headings with the wording from the actual items in the survey. This is something you really cannot do in SAS because you have a lot of field name limitations. But in R, you can actually rename columns in this data frame with names that are entire sentences. I'll show you how. This first line creates a vector called var headings, which stands for variable headings. The idea is we are going to use this vector to replace the column headings. You'll see the goofy item names I made up. My hair is too long. What the heck? My hair is never too long. There is no way to have my hair get too long. Okay, in the next line, we use the names command on the data frame survey four to apply the vector var headings to rename the columns, and we run the call names command to verify the operation. Let's run the code. Okay, finally, it's time for the plot. Here's step nine code. You'll see on the first line we call the library Lycurt. Then on the next line, we use the command Lycurt on the entire data frame called survey four to create a Lycurt object we call p. Then on the next line, we use some ggplot2 like commands to format the plot. We start by using the lycurt.bar.plot command, and then we create code using ggplot2 syntax to add different formatting. We format the legend, the size of the text, the size of the axis text, the legend text, and finally, I choose the classic theme. This command transforms object p into object a. And then finally, we plot a. Let's run all this and make the plot. And here we are, the finished plot. Beautiful. So after this, you have no excuse. Whenever you have Lycurt data, you know how to give it a makeover and make it gorgeous.