 In this tutorial I want to show you how to do at least how to create histograms using Plotli for R. So we're inside of R Studio here. I've created a new markdown file called histograms. It lives in a folder on my hard drive. So when we do the little setup here at the top, I am setting the working directory to get the working directory. It is where it stays on my hard drive. I'm also seeding the pseudo random number generator and we're going to use two libraries. One is Plotli, of course, and the other one Dplyr. D-P-L-Y-R. First thing here you can see inside of a markdown file I can create a little style sheet, a cascading style sheet which just colors the headings for me. Heading 1, Heading 2 and Heading 3. They all get a different color. The Heading 3, that color is actually exactly the same. Let's complete that by putting in the semicolons and then I'm also just introducing a PNG file. So that's going to be a picture that lives on the top of this HTML file or PDF file or Word document, whatever the export is that you're going to use. So the introduction, what is a histogram all about? Well, it visualizes the spread of the data point values for a continuous numerical variable. So what it does is it looks like it looks at the minimum value and the maximum value for that random variable and it divvies up that range into equal sizes and then it discounts how many of the data point values are actually inside of each of these little sections called bins. Now you can also normalize it so it doesn't discount but it actually just gives you the frequency distribution and we're going to have a look at that. So let's create a simple histogram. So we're going to create here a computer variable called WCC for white cell count for instance and it's going to be 200 data point values from a normal distribution with a mean of 15 and a standard deviation of 4. So you can see what we've done here with these code chunks. You can actually name the code chunk there so that when you go to the bottom here and you look at your markdown file on the left hand side you see the top level. There we go and we can see the chunks here actually it shows their names and just gives you an easy way to navigate or someone else who gets your file just to navigate to where they want to be. So these black ones that will be the headings that I use there with the two hashtag signs so that we're heading to. You can see all of them they're neatly done and all the chunks. It's very easy to navigate for someone else or yourself but later to navigate this document. So we're creating the random variable here. Let's go and it runs and we see the median there is 15 mean 15.3 maximum 27 minimum 6 and let's create our first histogram. So I'm going to call it p1 a computer variable p1. It's plot underscore ly and I'm passing it an x variable. Remember it's just on the x-axis it's a numerical variable that's going to be divvied up and you have to use the still design in front of your computer variable there. So it's the still design which I'll count and most importantly I'm going to say type equals histogram to create this histogram. Let's run this and see what it looks like. So there we go we've got an interactive histogram. If I hover over it as per usual with the plotly you can see what is going on how many there are and the right hand side of each of these bin counts. So it's a little bland and bare so let's add a title. We're going to use the layout command in this pipeline sort of notation. So I'm adding titles here. X is still white cell count type of histogram. My pipe operator there, command there at least and then layout. I'm going to give it a title and x-axis and a y-axis. The title again just is a string there and on the x-axis I can pass the list so that I have these key value pairs. I have a key of title and a value of white cell count and a key of zero line and a value of false. So the titles is going to set the x-axis title and the zero lines can remove this black lines these black lines that might appear on the x and y-axis naming the y-axis there as well. So let's have a look and there we go we can see the beautiful titles now histogram of white cell count. I can see count here on the y-axis and white cell count on the x-axis. So count I've specifically put that title there for the y-axis because it's counting how many fall inside of this bin. So there's five, there's six, there's five, there's 16, there's 26 in that bin. So let's just normalize that. Remember the normalization is just going to divide the count in that bin divided by the total number which in our case is 200 and give you a frequency distribution. And we're going to achieve that by just adding the hist norm argument here to the plotly function that we have here. So hist norm equals probability in quotation marks there that's all we've added and now I've also changed the y-axis title there to frequency just to so we show that is now a frequency this so this becomes this histogram becomes a frequency distribution. So you can see what fraction of values lie inside of each of these bins. Now to do a horizontal histogram that's quite easy all we're going to do is we're just going to flip things around instead of saying x equals we say y equals. So as y equals the white cell count and remember that you just have to flip your axis names then as well in the layout because you are flipping this around let's run this code chunk and let's see what happens and there we go beautiful we have white cell count now on the y-axis and the frequency on the x-axis so a horizontal histogram. Lastly what do you do if you want to display more than one categorical group and that is really what the interactive widgets are all about. We can put a lot of data inside of a graph and then we can interact with that graph. So let's create correct the spelling there creating data frames and I'm going to create a data frame called df it's going to have two columns two variables group and white cell count to the white cell count I'm just passing the 200 values that were in there but the group I'm taking a sample of selecting from a and b 200 and replacing is 2 so this is going to take a b a b etc b b b a whatever the random value selects and then there are more than one ways to go about this but just to remind ourselves of deep layer I'm going to create group a and a group b these are two separate data frames I'm using the falter verb and I'm only selecting values that have a in the group column and b in the group column for df so I have two separate data frames now as I said there are more ways of going about this but I think just a nice little reminder of how to use the falter verb inside of deep layer so let's do that now if you overlay two histograms on top of each other of course the front one's going to hide what happens behind so we've got to introduce some opacity so the way that we're going to do that is to say plot underscore and first thing we do is just to pass an alpha value of 0.7 so that's 70 opacity and then with this pipe command we're going to start adding things so we add a histogram and that histogram has an x-axis which remember the tilde symbol here is group a dollar white cell count so that's the white cell count only of our group a patients and this time I'm going to give it a name group a so when the legend appears on the right hand side so we can see what colors what what represents what adding my pipe there and then adding another histogram again the tilde sign this time group b white cell count I'm naming it group b and most importantly under the layout there's a new argument called bar mode and this time we set it to overlay everything else been exactly the same let's have a quick look at what this last histogram is going to look like very nice we can see the two values there and I can hide both of them this is interactive as you can see and I can hide group b as well and then bring them back selectively whichever one I want to see I only want to have a look at group b or I might only want to have a look at group a or I might want to have a look at both so there you go a histogram so let's have a quick look what it's what it looks like when we do knit this and publish it to our pubs here we go in our pubs and we can see the title there and this is the image that was imported and you can see the little cascading style sheet giving us this dark blue this royal blue and this gold sort of color for h2 so this colorizes and just changes the look of this html document a bit and we can see how nicely these graphs are plotting inside of a web an html file we can scroll down all the way to this one that's going to be interactive and we can see group a group b and bring them back selectively as we wish so still interactive as an html file so there you go I hope you enjoyed this tutorial and I hope that you use plotly to create some interactive widgets as plots using r