Note that although forcats is part of the “tidyverse”, it is not automatically loaded when you run library(tidyverse)

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.

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)
## 
## -- Column specification --------------------------------------------------------
## cols(
##   X1 = col_character(),
##   X2 = col_double()
## )
religions
## # A tibble: 21 x 2
##    religion                      millions_of_followers
##    <chr>                                         <dbl>
##  1 Christianity                                   2400
##  2 Islam                                          1900
##  3 Hinduism                                       1200
##  4 Secular                                        1200
##  5 Buddhism                                        506
##  6 Chinese Traditional                             394
##  7 Various Ethnic Religions                        300
##  8 African traditional religions                   100
##  9 Sikhism                                          26
## 10 Spiritism                                        15
## # ... with 11 more rows

1A: basic ggplot

Q: What are the most followed religions? A: Use ggplot to compare religious populations

You can also embed plots, for example:

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?

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()

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()

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()