Halloween is coming..!
Halloween is just around the corner, I am still trying to decide which candies to purchase this year for trick-or-treaters.
Initially I was looking for data sets maybe comparing American chocolate bars vs Canadian chocolate bars possibly with sugar contents or lists of ingredients. I am really curious why there are big differences in candy between Canada and US, but for now I couldn’t find them instead I came across below data sets.
Data itself is actually bit outdated, since it’s data from candy sales in 2007-2015. I’m not even sure if these popular candies changes every year or not. Now I’m curious to find data sets that are more recent…CandyStore.com’s sales from 2007–2015—focusing on the three months leading up to Halloween
Since data set contains data at state level for US, I decided I’ll practice plotting the data on grid map, I’ve been wanting to try. To colour the map, I’ve decided instead of colouring by volume of candy purchased (in pounds), I’ve gathered population by state in order to figure out ratio of candy per person.
Looks like Hawaiian eats lots of Hershey Kisses (or in general, maybe there are more sweet tooth in Hawaii!). Utah, Nevada and Arizona also seems to maybe either consumer more sweets or possibly top ranked chocolates are purchased in volume!
It would’ve been fun to gather all candy images and place them in the grid too…!
Below are the steps I took to visualize the data.
Importing Data &amp; Tidying Up
I’ve imported data to dataframe using jsonlite package using fromJSON function from OpenDataSoft site. There seems to be lots of other interesting data sets!
I’ve also harvested population by state from Wikipedia using rvest package.
library(tidyverse) ## basically tidyverse is needed for everything :)
library(jsonlite) ## to read data in json format
library(rvest) ## to scrape website
library(hrbrthemes) ## I love this themes! ## read this: https://cran.r-project.org/web/packages/hrbrthemes/vignettes/why_hrbrthemes.html
library(geofacet) ## I'll use the grid in this package to create
library(ggrepel) ## so that texts on ggplot don't overlap!
library(janitor) ## My recent favourite
## Download Candy Data from Opendata Soft site.
candy_data %
gather(ranking, chocolate, -state) %__%
mutate(ranking = as.integer(str_remove(ranking,&quot;rank_&quot;)))
candy_pound_long %
select(state, pound_1, pound_2, pound_3) %__%
gather(ranking, volume, -state) %__%
mutate(ranking = as.integer(str_remove(ranking,&quot;pound_&quot;)))
candy_long % inner_join(candy_pound_long) %__%
left_join(state_pop)
candy_long % mutate(volume_per_capita = volume/pop_2016)
## Just creating state table
state_df % adorn_totals(&quot;col&quot;) %__% arrange(-Total,-`1`,-`2`,-`3`) ## if candy was listed same # of times, then I want to make sure that better ranked candies are ranked higher.
#show_col(c(&quot;#A77044&quot;, &quot;#A7A7AD&quot;,&quot;#D6AF36&quot;)) # bronze, silber, gold
candy_long %__% mutate(chocolate = fct_relevel(chocolate, candy_list$chocolate)) %__%
count(chocolate, ranking, state, state_abb) %__%
ggplot(aes(x=fct_rev(chocolate),y=n)) + geom_col(aes(fill=fct_rev((factor(ranking)))), colour=&quot;#ffffff30&quot;) +
geom_text(aes(label=state_abb), position=&quot;stack&quot;, colour=&quot;#ffffff&quot;, size=5, hjust=1, family=font_rc, fontface=&quot;bold&quot;) +
theme_ipsum_rc() +
coord_flip() +
scale_fill_manual(values=c(&quot;#A77044&quot;, &quot;#A7A7AD&quot;,&quot;#D6AF36&quot;),
name=&quot;ranking within state&quot;, labels=c(&quot;bronze (3rd)&quot;, &quot;silver (2nd)&quot;,&quot;gold (1st)&quot;)) +
scale_y_comma() +
labs(title=&quot;Halloween Candy Olympic! - which candy gets which ranking from which state?&quot;) +
theme(legend.position=&quot;top&quot;, legend.direction = &quot;horizontal&quot;) +
guides(fill = guide_legend(reverse = TRUE)) Grid State Map
I recently discovered geofacet package. Examples of graphs you can create with this package looks super fun! I thought it was really neat that I can create my own grid using “Geo Grid Designer”!!
#get_grid_names() --__ you can see what grids are available.
## I can preview the grid first
#grid_design(us_state_grid1)
## Going to use US state map grid in geofacet package
head(us_state_grid1) %__% knitr::kable()
row
col
code
name
6
7
AL
Alabama
7
2
AK
Alaska
5
2
AZ
Arizona
5
5
AR
Arkansas
4
1
CA
California
4
3
CO
Colorado
## Join Candy data &amp; grid so I can create grid map
candy_long_with_coord % left_join(us_state_grid1, by=c(&quot;state_abb&quot; = &quot;code&quot;))
## Replace every other space with new line
## some chocolate names are little too long to lists in one line...
space_to_newline = function(x) gsub(&quot;([^ ]+ [^ ]+) &quot;,&quot;\\1\n&quot;, x)
candy_long_with_coord %
mutate(chocolate_label = map_chr(chocolate, space_to_newline),
facet_label = case_when(ranking==1 ~ &quot;Top Ranked Candies by State&quot;,
ranking==2 ~ &quot;2nd Popular Candies by States&quot;,
ranking==3 ~ &quot;3rd Popular Candies by States&quot;))
candy_long_with_coord %__%
#filter(ranking==1) %__%
ggplot(aes(x=col, y=row)) +
geom_tile(color=&quot;#ffffff90&quot;, aes(fill=volume_per_capita)) +
#geom_text(aes(label=state), size=3.5, color=&quot;#ffffff20&quot;, vjust=4, family=font_rc_light) +
geom_text(aes(label=state_abb),size=5, family=font_rc, vjust=-1, color=&quot;#ffffff90&quot;) +
geom_text(aes(label=chocolate_label), family=font_rc, size=3.5, lineheight=0.8, color=&quot;#ffffff&quot;, vjust=1, fontface=&quot;bold&quot;) +
scale_y_reverse(breaks=NULL) +
scale_x_continuous(breaks=NULL) +
theme_ipsum_rc(base_family=&quot;Roboto Condensed&quot;) +
scale_fill_viridis_c(option=&quot;magma&quot;, alpha=0.8, end=0.7, name=&quot;&quot;, breaks=fivenum(candy_long$volume_per_capita),
labels=c(&quot;&quot;,&quot;&quot;,&quot;__&quot;)) +
facet_wrap(~fct_reorder(facet_label, ranking), ncol=1) +
theme(legend.position = &quot;top&quot;,
legend.key.width = unit(2, &quot;cm&quot;),
legend.justification = &quot;left&quot;) +
labs(title=&quot;Top Ranked Candies by States&quot;,
caption=&quot;Data Source: CandyStore.com's sales from 2007–2015—focusing on the three months leading up to Halloween&quot;, x=&quot;&quot;, y=&quot;&quot;) +
coord_fixed(ratio=0.9)