WHT

Barplot | Barplot In R (8 Examples) | How To Create Barchart & Bargraph In Rstudio | Stacked, Grouped & Legend

Statistics Globe

Subscribe Here

Likes

113

Views

12,284

Barplot In R (8 Examples) | How To Create Barchart & Bargraph In Rstudio | Stacked, Grouped & Legend

Transcript:

Hi, this is urine for statistics. Bochum and in this video, I’m going to show you how to create bar plots in the R programming language. So in the video, I’m going to use some example data that we can create here in line two of the code. So if you run this code, you will see that a new data object appears here at the top right and this data object is simply containing five numeric values, which are representing the head of our bus now. If we want to draw a bar plot in the R programming language, then we can use the bar plot function as you can see here in line 5 of the code. The bar plot function is already provided by the basic installation of the R programming language, So we don’t need to install any packages for the function and we can apply the function simply by specifying our values within the bar plot function. So if you run this line of code, you can see that here at the bottom. Right of our studio. A new bar blood appears with five different bars. Now you can also see that this bar. Plot here is relatively plain and simple, so for that reason. I will show you in the following examples. How to modify this bar plot and how to make this bar plot a little bit prettier, so the first thing. I want to show you here. In example, 2 is how to add colors to the bar plot, and this is something we can do with a Col argument that you can see here in line 9 of the code and for the call argument, we can specify basically any hex color code, or there are also predefined colors that you can use in R. So in this case, I’m using the this hex color code here, and if you run this line of code, you can see that now. Our bars are colored in blue, so in the next example. I want to show you how to create a horizontal bar and this is something we can specify as you can see here in line Third of the code by specifying the Horus argument to be equal to true. And if you run these lines of code, you can see that now. Our bar plot is represented with horizontal alignment. You may already have noticed that our bar plots that we have created so far. Don’t have any labels for the boss, and this, of course, makes it very difficult for the reader to interpret this bar plot as you can see it right now. And for that reason, it makes a lot of sense to create another vector, which is containing our labels of the bar plot So here in this case as you can see in line 16 of the code. I’m simply creating labels from A to E. And now if you want to draw these labels below the bars of our bar plot, we can specify the names like argument as you can see here in line 18 of the code to be equal to this vector, which contains our labels of the box. So if we run line 17 and 18 of the code, you can see that now below our bars. We can see our labels, A B C D and E So another thing I want to show you is how to create a stacked bar chart and this is what we are going to do here. In life in Lines, 21 words, in example, 5 of the code. And if we want to create a stacked bar chart, we need to create a matrix as you can see here. In lines 21 to 26 of the code and this matrix needs Contae needs to contain columns, which are specifying each value of each subtool for the stacked bar chart. So let’s run this code and then we can have a look at this data how this data should look like so after running line 27 of the code, You can see that our matrix appears here at the bottom of our studio in the our studio console and you can see that we have created a matrix with five columns. Abcd and E and each of this. These columns contains two rows, one row for group one and another row for group two. And now we can simply specify within our bar plot this matrix as input. And then it also makes a lot of sense to specify two colors because now we have two rows so we would need two different colors for our stake to publish now. If you run these lines of code, you can see that our staked bar plot is already created here at the bottom, right, however, we don’t have a legend yet, and, of course it makes a lot of sense to add a legend to such a stacked bar plot because otherwise the reader will not know which of the colors is representing which group. Yeah, and that is something we can do here in lines 32 to 34 of the code by applying the legend function and within the legend function, we have to specify at which position We want to draw our legend, so in this case. I want to draw it at the top right then. We need to specify the labels or the names of our legend in this case. Group one and two two. And then we also want to specify for beach color, which group or which label stands. So in this case, Group one is shown with this color code and group Two is shown with this color code. So if you run these three lines of code, you will see that a legend appears here at the top right of our bar plot. Yes, oh, another thing. I want to show you is relatively similar compared to a stacked bar plot and this is a curved bar plot. And this is what we are going to do here. The next example, in example, six, starting at line 36 of the code and here in this case, we are doing exactly the same as in the previous example, but we are specifying the beside argument to be equal to true. And if we do that, then you can see that now. The bars are stacked above each other, but they are represented side-by-side in groups for each of the categories that we have and also here, Of course, it makes a lot of sense to edit that term to the plot so here we can simply apply the same code as we did already, in example 5 and then you can see a legend representing our troops is appearing at the top right of our body. OK, so far, we have only used the bar plot function of the basic installation of the our programming language, but as you might know, the our programming language is providing many different packages for the creation of plots and the most popular package for creation of plots is the ggplot2 package and in the following example. I’m going to show you how to create a bar plot with the ggplot2 package, So as first step, we need to install and load this add-on package to our our studio, and that is what we can do here in line 46 and 47 of the code. I have installed the Ggplot2 package already, so I’m just going to load it here with line 47 and after running this code, We also need to specify a data frame, which contains all our values and groups because the ggplot2 package only takes data frames as input. So this is what we are going to do here in line 49 of the code. You can also have a look at this data and here at the bottom in the our studio console. You can see that the data simply contains our true variable that we have created in the beginning, as well as the value variable that we have created in the beginning. So now after creating this data set, we can run the ggplot function as you can see here in line, 51 of the code, plus the game bar function and this function here actually is specifying that we are creating a bar plot based on our data. So if you run these two lines of code, then you can see a new bar. Plot is appearing here The bottom right and this bar plot is containing the same values as before, but this time the bar plot is shown in this typical ggplot2 design that you may have already seen before. Yes, so another picket That is also very well known for the duration of bar. Plots is the locally package that I want to show you here. In example, 8 of the code again, we can install and load the package here with the install packages and the library functions. I have installed this package too. So I’m just going to load it here. In 956 of the code and then within the blockly package, the plot ly function is included and we can apply the plot LY function as you can see here in lines 58 to 60 of the code, so within the function. We need to specify our truth. This is the grouping very variable that we have created in the beginning. Then we need to specify our values, and then we also need to specify that we want to create a bar chart and this is something we can do by specifying the type argument to be equal to bar. So if you run these three lines of code, you will see that a new bar chart appears here at the bottom, right and what you also can see is that this bar chart is created in the viewer window and not in the plot window as before. So this has some Froese, but also cons so for example. If you would like to export your data, you would have to do it a little bit differently, so I think it’s also a bit a matter of taste. Which package or which function you prefer for the creation of Bob. Lots, however, as we have seen in this video, there are many alternatives for the creation of bar charts in the R programming language. Yeah, so that’s what I wanted to show you in this video. However, if you would like to learn more about the creation of bar traffics in the R programming language. Then you could check out my home Page statistics blog, calm because on the home page, I have recently published a tutorial on the duration of bar charts, and in this tutorial, I’m explaining the R code of this video in somewhat detail. I will put a link to this tutorial in the description of this video. So you could check it out there. Also, you will find all the R code that I have used in this video in the description of this video. So you could copy/paste from there, and if you have liked the video, I would be very happy if you leave me a comment and don’t forget to subscribe to my Youtube channel in order to get notifications about videos that I’m releasing in future. That’s it for this video. Thanks a lot, see you next time. Bye bye!

What Is P | What Is A P-value? – Clearly Explained

Transcript: You have probably heard scientists quote p-values whenever they report the results from their experiment. But what exactly is a p-value anyway, in this video? I would clearly explain what a p-value is. A p-value is an abbreviation for probability value and...

read more

What Are Logits | Logit And Softmax In Deep Learning

Transcript: Hello, everyone, let's quickly. Go over! What is loaded and softmax? If you are working on the tip loading model for the multi-clas's classification, you might heard about the softmax and the logit and here is the example we have the input. Which is this...

read more