Skip to content

Instantly share code, notes, and snippets.

@jwinternheimer
Last active August 29, 2015 14:22
Show Gist options
  • Save jwinternheimer/0db1d8019e68deea4123 to your computer and use it in GitHub Desktop.
Save jwinternheimer/0db1d8019e68deea4123 to your computer and use it in GitHub Desktop.
Diversity Plots
library(data.table); library(dplyr); library(tidyr)
library(ggplot2); library(RColorBrewer)
## IMPORT AND TIDY DATA
demo_data <- read.csv(file.choose(),header=T)
names(demo_data) <- c("datetime","gender","ethnicity","region","age_range","department")
demo_data$ethnicity <- gsub("White","Caucasian",demo_data$ethnicity)
demo_data$ethnicity <- gsub("Southeast Asian","Asian",demo_data$ethnicity)
## GROUP BY DEPARTMENT AND GENDER
department_and_gender <- demo_data %>%
group_by(department,gender) %>%
summarise(n=n()) %>%
mutate(percent=n/sum(n),department_size=sum(n))
gender_bar_plot <- ggplot(department_and_gender, aes(x=reorder(department,-department_size), y=n, fill=gender)) +
geom_bar(stat="identity") + scale_y_continuous(breaks=seq(0,10,2)) + scale_fill_brewer(palette="Pastel1") +
labs(x="Area at Buffer",y="Team Members", title="Gender Breakdown of Buffer Across Areas") +
theme_minimal()
## GROUP BY DEPARTMENT AND ETHNICITY
department_and_ethnicity <- demo_data %>%
group_by(department,ethnicity) %>%
summarise(n=n()) %>%
mutate(percent=n/sum(n),department_size=sum(n))
ethnicity_bar_plot <- ggplot(department_and_ethnicity, aes(x=reorder(department,department_size), y=n, fill=ethnicity)) +
geom_bar(stat="identity") + scale_y_continuous(breaks=seq(0,10,2)) + coord_flip() +
labs(x="Area at Buffer",y="Team Members", title="Ethnicity Breakdown of Buffer Across Areas") +
scale_fill_brewer(palette="Pastel1") + theme_minimal()
## PIE CHART OF ETHNICITIES
by_ethnicity <- demo_data %>%
group_by(department,ethnicity) %>%
summarise(n=n()) %>%
mutate(percent=n/sum(n))
total_ethnicity_row <- demo_data %>%
group_by(ethnicity) %>%
summarise(n=n()) %>%
mutate(percent=n/sum(n),department="Total")
total_ethnicity_breakdown <- rbind(by_ethnicity,total_ethnicity_row)
ethnicity_pies <- ggplot(total_ethnicity_breakdown,aes(x=factor(1),y=percent,fill=ethnicity)) +
geom_bar(stat="identity",width=1) +
facet_wrap(~department) +
coord_polar(theta="y") +
scale_fill_brewer(palette="Pastel1") +
theme_minimal() +
theme(axis.ticks = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank()) +
labs(x="",y="",title="Ethnicity Breakdown of Buffer Team :)")
## PIE CHART OF GENDER BY DEPARTMENT
department_and_gender <- demo_data %>%
group_by(department,gender) %>%
summarise(n=n()) %>%
mutate(percent=n/sum(n))
total_row <- demo_data %>%
group_by(gender) %>%
summarise(n=n()) %>%
mutate(percent=n/sum(n),department="Total")
total_gender_breakdown <- rbind(department_and_gender,total_row)
total_gender_breakdown$gender <- factor(total_gender_breakdown$gender,levels=rev(levels(total_gender_breakdown$gender)))
gender_pies <- ggplot(total_gender_breakdown,aes(x=factor(1),y=percent,fill=gender)) +
geom_bar(stat="identity",width=1) +
facet_wrap(~department) +
coord_polar(theta="y") +
scale_fill_brewer(palette="Pastel1") +
theme_minimal() +
theme(axis.ticks = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank()) +
labs(x="",y="",title="Gender Breakdown of Buffer Team :)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment