 Hey folks, my name is Pat Schloss and this is Code Club. Over the past several episodes, we've been taking a barbell chart or dumbbell chart, whatever you want to call it, that was published by an agency called Ipsos describing the acceptance of the COVID-19 vaccine across 15 different countries. Now these data were back from last fall in 2020 before the vaccines were actually available, kind of gauging whether or not people would be willing to take that vaccine. They looked at people's responses in August of 2020 and then again in October of 2020. Of course, these data are now dated, but I thought the data and the plot was interesting fodder for learning about more tools within GG Plot than what we've been doing in other episodes. So we recreated the version of the dumbbell chart that was in that Ipsos report a few episodes back. I'll put a link across the top here if you want to go grab that first episode and kind of make sure you see the whole playlist. In the next episode, I revealed to you that, you know, I actually found out about this data and this type of visualization for the data from a newsletter that I get from a group called Chart R. And so although it's called Chart R, they don't actually make their plots in R. They tend to make very stylized figures of things that are kind of going in contemporary culture. And I like looking at it to kind of, you know, critique figures and then kind of get a sense of what other types of data out there and how do other people present data? It's a pretty cool newsletter. You should definitely subscribe. The first step of converting the Ipsos version to the Chart R version was coming up with a way to alternate the color in the background of the plotting panel. So we did that. The next step, which is what we did in the last episode, was to add arrow heads to the lines to indicate the direction of time from August to October. Those arrows were very helpful for our audience to figure out again that passage of time, so they don't necessarily need a legend to know what color is August and what color is October. They can look at the arrowhead. But when we put these two steps together with the alternating colored background and those arrowheads, whoa, it looks really bad. So what I want to do in this episode is using that combined figure. See if we can add in the color scheme that was used by the Chart R figure to make a far more attractive version of the figure. And at the same time, learn something about color and how to implement color within our RGG plot to generated figures. You might be asking yourself, Pat, where do we normally get colors from? Well, the colors we're working with are largely default colors. You should know that there's a really handy dandy function in R called colors. It's actually a function. So if you type colors with the parentheses at the prompt, you'll get a listing of 657 different named colors. Things like Dodger Blue, Sienna, Chartreuse 4, Gray 53. You could guess at what the colors are based on the names, but I'm not very, I'm pretty color challenged. And so that's pretty hard for me. What I end up doing with these named colors is I have a handful of colors that I like, and I kind of repeat using those colors, right? So you've seen me use dark gray, you've seen me use Dodger Blue, perhaps I've used hot pink, which isn't as hot pink as you might think it is. Anyway, this gets kind of tedious, though, if you're trying to match a color scheme that somebody else has, or if you're trying to kind of expand your color repertoire. Alternatively, there are online color palettes that you can use. Things we've talked about in previous episodes like Color Brewer is a really handy dandy web app that gives you all sorts of different color palettes. And then there are more whimsical color packages out there, things like Team Colors, which will give you the colors of your favorite sport team, the Beyonce color package, which will get you palettes inspired by Beyonce's outfits, and then also a Wes Anderson color palette, which is a palette inspired by Wes Anderson movies. What we're going to do in today's episode is take a color palette that we like from this figure and try to adapt it. Effectively, what we're doing is what those people who developed the Beyonce team colors and Wes Anderson color palettes did, where they found certain colors in pictures of say scenes or photo shoots or uniforms or whatever, and they then converted those colors to a numerical code that a computer understands. That code is called hexadecimal code. And hexadecimal code is a six digit string of hexadecimal values, where two of the digits are for the red colors, two are for the green, and two are for the blue. So based on different shades of red, green, and blue, we can get a myriad of different colors. What is hexadecimal, you're asking? Hexadecimal is a number between zero and F. What? What kind of numbers that? Well, when we typically count, we count in decimal, right, from zero to nine. All the numbers that we think of are these 10 digit numbers made up of these 10 values between zero and nine. Hexadecimal values, on the other hand, are have 16 different digits, so zero to nine, plus a through F, 16 different characters. For again, for each of the red, green, and blue, you get two digits of this hexadecimal number. And so 16 times 16 is 256. So you get 256 reds, 256 greens, and 256 blues, which is something like 768 total values in this six digit hexadecimal string. So again, thinking about this hexadecimal string, if zero is the absence of a color, and F is the fullness of that color. And again, we have three spots, right, for red, green, and blue, and a hexadecimal code of FF0000 would be full red, no green, no blue. And so you would get red. If you had 00000, you'd get black, right, because you would have no red, no green, no blue, black, right? Whereas if you had FF, FF, FF, that would be all three colors to the max, and that would give you white. So white is the combination of all colors, black is the absence of all colors. There are also colors like grays, right, where if you have the same value, the same two digit value in each of the three spots, you then get a gray color, right? So if we had 898989, we then get this gray color. I'm going to use a utility that is baked in preinstalled on my Mac computer. It's called the digital color meter. Windows also has a similar tool that you can use for my friends who are using Windows computers. But again, because I have a Mac, I'm going to use this built in tool. And you know, all the tools are basically the same, you kind of hover over a color over a set of pixels. And it will tell you the red, green, blue, the hexadecimal for that color. And then we can bring that into our now when we get to our, which is kind of, you know, what we're here for, I am going to show you four different ways that we can incorporate and modify color in our code. And so that's going to be really powerful to make our currently very hideous version of the chart are Ipsos graph to look a lot more attractive and a lot more like that original chart our figure before we get going in our if you wouldn't mind down below, hit that thumbs up button to let me and others know that you're getting something out of this episode that would just really make me happy. Also, if you like this type of content, I want you to know that I've recently put out a newsletter that will be coming out every Friday or so where I plan in packing that with even more content, say example problems, food for thought, and to let you know about workshops that I have coming up in the future. So I've got my code, I run it, and it's producing this beautiful dumbbell chart with the arrows and the alternating colored background. Again, we've got some work to do. And what we want it to look like is this version of the chart are figure of the same data. If I come to my applications directory down towards the bottom, there is the utilities directory. And then in there is my digital color meter. So as I move my cursor across the screen, in the larger of the two squares, you'll see a kind of blown up version zoomed in on kind of that P in skepticism, where we're getting this teal color. To the right of that, it then gives us three values for red, green, blue, these are decimal values, right, because they're digits that go from zero to nine, 65, 117, 134. It tells me that it's display native values. So this the first time I opened it, this is kind of the way it looked. So again, I don't want these digital values, I want the hexadecimal values. So I can come up to view and then display values as hexadecimal. And so now you can see that the format changed. If I come back to that P in skepticism, I can now see I've got RGB for red, green, blue, but I get zero x 46 zero x 77 zero x 88. So that zero x tells you that it's a hexadecimal number. The other thing I want to change is this display native values, the better setting to use is display an s RGB, which is a standardized red, green, blue. We'll notice a huge difference. But that's really the best one to use. And so again, you can kind of see that as you scan around that P, you get those hexadecimal values. So if I want to get a copy of those hexadecimal values without having to kind of remember them and write them out manually, I can do shift command C. And that will save it to memory. I can then come back to my text, and then paste that in as that hexadecimal value. So where did I get that keystroke from? If I come up to color, and then copy color as text, you'll see it's that shift command C. Again, if you're working in another tool, I'm sure there's another way to copy the hexadecimal for what you're trying to do. I now want to start by modifying the color of the handle of the barbell and the arrow that goes with it, as well as the digits that we have to either side of our dumbbells. So in my chart R version, I'm going to kind of put my cursor right over the handle of the dumbbell. And you know, I'll probably because the color of the handle is the same as the August, maybe I'll get a little bit more real estate here as I point over the August for China, and kind of get no point where I have a consistent color. What you kind of notice is that it gets a little bit grainy as you're zoomed in here. And the colors kind of fluctuate a little bit. But I think I'm in a spot here where it's a pretty good, consistent color. So again, I'll copy that. And I will then come back down to my code and look for the geom line. Previously, I had this gray color. And I will replace that with this value that I copied out of my color meter. Also, I'm going to put that in place of red for my arrow. And now we've got our colored lines, they look kind of black. It's very difficult to perceive the colors accurately, or as they finally will look because we've got this hideous alternating colored background. Don't worry, we'll change that soon enough. One thing I do want to modify though is that the thickness of the line is a bit thicker than what we see in the chart R version. I can modify that in my geom line where I had 1.75. Maybe I'll go down to 0.75. And I also want that width, the size for my arrow. So that was in geom path. I can do size equals 0.75. And those arrows, those handles of the dumbbells look like the right color and the right thickness. Again, you could do all sorts of futzing, but I'm pretty happy with the way that looks. The next thing I want to modify is the color of these digits that are on either side of the dumbbell. Looking at the chart R version, those colors are all the same. Now, the problem with picking a color out of these digits is again, as you can see, it's really grainy and there isn't a consistent color on this eight. Maybe I'll pick, you know, one of the more medium colored points. So we've got 67, 69, 6C. So they're all kind of in that high 660 value in hexadecimal. I'll go ahead and let's do 69 across all three channels for our geom text. So again, we can come up to geom text, and I'll do color equals and then quotes pound 68, 68, 68. So again, if you repeat the same digit three times, for the red, green, blue, you will get a gray color. And so now this gives us kind of a gray color, which I think is a pretty appropriate for those digits. The other thing I notice as I compare between what we have here with the chart R and my version is that my version, again, modeling after ipsos, the numbers are bigger than the points. Let's go ahead and make them about the same size or maybe a little bit smaller than the points. And so if we come up to geom text, I'll go ahead and make my size two. And that looks like a more appropriate size to the text that more closely resembles what we have in that chart R version. Okay. What I want you to take away from this part of the episode is that we can insert color, size, any kind of attribute of the geom we are plotting by including it with the value right outside of the AES statement. And so if we include that outside of the AES statement, it applies that value to all instances of that geometry, right? So we had geom text, and we set color equals six, eight, six, eight, six, eight. And so all of the text that was being plotted is now that gray color. And that's even though up in our geom plot statement, we had color by month, right? So before, the text was colored by the month. And now all of the text is getting the same color. And we did the same thing with our arrows, and with our lines. And we did the same thing also with the size. So all of the handles of the dumbbells are the same same width, the same size. Alternatively, there are cases where we do want to map color to the month in this case, right? But we want to control the color of the month, right? And so we want a color for August and a color for October. And actually, I think we've already modified that with our scale color manual, where we have a color for August and a color for October. So the first thing I'm going to do is I'm going to go ahead and grab this color for geom line, which again matched the August color. And I'm going to plop that in here in place of this gray color that we had. And I'm going to come back to my chart our version and look at one of these green points so that I can get the October color, right? And so this is kind of a pretty consistent region in color. So I'll go ahead and copy that, and then plop that in place of October, and get our green colors. Now, something that's fascinating is that it looks like our green color is alternating with each row, doesn't it? If we take our color dropper and look at the color that we have here, so 59 AC 74, and I look at these two green dots, they're the same red, green, blue. And so the perceptual problem is again, what's going on in the background. And so again, this is why we have to be so careful with color and things like heat maps can be really difficult to interpret because we interpret visually the color of that point based on the context of what's around it. So I'm happy with this color. And hopefully when we now go in and change the fill color of these strips, those colors will look a little bit more consistent and reasonable. So to modify the color of those background strips, instead of scale color manual, we'll do scale fill manual. We'll do name equals null. And then we'll do breaks equals a, b, and C. Those were the values that we kind of made up the variable names that we made up for those three different types of strips that we had. And then in here, we'll do values equals something for a something for B something for C. And then we can do the same thing for our labels to be C, a, b, and C. I don't really care about the labels, because ultimately, we're going to kind of remove that legend. But now we need to get the values for those colors of our strips, again, turning to the chart our version down this first spot, which will be a, we'll go ahead and copy this color, and then the B color, and then the C color, which is also the background of the plot. We now get the same color of our alternating strips that we have in the chart our version. Why don't we go ahead and get rid of this legend. And as I've shown you in past episodes, we can get rid of a legend by coming up to the geome that is producing that legend. So in this case, geome ribbon, and we can do show dot legend equals false. It's a much wider version of the figure. That's fine. We'll deal with that in a future episode probably. But our colors are pretty consistent. The next thing that I want to go over with you is how we can change the color of non data elements in the figure. So those data elements that we changed were again things that were expressed in the AES function, or as an attribute within each of the geome. But there are other things like the title of this figure, the caption, perhaps a subtitle, labels on the x and y axis, the background color that we want to change. And we can change that using a variety of arguments to the theme function. The first thing that I want to change is the background color. So we can modify that background color in theme by doing plot dot background equals element rect, and we'll do fill equals. And then this is going to be the color for C, which I had up here in my scale fill manual, this one here. And right in here, we'll put that fill, add the comma. And don't you know, now we have that colored background. Very good. And again, that matches the coloring to the most part, I think pretty well, that we have for the chart R version. The next thing I want to change is the color of my title. We'll go ahead and highlight maybe over that V, get a kind of consistent region. So we'll come back up to our theme function. And in element text, we will then put color equals and then in quotes, our hexadecimal code, and you'll notice that we get the same color as the title from the chart R version, we're going to change the title in a future episode, also the font. But I think what I'm trying to do now again is to match that color. And I'm pretty happy with the way that looks. So these labels on the x axis are a little bit darker than what I have for my x axis. And I'm going to make them the same color as I labeled my text in the geome text. Again, all this can be done in the theme function. You'll notice I already had that element text. And I'm going to come back up to my geome text and grab this color and replace that in there for the dark gray, and I get a little bit darker color for the axis going back to the same axis, text x, I can put in here size equals six. Yeah, and those numbers do look a smidge smaller. What I'm trying to show you is that in the theme function, we can modify the color of the different non data elements, right? So the lines, the text, the, the squares, the rectangles, right, of that plotting window, we can modify. And if I haven't said this before, let me just repeat it, that if it's a line, or if it's text, or if it's a solid point, then we change the color of that using the color argument. But if it's a rectangle, like we saw in plot background, then we use fill. So alternatively, we could also say color equals let's do red. This then gets me a red frame around my plotting window, which is not what I want. So I will remove that color equals red. But again, wanted to drive home the point of the difference between fill and color. The final approach to modifying color that I want to share with you, I think is super powerful, and is way under utilized. And in this figure, it's kind of subtle. And so if you look down here in the middle of the bottom, they have an x axis label, or I'm going to just assume it's an axis label for our purposes of chart R. And sure, it's colored, but it's got two different colors. So we've kind of done this before in a previous episode. But we could actually use CSS, so stuff from HTML programming, to color the chart part of the word in this teal color and the R to be this reddish type color. So let's go ahead and remember how we do that. So first of all, we need to make sure that we have library GG text, because we're going to use something called element markdown from GG text to bring in the CSS HTML styling. And if we come down to our labs, you'll notice we had x equals null. I'm going to go ahead and replace that with chart R. And so we have chart R good. So we're going to wrap chart R from our x axis label in some HTML code. So I'll start with a span. And close that with another span around the chart within this first span tag, I'll do style equals and then in single quotes, I will do color colon, I'll do green. So 00 ff 00. Again, we're going to modify this later, put in a semi colon there for good measure. And then we'll do the same thing for the R. And instead of green, for now, let's make it red. And we need to make sure that we close out that span as well. And so we get all that great HTML. But to get that HTML to render correctly, we need to go ahead and do access dot title dot x equals element markdown. And so now we get chart R that's part green and part red. And those are definitely not the right versions of green and red. So let's grab our picker again. And I think this is the same color as the title, but I'm not totally sure. And then let's get this orangish reddish color. This is looking so much better than it did when we started. Again, that comes back to being able to use that digital color meter, the eyedropper tool that you can get from a lot of different places to match the different colors. So I hope this was really useful in getting you to think about colors and how we can match the coloring from other graphs or other, you know, websites or wherever you're getting colors and bring them into your own code to make a more attractive figure. Color is something that's a really powerful tool for expressing information. As we also saw that kind of depending on the surrounding pixels, the surrounding colors, it can alter how we perceive colors ourselves. So it's something we certainly want to be careful with. In the next episode, I'm excited because it's going to be something that I've never done before. You'll notice that the chart R version of the figure has a couple different fonts, right? So they've got this serif font, so font with, you know, where the H has kind of a hat on it. Whereas what we're using is a sans serif font. And while we can certainly get kind of a generic serif and sans serif font in our charts, what I want to do is go out and see if we can't grab the actual font that chart R is using and incorporate it into our figure so we can have that extra layer of polish that they have by using kind of these non default fonts. Be sure that you're subscribed to the channel that you've hit that notification icon so that you know when that episode comes out, because I think that's going to be something that's a pretty cool trick that you can use to make your figures stand out. Keep practicing and we'll see you next time for another episode of Code-