-
Notifications
You must be signed in to change notification settings - Fork 31
/
ggplot_forcats.rmd
93 lines (69 loc) · 2.79 KB
/
ggplot_forcats.rmd
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
---
title: 'tidyverse: using forcats to improve your ggplots'
author: "catlin"
date: "2/10/2021"
output: html_document
---
Note that although forcats is part of the "tidyverse", it is not automatically loaded when you run `library(tidyverse)`
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(forcats)
```
# Handy forcats functions for ggplot2
## Comparing followers of world religions
Source: https://en.wikipedia.org/wiki/List_of_religious_populations
I was looking for a simple dataset with count data for many items to demonstrate some basic forcats functions that are useful when creating plots.
```{r, warning=FALSE}
religions = read_csv("https://raw.githubusercontent.com/acatlin/data/master/religions.csv",
col_names = FALSE) %>%
rename(religion = X1, followers = X2) %>%
mutate(millions_of_followers = followers/1000000.0) %>%
select(religion, millions_of_followers)
religions
```
## 1A: basic ggplot
Q: What are the most followed religions?
A: Use ggplot to compare religious populations
You can also embed plots, for example:
```{r}
religions %>%
ggplot(aes(x = religion, y = millions_of_followers)) +
geom_col(fill = "lightblue") +
labs(x = "religion", y = "millions of followers",
caption = "https://en.wikipedia.org/wiki/List_of_religious_populations")
```
## 1B: How do I flip coordinates?
```{r}
religions %>%
ggplot(aes(x = religion, y = millions_of_followers)) +
geom_col(fill = "lightblue") +
labs(x = "religion", y = "millions of followers",
caption = "https://en.wikipedia.org/wiki/List_of_religious_populations") + coord_flip()
```
## 2A: How do I change sort order?
Revised by: Andy Catlin
Q: How do we change the chart to show the most followed religions first?
A: Use forcats::fct_reorder()
```{r}
library(forcats)
ggplot(religions, aes(x = fct_reorder(religion, millions_of_followers),
y = millions_of_followers)) +
geom_col(fill = "lightblue") +
labs(x = "religion", y = "millions of followers",
caption = "https://en.wikipedia.org/wiki/List_of_religious_populations") +
coord_flip()
```
## 2B: How do I combine less frequently used categories?
Q: How do we combine the less-followed religions into a single group?
A: Use forcats::fct_other()
```{r}
top5 = unlist(select(head(arrange(religions, desc(millions_of_followers)), 5), religion))
religions %>%
mutate(religion = fct_other(religion, keep = top5, other_level = "Other religions")) %>%
ggplot(aes(x = fct_reorder(religion, millions_of_followers), y = millions_of_followers)) +
geom_col(fill = "lightblue") +
labs(x = "religion", y = "millions of followers",
caption = "https://en.wikipedia.org/wiki/List_of_religious_populations") +
coord_flip()
```