Created
April 1, 2017 12:53
-
-
Save patternproject/f1037ff02385ca137a51b763d209e7f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## ReadMe | |
# an attempt to recreate @VizWizBI Viz (http://www.vizwiz.com/2017/03/secret-of-success.html) using Waffle | |
## Libraries | |
if (!require("pacman")) install.packages("pacman") | |
pacman::p_load(tidyverse) | |
pacman::p_load(readxl) | |
pacman::p_load(waffle) | |
pacman::p_load(extrafont) | |
pacman::p_load(ggedit) | |
pacman::p_load(stringr) | |
## Setting up glyps to use circles instead of default squares | |
# src: https://www.r-bloggers.com/pre-cran-waffle-update-isotype-pictograms/ | |
# To use the FontAwesome glyphs you need to: | |
# grab the ttf version from here | |
# install it on your system | |
# install the extrafont package | |
# run font_import() (get some coffee/scotch while you wait) | |
# load extrafont when you need to use these glyphs | |
# font_import() | |
# Addiontally: | |
# src: http://stackoverflow.com/questions/14733732/cant-change-fonts-in-ggplot-geom-text | |
# extrafont::loadfonts(device="win") | |
## Helper Functions | |
# lambda function for purr::map | |
fn.1 = function(df) | |
{ | |
namedVec = df$value | |
names(namedVec) = df$key | |
return (namedVec) | |
} | |
## Input File | |
# read input | |
Secrets_of_Success <- read_excel("/1. Data/Secrets of Success.xlsx") | |
#View(Secrets_of_Success) | |
## Basic Validations | |
# master copy | |
df.1 = Secrets_of_Success | |
# fixing col names | |
names(df.1) <- tolower(names(df.1)) | |
names(df.1)[1] = "social.strata" | |
# changing rating to factor | |
df.1$social.strata <- as.factor(df.1$social.strata) | |
df.1$reason <- as.factor(df.1$reason) | |
## Data Wrangling | |
# sorting by social.strate | |
df.1 %>% | |
arrange (social.strata, rate) %>% | |
mutate(rate.2 = 100 * rate, | |
filler = 100 - rate.2) -> df.2 | |
View(df.2) | |
df.2 %>% | |
filter(str_detect(reason,"abili")) %>% | |
select(social.strata,rate.2,filler) %>% | |
gather(key,value,-social.strata) %>% | |
arrange (social.strata) -> df.3 | |
df.3 %>% | |
split(.$social.strata) %>% | |
map(fn.1) -> df.4 | |
## Plotting | |
waffle( | |
df.4$Poor, | |
size = 1, | |
colors = c("#E82C7D", "#E9E9EB"), | |
legend_pos = "none" , | |
use_glyph = "circle" | |
) -> w.poor | |
w.poor | |
waffle( | |
df.4$`Middle class`, | |
size = 1, | |
colors = c("#6B9CCB", "#E9E9EB"), | |
legend_pos = "none", | |
use_glyph = "circle" | |
) -> w.middle | |
w.middle | |
waffle( | |
df.4$`Rich people`, | |
size = 1, | |
colors = c("#F47E13", "#E9E9EB"), | |
legend_pos = "none", | |
use_glyph = "circle" | |
) -> w.rich | |
w.rich | |
iron(w.poor,w.middle,w.rich) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Social Strata | Reason | Rate | |
---|---|---|---|
Rich people | good education, high qualification | 28% | |
Middle class | good education, high qualification | 33% | |
Poor | good education, high qualification | 18% | |
Rich people | cunning, cheating | 11% | |
Middle class | cunning, cheating | 21% | |
Poor | cunning, cheating | 32% | |
Rich people | abilities, talents | 13% | |
Middle class | abilities, talents | 8% | |
Poor | abilities, talents | 7% | |
Rich people | connections to the right people | 9% | |
Middle class | connections to the right people | 32% | |
Poor | connections to the right people | 39% | |
Rich people | fortune, good luck | 13% | |
Middle class | fortune, good luck | 15% | |
Poor | fortune, good luck | 12% | |
Rich people | hard work | 38% | |
Middle class | hard work | 27% | |
Poor | hard work | 16% | |
Rich people | entreprenurial spirit, courage | 27% | |
Middle class | entreprenurial spirit, courage | 16% | |
Poor | entreprenurial spirit, courage | 16% | |
Rich people | presence of initial capital | 15% | |
Middle class | presence of initial capital | 23% | |
Poor | presence of initial capital | 27% |
Nice code @hrbrmstr, I just had to tweak your build_waffle()
function as the argument was colliding the internal x
build_waffle <- function(df) {
expand.grid(x = 1:10, y = 1:10) %>%
mutate(fill = c(rep(as.character(df$social_strata), df$has), rep("empty", df$has_not))) %>%
as_tibble()
}
thanks for sharing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's an easier way to do it than using the waffle package: