-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstackedbars.qmd
128 lines (102 loc) · 4.21 KB
/
stackedbars.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Stacked bar charts
One of the elements of data visualization excellence is **inviting comparison**. Often that comes in showing **what proportion a thing is in relation to the whole thing**. With bar charts, we're showing magnitude of the whole thing. If we have information about the parts of the whole, **we can stack them on top of each other to compare them, showing both the whole and the components**. And it's a simple change to what we've already done.
We're going to use a dataset of college basketball games from this past season.
```{r}
#| warning: false
#| message: false
#| results: asis
#| echo: false
library(downloadthis)
library(glue)
dllink <- download_link(
link = "http://mattwaite.github.io/sportsdatafiles/logs22.csv",
button_label = "Download csv file",
button_type = "danger",
has_icon = TRUE,
icon = "fa fa-save",
self_contained = FALSE
)
glue("<pre><p><strong>For this walkthrough:</strong></p><p>{dllink}</p></pre>")
```
Load the tidyverse.
```{r}
#| warning: false
#| message: false
library(tidyverse)
```
And the data.
```{r}
games <- read_csv("data/logs22.csv")
```
What we have here is every game in college football this season. The question we want to answer is this: Who had the most prolific offenses in the Big Ten? And how did they get there?
So to make this chart, we have to just add one thing to a bar chart like we did in the previous chapter. However, it's not that simple.
We have game data, and we need season data. To get that, we need to do some group by and sum work. And since we're only interested in the Big Ten, we have some filtering to do too. For this, we're going to measure offensive production by rushing yards and passing yards. So if we have all the games a team played, and the rushing and passing yards for each of those games, what we need to do to get the season totals is just add them up.
```{r}
#| message: false
games |>
group_by(Conference, Team) |>
summarise(
SeasonOffRebounds = sum(TeamOffRebounds),
SeasonTotalRebounds = sum(TeamTotalRebounds)
) |>
mutate(
SeasonDefRebounds = SeasonTotalRebounds - SeasonOffRebounds
) |>
select(
-SeasonTotalRebounds
) |>
filter(Conference == "Big Ten")
```
By looking at this, we can see we got what we needed. We have 14 teams and numbers that look like season totals for two types of rebounds. Save that to a new dataframe.
```{r}
#| message: false
games |>
group_by(Conference, Team) |>
summarise(
SeasonOffRebounds = sum(TeamOffRebounds),
SeasonTotalRebounds = sum(TeamTotalRebounds)
) |>
mutate(
SeasonDefRebounds = SeasonTotalRebounds - SeasonOffRebounds
) |>
select(
-SeasonTotalRebounds
) |>
filter(Conference == "Big Ten") -> rebounds
```
Now, the problem we have is that ggplot wants long data and this data is wide. So we need to use `tidyr` to make it long, just like we did in the transforming data chapter.
```{r}
rebounds |>
pivot_longer(
cols=starts_with("Season"),
names_to="Type",
values_to="Rebounds")
```
What you can see now is that we have two rows for each team: One for rushing yards, one for passing yards. This is what ggplot needs. Save it to a new dataframe.
```{r}
rebounds |>
pivot_longer(
cols=starts_with("Season"),
names_to="Type",
values_to="Rebounds") -> reboundswide
```
Building on what we learned in the last chapter, we know we can turn this into a bar chart with an x value, a weight and a geom_bar. What we are going to add is a `fill`. The `fill` will stack bars on each other based on which element it is. In this case, we can fill the bar by Type, which means it will stack the number of rushing yards on top of passing yards and we can see how they compare.
```{r}
ggplot() +
geom_bar(
data=reboundswide,
aes(x=Team, weight=Rebounds, fill=Type)) +
coord_flip()
```
What's the problem with this chart?
There's a couple of things, one of which we'll deal with now: The ordering is alphabetical (from the bottom up). So let's `reorder` the teams by Rebounds.
```{r}
ggplot() +
geom_bar(
data=reboundswide,
aes(x=reorder(Team, Rebounds),
weight=Rebounds,
fill=Type)) +
coord_flip()
```
And just like that ... Purdue comes out on top? Huh. And look who is not last.