 Hey folks, I'm Pat Schloss and this is Code Club. In recent episodes, we've spent a lot of time developing a series of dumbbell charts based off of data that was procured by Ipsos looking at people's willingness to receive the COVID vaccine from August and October of 2020. Well, going forward, I want to transform this dumbbell chart that we've been working on into something called a slope chart. Today, I'll show you how we can make that slope chart using two different methods, one using geome segment and one using geom line. They both have their strengths and weaknesses, but in the end, we get the same figure. And also in the end, I think we'll find that geom line is the better approach for carrying the visualization forward into future episodes. As I mentioned, we have spent a lot of time poking and prodding at this dumbbell chart that was originally published by Ipsos and later released with more styling by chart R. We of course then re-implemented both versions using R and made our own kind of makeover version of that figure. One of the things that I highlighted regularly over those episodes was the difficulty of showing the flow of time in that dumbbell chart. An advantage of the dumbbell chart is that we could have each of the 15 countries on a separate line. Again, the challenge is indicating the flow of time. The chart R version added an arrow to again indicate that flow of time, but the challenge there was for countries like India and Canada where there was no change between August and October. It's difficult to see, was there only one point? Was there two points? Was there any change? What's going on? So that's a challenge. In the last version, what I did was I desaturated the royal blue color. So I used royal blue for October and desaturated to show an older time for August. What we're going to do in today's episode, as I mentioned, is start to work on what's called a slope chart. A slope chart is really a line chart with two points. We have two time points before and after putting time on the x-axis, right? And then we can connect the two points from the same country to indicate the slope. That slope allows you to see a change in angle. Angle is a pre-attentive attribute, which we've talked about regularly over the course of these episodes as well, things like color, shape, separation, or joining or grouping. These are all pre-attentive attributes that are your eye naturally notices and allows you to kind of instantly interpret what's going on. Well, again, an angle is a pre-attentive attribute. We also see angles in things like pie charts. The problem with an angle in a pie chart is that there's really no common basis of comparison. But if we make a slope chart, we can again have an angle and the same basis of comparison, again being that first point on the x-axis. So we will use angle as a pre-attentive attribute in creating these slope charts. One of the challenges of a slope chart, however, is when we have 15 different countries, we're going to have 15 different lines. And if it's important to you to know which country is which line, as we'll see, we'll run into problems. And so in future episodes, we'll kind of dive into different ways of dealing with, you know, how can we parse apart what line corresponds to which country. So again, in today's episode, we'll look at two different ways of generating a slope chart, the first using geome segment and the second using geome line. Here we are in our studio. I've got a stripped down version of the R code that we had from the last episode where we made that makeover file. If you want to get this code or any of the code that I developed in these episodes, know that down in the below in the description for all of these episodes are links to blog posts, where you can get the code that I'm starting with as well as the code that I end with. As always, we are loading the tidyverse. I've also left in some stuff here for future episodes for worrying about fonts. Again, we're not going to touch that for today. And that what we really are focusing on is this pipeline here to create the data frame called data. And then we'll output this to a TIFF file called August October 2020 slope. And let's go ahead and give this a run. And so if I do data, I now see that I have this data frame that has my country, August and October. So I have my two months, as well as the country. Now to generate a slope chart, we can work with the data just as it is without having to do any dplyr work to get the data into a better format. You'll recall that in the previous episodes, when we made those dumbbell charts, we needed to gather the data pivot longer, so that we had one column for the month. Well, I'll show you how we can make a slope chart without having to do that. So I'll start by taking my data data frame, and we'll pipe that into ggplot. And then for aesthetics, for GM segment, we need an x, an x and a y and a y end. And so for the x and x end, we're going to put the names of the months. So we'll do x equals August, and then x and equals October, right? And so again, that's coming in the order that we want them from left to right on the x axis. And then for the y coordinate, so again, where it is on the y axis for the August, we'll then put August because that was the name of that column that month. And then y end, we'll put October. And then we give it our GM segment. So this gives us a slope chart, right? We've got August and we've got October. We have the percent on the y axis. Here it's saying August because that was the name of the y aesthetic. But really, it's the percentage. And so we can see those lines trailing down into the right. Of course, we don't know what each line corresponds to, except that we've looked at this data a lot. So I know like this big drop is China. And this horizontal line here is India. And down here, one of these lines, I think, is the United States. And this bottom one is France. But again, you should not expect your audience to memorize the lines. So we need to add color to this, right? So we can add color to this by adding another aesthetic. And I'll put in a line break here. So it's easier to see everything on one screen. And I'll do color equals country. And so here now we're going to map the value of the country to the color aesthetic. So yeah, now we have 15 lines and 15 different colors. And this is a bit of a disaster, right? It's really difficult for many of these lines to differentiate between the colors, right? So like there's this pink and this orange, and this pinkish purple and that pink, right? And I'd be really hard pressed to differentiate between those different colors, as well as this line for Canada. And I think this green for India is pretty similar, right? Or no, this is China. So it's just really hard and gets very confusing to understand what country corresponds to what line. Again, we'll talk about that in the future. I just want to point out that problem with the slope chart. I know that's there, but we'll come back to it in the future. But again, if the goal is to say there is an overall decrease in people's willingness to receive the COVID-19 vaccine, then this gets us really close, right? We can see that most of these lines are going down into the right between August and October. So that's a slope chart. And this is how we could make a slope chart using geome segment. Again, we give it X and X and we're going to name those months, Y and Y and those are the columns that we're plotting. And then the color we're mapping country to the color. So I'm going to save this by commenting it out for now. And what I'd rather do is use geom line, because I think that's going to set us up to be in a better position going forward in future episodes when we want to make modifications of our code to make it perhaps a little bit more appealing. So again, if we look at our data data frame, we see that we have our country and then August and October. And as I mentioned, when we're building out those dumbbell charts, one of the first things that we did was to pivot longer so that we had a column for country, a column for the month, and then a column for the percentage. That's what I want to do in today's episode to build out that geom line. So I'm going to add that to the end of this data pipeline, where we can pipe this into pivot longer. And we will pivot longer the calls of August and October. And we'll say names to month and values to equals percent. Now if we look at data, we see that we do have a tidy data frame with the country, the month and the percent as our three different columns. Now what we can do is we can take data and we can pipe that to ggplot. And we can say aes x on the x axis is going to be our month, or y is going to be percent. And then our color is going to be country, and then we'll do geom line. And we get an error message. Each group consists of only one observation. Do you need to adjust the group aesthetic? So that reminds me that we need to go ahead in here and put group equals country. And so now we should be in good shape. Here we have our slope chart that should look very familiar from the version we made with geome segment. So they're basically identical. There's a couple subtle differences you might notice in terms of the x axis and y axis labels. Here we have month instead of x and here we have percent instead of august. Also the labels on the x axis are all lowercase august and october because those are the columns. They were the column names, right? And they're the values then in the month column. Whereas before we specified the value in x and x and right so before we had august and october with the capital a and o to get us that nicer styling again don't get hung up on the text formatting or what text we do or don't have on this specific plot. In future episodes we will come back and we'll kind of build that out. So be sure you're subscribed so you know when that comes. But what I really wanted to focus on in this episode is the idea of a slope plot. And if you have paired data sure we can build that dumbbell chart where we have each observation in its own row. But if you're looking for overall trends like say decreasing intent to receive the COVID-19 vaccine that a slope chart can actually be really effective. And so we can see that most of the lines are going down from august to october and that way then you don't have to worry about what color is august and what color is october. Of course we still have that problem of what color is australia what color is the usa. But again we'll come back and we'll deal more with that in future episodes. So I hope you found this interesting as a different way to represent slope charts. Slope charts are something that my lab has used in our own scientific papers to you know compare before and after some type of intervention. And I think it has a lot of potential applications or again you have two time points for all of your observations. Keep practicing with this. See if you can spot slope charts out there in the wild. See if you can find dumbbell charts and think about how you might represent them as slope charts as well. Keep practicing and we'll see you next time for another episode of Code Club.