<jupyter_start><jupyter_text>Waffle charts  ... or never ever use pie charts Pie charts are the devil. They are an instant F in this class. I'll give you an example of why. What's the racial breakdown of journalism majors at UNL?Here it is in a pie chart:<jupyter_code>library(dplyr)
library(ggplot2)

enrollment <- read.csv("../../Data/collegeenrollment.csv")
jour <- filter(enrollment, MajorName == "Journalism")

jdf <- jour %>% 
group_by(Race) %>%
summarize(
       total=sum(Count)) %>%
select(Race, total) %>% 
filter(total != 0)

ggplot(jdf, aes(x="", y=total, fill=Race)) + geom_bar(width = 1, stat = "identity") + coord_polar("y", start=0)<jupyter_output><empty_output><jupyter_text>You can see, it's pretty white. But ... what about beyond that? How carefully can you evaluate angles and area? Not well. So let's introduce a better way: The Waffle Chart. Some call it a square pie chart. I personally hate that. Waffles it is. [Here's the library's github page with some instructions](https://github.com/hrbrmstr/waffle).<jupyter_code># install.packages('waffle')

library(waffle)<jupyter_output><empty_output><jupyter_text>The downside of the waffle library -- it doesn't play well with arbitrary dataframes. The good news? You don't want to be doing too many categories, so it's easy the create vectors to handle this.<jupyter_code>jour %>% 
group_by(Race) %>%
summarize(
       total=sum(Count)) %>%
select(Race, total) %>% 
filter(total != 0)<jupyter_output><empty_output><jupyter_text>So that's the data we need to create a vector for. Here's what that looks like:<jupyter_code>j <- c('Asian'=3, 'Black'=9, 'Hispanic'=10, 'NonResidentAlien'=4, 'Two or more races'=9, "Unknown"=1, "White"=194)<jupyter_output><empty_output><jupyter_text>Now we can put into the waffle library. The element j is our data, the number of rows you can change if you want, `title` and `xlab` are straight out of ggplot.<jupyter_code>waffle(j, rows = 10, title="Journalism majors by race", xlab="1 square = 1 student")<jupyter_output><empty_output><jupyter_text>How does this compare with the pie chart? Is it easier to read? Does it tell a different story?  By gender, specifying colors in the most basic way possible<jupyter_code>jour %>% 
group_by(Gender) %>%
summarize(
       total=sum(Count)) %>%
select(Gender, total) 
g <- c('Female'=141, 'Male'=89)

waffle(g, rows = 10, title="Journalism majors by gender", xlab="1 square = 1 student", colors=c("#ff69b4", "#0000ff"))<jupyter_output><empty_output><jupyter_text>Comparing different datasetsTo compare different datasets in sequential waffle charts, you create an iron (get it?) to put them together. Here, we'll compare the percentage of business administration majors who are male to the percentage of advertising majors who are male. First, let's get the stats.<jupyter_code>ba <- filter(enrollment, MajorName == "Business Administration")

ba %>% 
group_by(Gender) %>%
summarize(
       total=sum(Count)) %>%
select(Gender, total) 

ad <- filter(enrollment, MajorName == "Advertising & Public Relations")

ad %>% 
group_by(Gender) %>%
summarize(
       total=sum(Count)) %>%
select(Gender, total)<jupyter_output><empty_output><jupyter_text>Going old school, I calculated the percentages by hand, then added them to a vector where each waffle chart is expecting them. I then added a table to make it clearer to readers.<jupyter_code>iron(
  waffle(c(Women = 32, Men = 68), rows = 10, title = "Business administration majors"),
  waffle(c(Women = 66, Men = 44), rows = 10, title = "Advertising majors")
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12412831241283125, 'fraction_numerical': 0.013110181311018132, 'mean_word_length': 5.4846292947558775, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '444459', 'license': 'MIT', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1132, 'n_words': 396, 'path': 'Assignments/19_WaffleCharts/wafflecharts.ipynb', 'repo_name': 'mattwaite/JOUR491-Data-Visualization', 'size': '3585'}