Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# data source: https://github.com/datasets/world-religion-projections
# color palettes: http://www.sthda.com/english/wiki/ggplot2-colors-how-to-change-colors-automatically-and-manually
# import libraries
library(tidyverse)
library(RColorBrewer)
library(scales)
# import
url = 'https://raw.githubusercontent.com/datasets/world-religion-projections/master/rounded_percentage.csv'
setwd("~/Documents/Python/world-religions")
download.file(url, 'religion.csv')
religion = read_csv('religion.csv')
# reshape
religion = religion %>% gather(Religion, Percentage, -Year, -Region, -Country)
write_csv(religion, 'religion-reshape.csv')
# regional trends over time
p1 = religion %>%
mutate(Percentage = Percentage/100) %>%
filter(Country == 'All Countries' & Region != 'World') %>%
ggplot(aes(x = Year, y = Percentage, fill = Religion)) +
geom_area(size = 2) +
scale_fill_brewer(palette = 'Spectral') +
facet_wrap(~Region) +
scale_y_continuous(labels = percent) +
labs(title = 'World Religion Projections', subtitle = '2010-2050', caption = 'Data: github.com/datasets/world-religion-projections; Visual: @erikgregorywebb') +
theme_minimal() +
theme(legend.position = 'top') +
theme(text = element_text(size = 18),
axis.text.x = element_text(size = 12),
plot.title = element_text(size = 22, face = "bold"),
strip.text = element_text(face = "bold"))
setwd("~/Documents/Python/world-religions/images")
png('p1.png', width = 9, height = 6, units = 'in', res = 400)
p1
dev.off()
# least religious countries
p2 = religion %>%
filter(Year == 2010) %>%
filter(Religion == 'Unaffiliated') %>%
filter(Country != 'All Countries') %>%
filter(Country != 'Falkland Islands (Malvinas)') %>%
mutate(Percentage = Percentage/100) %>%
filter(Percentage >= .30) %>%
ggplot(., aes(x = Percentage, y = reorder(Country, Percentage), col = Region)) +
geom_point(size = 6) +
geom_point(shape = 1, size = 6,colour = "black") +
scale_color_brewer(palette = 'Spectral') +
scale_x_continuous(labels = percent) +
labs(title = 'Least Religious Countries', subtitle = 'Countries with 30%+ Unaffiliated, 2010',
caption = 'Data: github.com/datasets/world-religion-projections; Visual: @erikgregorywebb', y = '') +
theme_minimal() +
theme(legend.position = 'top') +
theme(text = element_text(size = 18),
axis.text.x = element_text(size = 12),
plot.title = element_text(size = 22, face = "bold"),
strip.text = element_text(face = "bold"))
png('p2.png', width = 9, height = 6, units = 'in', res = 400)
p2
dev.off()
# top gainers and decliners
p3 = religion %>%
filter(Year == 2010 | Year == 2050) %>%
filter(Country != 'All Countries') %>%
spread(Year, Percentage) %>%
mutate(Diff = `2050` - `2010`) %>%
filter(Diff > 2 | Diff < -2) %>%
filter(Religion %in% c('Christians', 'Unaffiliated', 'Muslims', 'Buddhists')) %>%
ggplot(., aes(x = Diff, y = Region, col = Religion)) +
geom_jitter(size = 5, alpha = 1) +
#geom_jitter(shape = 1, size = 6,colour = "black") +
scale_color_brewer(palette = 'Spectral') +
#scale_x_continuous(labels = percent) +
labs(title = 'Change in Religious Composition', subtitle = 'Percentage difference between 2010 and 2050, more than +/- 2%', x = 'Percentage Difference, 2010 to 2050',
caption = 'Data: github.com/datasets/world-religion-projections; Visual: @erikgregorywebb', y = '') +
theme_minimal() +
theme(legend.position = 'top') +
theme(text = element_text(size = 18),
axis.text.x = element_text(size = 12),
plot.title = element_text(size = 22, face = "bold"),
strip.text = element_text(face = "bold"))
png('p3.png', width = 9, height = 6, units = 'in', res = 400)
p3
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment