 Hey folks, I'm patch loss and this is Code Club. In today's episode, we're going to take this slope chart showing people's attitudes towards receiving the COVID-19 vaccine from the fall of 2020. And we're going to modify it so that instead of coloring by the 15 different countries, yes, we have 15 different colors, which is a problem, right? So instead of 15 colors, we're going to try to re-represent the data using two colors, one color to indicate a decrease in intent and another color to increase the increase in intent. We're also going to see how we can use a gradient to show the degree to which people's attitudes changed rather than the absolute intent to change. So stay tuned and I'll show you how we'll do this. Okay, folks, I'm over here in our studio where I have the code that we left off from the last episode. Looking back at our code, we have our libraries that we are loading in as well as our fonts to make things look not default, make it look a little bit more attractive. We have this pipeline to create a data data frame. We have this other data frame for creating the country labels and indicating where to place them. And then we also have our GG plot pipeline, obviously. And so in GG plot, on the x-axis, we're putting the month. So again, August or October of 2020, the y-axis is the percent of people in those countries who say that they would be willing to receive the vaccine when it becomes available. We're then grouping it by the country so that we have a line connecting the two points in each line representing each country. And then color, we were representing by the country. So I want to change this to be change. And so we don't have change yet. So we need to go ahead and add that to our data data frame. So again, if we come back up to data, I'm going to look at the state of the data frame before we do the pivot longer. And what I see is that we have the country name, and then the column for August and the column for October. And what I want is to create a column called change. So I'm going to create a column that I'll call change. And then I'll use a case when, and I'll say August last than October, tilde increasing. And then I'll do August greater than October is decreasing. And then I will then do true for basically when they're the same as each other. And then I'll say stable. And so now if I run this chunk of code through to stable, I now see that I've got increasing, decreasing, and stable, that all looks good. I'll run the rest of that pipeline, looking at data to make sure everything is here. And we see that we still have that change. Now let's go into our pipeline for building out the figure and run that up. And I'm getting an error that object change not found. And I know exactly where that's happening. That's happening up here in my geome text rappel, where I'm using the aesthetics that are coming from GG plot. And so I'll need to modify this to then say x equals month, y equals percent, label equals country. But otherwise, I want to inherit dot a s equals false, which means I don't want the color coming from change. So now our slope plot has in red, those lines that are decreasing green, those that are increasing, I know this is not red, green, colorblind friendly, and those that are stable that haven't changed in blue. We need to go ahead and change the colors. But I think one of the things that I'm struck by immediately looking at this is that it's much easier to see that they're decreasing, right? There's 15 lines here, it gets to be a jumble. But when I see 15 red lines all kind of going in the same direction that says, Wow, there are, I guess not 15, there's 10 lines going down into the right. Wow, there's a lot of countries that have decreasing intent to receive the vaccine. I think it becomes a little bit more clear what's going on, rather than coloring by the country. So let's see if we can improve that a little bit more. What I want to do is we're going to use a custom scaling of our colors. And so after labs and before theme, I'll go ahead in here and do scale color manual. And then my name is going to be null. So we can set the name of the title of that legend. We already have done that up here in color equals and all. So you don't really need the name there, I don't think we'll do breaks equals decreasing, stable, and then increasing. And then for our values, we'll then do see for decreasing, I'm going to use blue. So I'll do hashtag 0000FF that's hexadecimal for blue for stable. I'm going to put in white. And so we'll see what that looks like. I'm picking white for a reason that we'll come back to by the end of the episode. And for increasing, let's do red, which is FF and then all zeros. And then for our labels, I'm going to use the same values as our breaks. And then I'll add the plus sign. And so now we see, again, decreasing blue, increasing red, the white for India and Canada is hard to see because we have all those grid lines in here. So I need to go ahead and turn off those grid lines in my theme function. To do that, we'll do panel dot grid equals element blank. So that got rid of those grid lines. And it's now a little bit easier to see the white lines might make it a little bit thicker the size of those lines. And again, we can adjust the thickness of our line by coming up to GM line and let's do size equals one. So I think the thicker line makes the lines pop out a bit more. I could make the background darker, but then it's going to perhaps just be overwhelmingly dark. I'm not totally a fan of that. We could achieve that by coming back and doing panel dot background equals element wrecked, and then do fill equals and let's say a a a a a a. So that dark background really does help bring out the white lines for Canada and India. So there's there's trade offs here, right? The dark background makes it a little bit more heavy. The white makes it clear that there's been no change. In this case, we'd say white is kind of the absence of change, right? And I think intuitively that works. Blue we think of as being cold. So it's decreasing red as hot and increasing. I've chosen not to include a legend here. We could put in that legend by going back to GM line and saying, you know, show dot legend equals true. But I think the colors are intuitive. And I'm going to make the design choice not to add a legend. There's something else we could do to make that more clear. I could make this word decreasing blue. And to do that, we are going to make use of something we talked about a few episodes ago when we talked about how we can format text. And so back up here in my labs function, at the decreasing, I'm going to put in a span. And again, this is HTML will do style equals, and then in the single quote now, because I'm already in double quotes, I'll do color colon, and then I'll put in the blue, which is 00FF. And then I close with the single quote, and the greater than sign. And then after decreasing, I need to back out of the span. And so I think that decreasing again, in blue makes it clear that things that are decreasing are in blue. This was looking at increasing or decreasing or staying steady as basically a dichotomous variable as a discrete variable. But what if I'd like to make it a gradient to show that China really fell off, whereas the United States sure it's blue, it's decreasing, but it only went down a couple of percent. How would we represent that? So instead of making change a categorical or discrete variable, I'm going to make it a continuous variable. And because I want you to have this in the notes, if you go check out that blog post, I'm going to leave it in here, but I'm going to comment it out, I'll do change equals, and then I'll say October, minus August. And now if I look at data, I will see that that change column is a continuous variable. And now I've got color as change. And for the time being, I'm going to come down to my scale color manual. And I'm going to comment this out as well. And we see that we get this continuum of blues. So I need to now come back up and turn back on my legend change show legend false to be true. And so now I've got this legend going from very dark at minus 12, to lighter blue at four, I would rather have this blue of decreasing B minus 12, and have a red color of four. And then I'd like to have something in the middle to indicate zero. If I if I was going from zero to some value, then I would want this kind of monotonic change in color. But because I'm going from negative values to positive values, I need to show that change between two different colors, because I'm going to two different ends that have different values different, you know, negative and positive values to do that. We'll come back up. And again, I'll leave that scale color manual in there for you to look at later if you want to grab the notes. I'll do scale color gradient. And we'll do name equals null again. And then I'm going to set a high value, which will be again hexadecimal will be the red. So that will be ff 0000. And my low value will be blue. So be 0000 ff. And again, you can use whatever colors you want. For me, I typically associate red with high blue with low. And so now what we see is in fact that the high change, the positive change is associated with red, and the load, the low change that the decrease is associated with that blue. But something I'm noticing is that we have kind of equal red and blue right around minus four. I would rather have white at zero to indicate zero as being kind of neutral that there's been no change. And so we can change that by modifying our scale color gradient to be scale color gradient two. And then we can put in mid to then be white. And to do that, we'll put in all f's. So now what we see is that we have the lighter shades showing up right around those countries that didn't change as much. And so clearly China changed a lot. And that, you know, something like the US and Spain, where these lighter shades of blue indicating a kind of a couple percent decrease in, you know, the intent to receive the vaccine. One thing you'll notice is that the saturation that we see of the red at positive four is comparable to the saturation we see in the blue for negative four. And that what we see is that China is dark red because it has such a large absolute value of decrease. Something that I'd like to try is let's limit the bounds to plus five and minus five and see if we can't kind of squish everything that's less than minus five into that dark blue color that's fully saturated. To do that, we can come back up to scale color gradient to I can say limits equals C minus five to five. And so what that does is it does constrain the limits from five to minus five and we see full saturation at those two bounds. But the values that are outside of those bounds are turned gray because the scale color gradient to is converting those values into n a's and representing those n a's as great, we could tell it to take those n a's and represent them as blue. But, you know, that might not work so well if we repeat this with future data. And we see actually something that went up, right? So say a country went up by 8%. We wouldn't want that to be blue, we'd want that to be red, right? So what I'm going to do is I'm going to modify my code so that if it's a change of greater than five, then I'm going to indicate that it is five, okay? So I'm going to modify change using a case when so I'll do change equals case when change is less than minus five, then I want to return minus five, right? If change is greater than five, I want to return five, right? Otherwise, so if it's true, then we're going to return change. And so now if we look at data, we can see that the change is bound by negative five and five. So China has been turned to minus five. I now see that China, Brazil, UK, Japan, I think that's Germany and Spain maybe, and France are all solid blue. But I'm not indicating that it's less than minus five, right? And so what I'll do is I will come back up to my scale color gradient two, and I will go ahead and put in some breaks. So my breaks are going to be five, 2.5, zero, minus 2.5, and minus five. And the labels are then going to be greater than 5.0, 2.5. Get rid of that extra parentheses I got in there. I'll do 0.0, and then minus 2.5, and then less than negative 5.0. And then I think I'm missing parentheses. So this again, bounds our color scheme between a bright red at five and higher and a solid blue at negative five and lower. You could quibble that it's a little bit deceiving because you're lumping China in with Brazil and UK that don't quite have nearly the same level of decreasing intent to receive the vaccine. At the same time, I would say if your change is greater than 5% negative from over two months, then that needs to be highlighted, right? That's a big change. That's beyond what you might consider as being kind of the margin of error of the poll that Ipsos conducted. So I'm okay with leaving that as negative five. And I think that's in good shape. So again, for telling the story of decreasing intent for receiving the vaccine, I think this tells a compelling story. The challenge with this, of course, is that it's no longer possible to more to easily link the country name to the line itself. As I talked about, there's ways that you could perhaps move the point that the name is connected to using geome text or geome text repel. I'll leave that for you to go back and look at that previous episode and see if you can't figure out how to do that. If you do, leave the code down below in the comments. I know the code gets a little bit funky, but maybe we'll revisit in a future episode and take a look. Otherwise, I think the story really is about decreasing intent. Anyway, like I said, this is not a perfect plot. We're kind of marching through different ways of working with this slope chart. Who knows, you might get to the end of this and say, you know, those dumbbell charts that Ipsos and Chard are made were pretty good for, you know, the limitations of the data. Keep practicing with this. See if you can't incorporate these other things that we've been talking about over recent episodes to see if you can't make this even better. Make your own figures even better. Who knows? Anyway, we'll see you next time for another episode of Code Club.