 So this is the second video on histograms using plotly an R Now I've had a question or two just about controlling the color of our histogram And so this is in actual fact then part two. So what we can see here is my rpubs repository rpubs.com Ford slash there's juan and then each copper and you'll find all my Documents here. Remember they were created in our studio. Saved is RMD files. That's our markdown They were then knitted as HTML and upload it You can also download the actual files here on my github repository. Remember, that's John copper That's without the H and you'll see a lot of my repositories here The plotly for R is where you'll find this growing list of files download them So you can play with them on your own system If you don't know how to clone a get-up repository, remember you can always just download and the zip file and then Unzip that and all these files will be there for you So let's have a look at this HTML rendered file created in our studio Let's just run through just quickly catch up again. Remember simple histogram All we did here was just to create a computer variable called WCC It was from a normal distribution 100 values with a mean of 15 a standard deviation of 4 and we created this histogram We called our first plot P1 plot underscore L y That is the function for creating plots and plotly and I said x equals and then you always remember that little Till the symbol WCC and then the type a histogram and very night neatly We'll see a histogram here. Remember histograms are for numerical variables and it's going to burn the numerical variables So it's going to create These little sections. It's going to create a bun. So if I hover over this top one, you see that 12 to 13 point 999 so The numerical variable it counts how many values fell between 12 and 13 point 999 and then they from 14 to 15 999 it's just rounding that off It just means 16 is not included and there it goes from 16 so 16 is included in this bun then up to 17 point 999 So it's very easy to understand the histogram and you can see that this was 100 values taken from a normal distribution. I showed you how to add title axes and title and axis labels at least and for that we just have this Percentage greater than and percentage symbol there. That just means we piping This first function into the second one. The second part is a layout I've got three keyword arguments here title x-axis and y-axis for the x-axis We just have a string histogram of white cell count that gives us this nice little title up here and the x-axis I want a couple of things to happen So I'm passing all those keyword arguments as a list So the list has a title and a zero line the zero line is just these black lines that are drawn and Sometimes they appear sometimes they don't I don't like them very much So we put that to false But the title is white cell count on the x-axis and the y-axis that white cell count and count on the x-axis I showed you how to normalize the histogram. We've just added a his norm equals probability So it's just going to take the number that was in there So if we go up remember here There was 24 in there and the divide Divide said 24 by how many there are in total and total there were 100 values So that just gives us this 0.24. So 24% of the values fell there Change the histogram into a horizontal histogram very easy instead of saying x equals we just say y equals and Everything else stays exactly the same so to plot to Histograms in other words, we take any medical variable and we divide it into two in this instance You can divide them more than two by some categorical variable So we created a data frame data dot frame and we have two columns in there The first one's called group the other one's white cell count inside of the white cell count We just put the hundred values that we That we had created and then the group we use the sample function and it's going to choose between a and b it's going to create 200 of those and Replacement equals true. So 200 values not a hundred I meant and So it's going to have a b a b b a b etc And we can group these 200 white cell count values Those that belong to a and those that belong to be and what we've done here is just to create two new data frames one school Group a and other ones called group B and we can use the filter function, which is part of the deep liar library So DF we piping that or passing that as first argument to the filter function So we might as well have written filter and then the first argument would have been DF comma and then a Boolean question group equals Equals a so that's only the values that return true So the values that do contain contain a will make it into this group a data frame similarly for group B And then we have to plot them separately So Plotly, I've just put an alpha value there of 0.7. That's just a bit of transparency It's not normally how we do transparency, but I've put it in there And then I add separate histograms to this same plot see the pipe there. I'm piping that This plotly into both of these histograms. So X is the group a Dollar sign the white cell count the white cell counts I'm doing both of those separately giving them names so that we have this legend on the side Otherwise everything is exactly the same remember this is plotly so I can turn off group a turn on group a turn off group Be turn on group B very nice young plotly So let's change the colors the first way I'm going to show you is just to use the actual actual names of of colors in there a few colors that That do have names and you can use them directly So for p6 here we have plot underscore Lee We're going to have X equals the white cell count the type is a histogram the east norm is probability And then the marker Now if you see the word marker markers are the lines the actual dots for scatter plots these little rectangles For a histogram all of those those are markers And I want a few keyword arguments. Yes, I'm going to put them in a list for the markers One is the color and one is the line the color. I just wanted to be light gray And then the border because you see by default there's no borders in a plotly histogram But I can add borders saying line equals and I want two things with that line So I'm going to put them in a list So the color is going to be dark gray and the width is going to be two And then again I pipe that into a layout very nicely and this is what we get see the dark gray border the light gray interior and everything else the same it looks beautiful actually And uh, so just by adding some elements here keyword arguments to the marker Argument as simple as that. So what if we want to well, I mean, we can just do that individually remember I've got group a white cell count and group p a b white cell count e each to the ad histogram Remember, you could also say add underscore trace and then first argument or you can put it anyway because it's a keyword argument You would say type equals histogram. So no problem there add histogram works perfectly And now I can do that individually So the first one is going to be a teal color with a dark gray border width of two pixels And the second one is going to be orange with a dark gray border width of two and that is what it looks like And remember that I can always turn these off I can always turn group a off so I can only see group b there turn it back on Turn off group b so that I only see group a very beautiful Another way to do it is just to use rgb. That's red green and blue and rgba Which adds the opacity for us so we can control that So what I'm going to say is list Color equals and then instead of naming it are you using rgba? That's going to take four values the red value from 0 to 255 0 being none of that color whatsoever and 255 maximum brightness for that color So 255 for the red 165 for the green 0 for the blue and 1.0 So it's not going to be transparent at all. That's it shows the full color there for us And then the line just rgb without the a so i'm not putting any opacity in there So 169 169 169 that's a that's a grayish color So for the one on top the group b I'm adding rgba And i'm putting that as a 0.7 And again the line color i don't care about Opacity there so we just use rgb And very nicely there we get our plot and once again always I can turn off Any of the two or there's more than two I can turn them off so that I can just concentrate on the one that is left So very beautiful. You can really control the colors of these histograms. I've shown you two ways I plan to make a video where we really go in depth about into all the ways that you can control colors inside of plotly And I hope you look forward to that one. So that's it for adding color to your histograms We'll speak again in the next video