In this project, a calendar that displays pie charts is created to display changes in categorical data (air quality index) over a month.
#Create Date and Day of Week (DOW) from Epoch
data[,"Date"] = as.Date(as.POSIXct(data$Epoch,
origin='1970-01-01', tz="Etc/GMT+5"))
data[,"DOW"] = as.factor(weekdays(data[,"Date"]))
#Subset the data to include only Date, DOW, and the average reading over the last 24 hours.
data = data[,c(14,15,11)]
#Air Quality Index (AQI) Calculation
#https://www.airnow.gov/aqi/aqi-basics/
data[,"AQI_Cat"] = factor(NA,
levels = c("Good", "Moderate", "Moderately Unhealthy",
"Unhealthy", "Very Unhealthy", "Hazardous"),
labels = c("Good", "Moderate", "Moderately Unhealthy",
"Unhealthy", "Very Unhealthy", "Hazardous"))
for(i in 1:dim(data)[1]){
if(data[i,"Avg24h"] <= 12){
data[i,"AQI"] = 50/12*(data[i,"Avg24h"])
data[i,"AQI_Cat"] = "Good"}
else if(data[i,"Avg24h"] <= 35.4 & data[i,"Avg24h"] > 12){
data[i,"AQI"] = round((100-51)/(35.4-12)*(data[i,"Avg24h"] - 12) + 51, 1)
data[i,"AQI_Cat"] = "Moderate"}
else if(data[i,"Avg24h"] <= 55.4 & data[i,"Avg24h"] > 35.4){
data[i,"AQI"] = round((150-101)/(55.4-35.4)*(data[i,"Avg24h"] - 35.4) + 101)
data[i,"AQI_Cat"] = "Moderately Unhealthy"}
else if(data[i,"Avg24h"] <= 150.4 & data[i,"Avg24h"] > 55.4){
data[i,"AQI"] = round((200-151)/(150.4-55.4)*(data[i,"Avg24h"] - 55.4) + 151)
data[i,"AQI_Cat"] = "Unhealthy"}
else if(data[i,"Avg24h"] <= 250.4 & data[i,"Avg24h"] > 150.4){
data[i,"AQI"] = round((300-201)/(250.4-150.4)*(data[i,"Avg24h"] - 150.4) + 201)
data[i,"AQI_Cat"] = "Very Unhealthy"}
else if(data[i,"Avg24h"] > 250.5){
data[i,"AQI"] = round((500-301)/(500.4-250.5)*(data[i,"Avg24h"] - 250.5) + 301)
data[i,"AQI_Cat"] = "Hazardous"}}
data = as.data.frame(table(data$Date, data$AQI_Cat))
colnames(data) = c("Date","AQI_Cat","Count")
data$Date = as.Date(data$Date)
data$DOW = factor(weekdays(data$Date), c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"))
data$Week = lubridate::week(data$Date)
data = data[order(data$Date),]
#A function that builds a calendar of pie charts
calendar_plot = function(d, month) {
ggplot(d, aes(x = "", y = Count, fill = AQI_Cat)) +
geom_bar(width = 1, stat = "identity", color = "black", position = position_fill()) +
coord_polar(theta = "y") +
labs(title = paste("AQI: ", month, "-2018", sep = ""),
x = "",
y = "") +
theme(legend.title = element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
panel.background = element_rect(fill = "gray94"),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
strip.text.y = element_blank(),
panel.spacing = unit(0.1, "lines"),
legend.position = "bottom",
legend.text = element_text(size = 11),
legend.spacing.x = unit(4, 'pt')) +
geom_text(data = d,
aes(label = format(Date, '%d'), x = 0, y = 0),
size = 4,
inherit.aes = FALSE) +
facet_grid(Week~DOW) +
scale_fill_manual(values=c("Good" = "#26B52A",
"Moderate" = "#E2F224",
"Moderately Unhealthy" = "#F2B224",
"Unhealthy" = "#F22424",
"Very Unhealthy" = "#7D18B4",
"Hazardous" = "#000000"),
drop = FALSE)}
Description: This visualization provides a breakdown of the air quality index (AQI) by day for a single month.
calendar_plot(subset(data, format(data$Date, '%m') == '11'), '11')