 So in this video I want to talk about all the ways or at least many of the ways that you can change colors of your plots and graphs and plotly Now we are here in our studio And I'm going to show you the actual code as it's put together in our markdown file remember the surrendered file is available on our pubs and This actual rmd file will be available on github as well So you can see they markup language at the top here just to instruct This coding to html what to do we see a title there author in the outputs going to be html and the table of content set to true So let's have a look at the libraries that we're going to use I'm going to use a table as always. I prefer that over data frame Just to print a data frame nicely to a web page I'm going to use the dt library of course plotly and then there are color brewer Now color brewer is a website that you can visit and then there is this wonderful package in R That can make use of those colors as always I do use some cascading style sheets just to color the heading one heading two and heading three levels. I Have a logo that I always insert in these art pubs documents and you can see there how I How I add that now remember I am using the set working directory get working directory Function I'm using up here and this rmd file in this png file live in exactly the same folder So that by default then as I mentioned plot there are some excellent colors and you can Customize them extensively and then if we add our color brewer package we can do even more So first of all, let's create some data that we can work with I'm going to just set the pseudo random number generator I'm going to see that with a integer one two three just so that if we rerun this code We get exactly the same random values back. I'm going to create a computer variable called cities And that is going to have a sample space of these one two three four five six seven cities And as I clip there spot the odd one out Anyway, I want 200 of those at random and replacement is true so that when one is drawn it goes That name gets goes back into the hat to be able to redraw So let's run all of our code. I'm going to run all the imports of my libraries there And then if we go down Let's create these random values off we go and we see on the right hand side under global environment I see that I have 200 values in there Now remember if we use the table function on the cities that is going to give me back At least to the screen this little table that just shows me the sample space values Boston Cape Town LA Miami New York City Seattle and San Francisco And the number the count of those unique values Amongst these 200 if I just use the names function Of on this table of cities. I'm just going to Save as a string vector then the names Boston Cape Town And let's store that in a computer variable called city dot names and you see I have my one two three four five six seven cities there And if I use the as dot numeric function on this table of the cities, then I'm only going to save The numbers the counts. So let's do that And if I do have those that makes it makes it easier to create a bar chart And remember bar chart just shows us these counts. So it's very good for a Categorical variable. So let's create it there And we see plot underscore ly And I'm going to add to that a trace and this trace type is a bar Remember all of this could have just gone into that function right there This is another way to do it on the x-axis is the city dot names Remember those are just the seven names and the y-axis are those counts I'm doing some layout by putting a title x-axis and the y-axis on the x-axis I want to list because I have both title and zero line both those keyword arguments And something else that you'll see here. I use fig dot cap as part of my r Notation up here and it just creates a nice little Caption for my figure if I render that out to html So there we go And we see the default deep sky blue color. It looks fantastic I can hover over those and see the counts. We can see la there 34 San Francisco set 34 So let's just change away from this deep sky blue default By using rgb and rgba Now rgb stands for red green and blue and that is how your television works How your computer screen works those red green and blue little dots on your screen And each of those can take up a value from zero. That's absolutely black to 255 Which is maximum intensity so 256 levels there And if we push the red all the way up to 255 and keep the others Dark those pixels you're going to see red and then you get these mixtures in between the a is for opacity So I can also if I use rgba add a fourth value And that ranges from zero to one and that has to do with a bit of transparency So let's do that. It's exactly the same plot But this time i'm going to use the marker keyword and i'm going to pass the list to that because I want to Use the color and the line keyword arguments as part of this marker keyword argument So the color i'm just going to use rgb. That's 195 195 195 So that's sort of a light grayish color or the values being the same There's not going to be a color cast no color dominates So that's just going to be on the spectrum of pitch black to white and gray levels in between So this is going to be a lighter gray The line as part of the marker tells Plotly what you want the border Of whatever you're trying to plot to be so that color i'm going to use rgb 2020 20 that's very low intensity. So that's going to appear very dark And I want a pixel pixel width for this border of two Everything else stays the same. Let's have a look at that And now we can see our lighter gray and our very dark gray almost black border to these So very easy to change the colors This might be more appropriate if you're going to submit it to a journal who wants monochrome images So let's just add a bit of opacity to that. So on the color here I'm going to use this 255 in the red channel nothing in green and nothing in blue But I used rgb a so I can put this fourth value 0.6. That's the opacity 0 meaning That they From 0 to 1 as I said and that is from being fully transparent at 0 to fully opaque at 1 So let's do a 0.6 there and you're going to see the red is going to be able to see through And there we go. So you can see those lines come through As far as the red is concerned Now I can Individualize each of these bars. I can give them each a value So my marker here again, I'm going to pass a list of values the color is what I'm after in this instance And I need to pass values for all seven of them So you can see two of them are a bit different And you've got to do it in the order that they did appear here that Vector of names that you created initially you have to follow that order. So let's have a look at this And there you go see there's no border that the color was the only keyword argument And here we can see the transparency at 0.7 follow them, but you can see these two the la and san francisco They were the highest at the highest count. So we colored them in red So you can really just individualize those colors Instead of using rgb in rgba We can actually use a list of named colors and if you go to this website of The worldwide web organization You can see a list of the color names that you can use and you can see we said here silver silver red silver silver silver red And then the opacity remember this one didn't have any opacity when we Right at the beginning Up there now i'm going to add some opacity And that opacity has to go on its own and i'm just repeating 0.77 times. So i have this vector Of 0.7 0.7 0.7 etc So let's have a look at that there we go. It looks exactly the same Those values are used were for silver and red and that's another way that we can do this by just using these These names of the colors Another way to go about it is just use hex colors hexadecimal color values also Hslu saturation and lightness and hsv color sets that you can use So you're using the hexadecimal color set And you see there i pass it as this vector of these strings And those are the values for red green and blue c0 c0 c0 and then ff 00 that ff Is maximum That's uh 2 to the power 4 16. So that's the maximum value there And if we print this out Again, there's just opacity there. I'm just repeating 0.77 times Again, exactly the same thing that we're going to get back Now we've also got some inbuilt color palettes that we can use First of all though, let's create a tipple and it's just going to hold all of these values for us So there's my df is my computer variable a pass a tipple city and sale So city is going to hold all my cities The 200 that we created before and then i'm just going to create another set of Values the length being the number of cities. So those are equal I'm going to round it to one digit and a mean of 100 and a standard deviation of 10 And then the data table function from the dt package And that's going to print it to the screen very nicely here You can see you can run through all of those you can even Go for ascending and descending order you can show how many entries you want to see at one time very nice the dt package So here we go. We're going to add this trace the data's df on the y-axis We want sale in on Color by city remember that there's nothing to do with color If you wanted colors that would be with an s So let's just have a look at this box plot And that is just going to give us these cities And then the sales on the y-axis as we asked and that sale is now going to just be individualized for each of the cities And that's the default we use there, but let's use one of the color sets You see the color sets for instance, they have set one set two set three Pastel one two and paired and dark to an accent And so those are the qualitative qualitative pellets you get many sequential. That's this light to dark pellets and You even get these dark to light to dark pellets as well And examples of those would be dark two and b r b g So let's just have a look at two of these. Here's the dark two So let's have a look at that And indeed you'll see every dark colors there And we're plotting out exactly the same thing but but by a certain palette And let's have a look at this brown green and beige Kind of colors you see goes from dark to light to dark So if you go to the colorbrewer website, you can certainly have a look and also look at things like color blindness, etc A lot for you to read and I've put a little of that From their webpage the other our colorbrewer package at least In here for you. You can also go to colorbrewer2.org and you can read more about these. So let's use that and One of the pellets is the paired pellet So the way that we're going to do that is say colors equals brewer dot p a l pal for pellet The number that I want is the length of the names of the tables of the cities Remember there were seven so that's going to give me seven colors back just using the length If I knew to a seven I could just have said brewer dot p a l and then seven comma What I want is the paired pellet. So let's have a look at what that looks like And there you go the paired pellet that looks rather nice And let's use something else. Let's just have a look at this now the thing is that many of these packages have between eight and twelve possible colors, but you might have a need for more than just those twelve And you can certainly do that with our colorbrewer package and we're going to use the color ramp pellet function as a keyword argument So this is how we would go about it. First of all, I'm just creating the sequence of values from negative two to two in steps of 0.1 And on the y-axis, I'm taking the sign of each of these values So I'm going to create this scatter plot x is going to have my x values that sequence y is going to have the sign values mode as markers. So it's a scatter plot And the marker is a list size equals 10 and the color then is this color ramp pellet Inside of that we have brewer dot p a l and we are asking for 10 there Spectral is what we want and we want 41 of these colors, please 41 colors So let's have a look at that and see how that comes out Beautiful there. We see our sine curve and we can see all these colors from red to blue and the 41 shades That we can see they're absolutely fantastic So that's in short, uh, how to manage colors inside of platte. There's really The sky's the limit you'd need and stick stick to deep sky blue You can choose whatever you like and there are many ways to go about this