Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
## 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)
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%
@patternproject

This comment has been minimized.

Copy link
Owner Author

commented Apr 1, 2017

An attempt to recreate @VizWizBI Viz (http://www.vizwiz.com/2017/03/secret-of-success.html) using Waffle
waffle

@patternproject

This comment has been minimized.

Copy link
Owner Author

commented Apr 1, 2017

all combined

Issues/Suggestions:

  1. The output waffle chart using circle instead of square works individually, but not when multiple charts are combined with "iron". The above output has been combined using inkscape. The iron output is as follows:
    iron

  2. How to combine them in a grid of "social class" on one and "reasons" on the other.

  3. Starting point of individual waffle chart is different. Original starts from top-left, waffle starts from bottom-left.

@hrbrmstr

This comment has been minimized.

Copy link

commented Apr 1, 2017

There's an easier way to do it than using the waffle package:

library(stringi)
library(extrafont)
library(rprojroot)
library(hrbrthemes)
library(tidyverse)

build_waffle <- function(x) {
  expand.grid(x=1:10, y=1:10) %>% 
    mutate(fill = c(rep(as.character(x$social_strata), x$has), rep("empty", x$has_not))) %>% 
    as_tibble()
}

wrap_20 <- scales::wrap_format(20)

prefix <- rprojroot::find_root(has_file("Secrets of Success.csv"))

read_csv(file.path(prefix, "Secrets of Success.csv"), col_types = "ccc") %>%
  set_names(c("social_strata", "reason", "rate")) %>%
  mutate(social_strata = stri_trans_totitle(social_strata),
         reason = stri_trans_totitle(reason),
         reason = stri_replace_all_fixed(reason, ",", " &"),
         reason = wrap_20(reason)) %>% 
  mutate(social_strata = factor(social_strata, levels=c("Poor", "Middle Class", "Rich People"))) %>% 
  mutate(rate = as.numeric(stri_replace_last_fixed(rate, "%", ""))/100) %>% 
  mutate(has = as.integer(rate*100), has_not = 100-has) %>% 
  group_by(social_strata, reason) %>% 
  do(build_waffle(.)) -> success_df

ggplot(success_df, aes(x, y)) +
  geom_point(aes(color=fill), size=3) +
  facet_grid(social_strata~reason, switch = "y") +
  scale_y_reverse() +
  scale_color_manual(values=c("Poor"="#e53780", "Middle Class"="#4d8cd1",
                              "Rich People"="#f27f14", "empty"="#e8e9ec")) +
  labs(x=NULL, y=NULL) +
  theme_ipsum_rc(grid="") +
  theme(plot.margin=margin(30,30,30,30)) +
  theme(panel.spacing.x = unit(0.75, "lines")) +
  theme(panel.spacing.y = unit(0.75, "lines")) +
  theme(panel.grid=element_blank()) +
  theme(strip.text=element_text(size=10)) +
  theme(strip.text.x=element_text(hjust=0.5, vjust=0)) +
  theme(axis.text=element_blank()) +
  theme(legend.position="none") 

image

@ginolhac

This comment has been minimized.

Copy link

commented Apr 3, 2017

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
You can’t perform that action at this time.