The US fertility heat maps that I made a couple of weeks ago received a lot of attention and one of the questions I’ve been asked is how I produced it, which I describe in this post.
As I mentioned in my previous post, I simply followed the directions specified in this article, but I limited the UN data to the US. Overall, I think the article does a good job of explaining how they created their heat map in Tableau. The reason why I remade the heat map in R is because I was just frustrated with the process of trying to embed the visualization into WordPress. Both Tableau and WordPress charge you to embed visualizations in a format that is aesthetically pleasing. Luckily, recreating the heat map in R was extremely easy and just as pretty, at least in my opinion. Here’s how I did it:
Now type the following in Rstudio:
#load libraries: #if you need to install first, type: install.packages("package_name",dependencies=TRUE) library(tidyverse) library(viridis) #set your working directory to the folder your data is stored in setwd("C:/Users/Stella/Documents/blog/US birth Map") #if you don't know what directory is currently set to, type: getwd() #now import your data us_fertility<-read.csv("USBirthscsv.csv", header=TRUE) #change the file name if you did not use the data I provided (osf.io/h9ta2) #limit to relevant data dta% select(Year, January:December) #gather (i.e., "aggregate") data of interest, in preparation for graphing dta% arrange(Year) #orderring the data by most frequent incidence of births dta %>% group_by(Year) %>% mutate(rank=dense_rank(desc(births))) #plot the data plot<- ggplot(bb2, aes(x =fct_rev(Month), y = Year, fill=rank)) + scale_x_discrete(name="Months", labels=c("Jan", "Feb", "Mar", "Apr", "May","Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) + scale_fill_viridis(name = "Births", option="magma") + #optional command to change the colors of the heat map geom_tile(colour = "White", size = 0.4) + labs(title = "Heat Map of US Births", subtitle = "Frequency of Births from 1969-2014", x = "Month", y = "Year", caption = "Source: UN Data") + theme_tufte() plot+ aes(x=fct_inorder(Month)) #if you want to save the graph dev.copy(png, "births.png") dev.off()
And that’s it! Simple, right?!