Radial Histogram

Visualising dataset of temperature in San Francisco in 2014,  using a Radial Histogram.

R code

Sys.setlocale("LC_ALL", 'en_US.UTF-8') #handle special characters


# Libraries -------------------------------------------------------------

library(tidyverse)
library(scales)
library(RColorBrewer)
library(lubridate)

# Data -------------------------------------------------------------

inspiration <- "http://jkunst.com/r/how-to-weather-radials"
source <- "data/San Francisco Weather.csv"

dfTemperature <- read_csv(source)
dfTemperature <- dfTemperature %>% select("date", contains("Temperature")) # only retain variables for date and temperature

# format variable names - lower case, ' removed, _ added
names(dfTemperature) <- names(dfTemperature) %>% str_to_lower() %>% str_replace("\\s+", "_")

dfTemperature <- dfTemperature %>% mutate(date = as.Date(ymd(date) ) )

absMin <- min(dfTemperature$min_temperaturec)
absMax <- max(dfTemperature$max_temperaturec)
absMean <- mean(c(absMin, absMax))

absMeanMin <- min(dfTemperature$mean_temperaturec)
absMeanMax <- max(dfTemperature$mean_temperaturec)
absMeanMean <- mean(c(absMeanMin, absMeanMax))

# the days with maximum and minimum temperatures
dayMax <- dfTemperature$date[dfTemperature$max_temperaturec == absMax]
dayMin <- dfTemperature$date[dfTemperature$min_temperaturec == absMin]

# Titles
plotTitle <- "San Francisco temperature in 2014"
plotCaption <- paste("inspired by:", inspiration)
plotSubtitle <- paste( "the coldest day was ", strftime(dayMin[1], "%B %d"), " - ", "it was hottest on ", strftime(dayMax[1], "%B %d"), sep = "")
plotLegendTitle <- "average daily temperature"

# Theme -------------------------------------------------------------------

fontFamily <- "PT Serif"
colrBackground <- "#ffffff"
colrPrim <- "#2a2a2a"
colrMed <- "#777777"
colrSub <- "#e1e1e1"
summaryBackground <- "#fafafa"
titleFontSize <- 14
subtitleFontSize <- 9
axisFontSize <- 9
axisType <- "dashed"
shiftAnnotation <- absMean
fix_annotationFontSize <- 3

update_geom_defaults("text", list(family = fontFamily, size = axisFontSize/fix_annotationFontSize)) # update default geom text

themePlot <- function(){
theme(

text = element_text(family = fontFamily),

panel.background = element_rect(fill = NA, colour = NA), 
panel.border = element_blank(),
panel.grid = element_blank(),
#panel.grid.major.x = element_line(linetype = "dotted", color = colrSub ),
#panel.grid.major.y = element_line(linetype = "dotted", color = colrSub ),

plot.margin = unit( c(32, 32, 32, 32), "points"),

# plot.title = element_text(size = titleFontSize, color = colrPrim, face = "plain", hjust = 0, margin = unit(c(0, 0, 12, 0), "pt") ),
# plot.subtitle = element_text(size = subtitleFontSize, color = colrPrim, face = "plain", hjust = 0, margin = unit(c(0, 0, 40, 0), "pt") ),
plot.title = element_blank(),
plot.subtitle = element_blank(),
plot.caption = element_text(size = subtitleFontSize, color = colrMed, face = "italic", hjust = 0.5, margin = unit(c(40, 0, 0, 0), "pt") ),

axis.text.y = element_blank(),
axis.ticks = element_blank(),

axis.text.x = element_text(color = colrMed, face = "italic"),

legend.text = element_text(size = axisFontSize, color = colrPrim, face = "plain"),
legend.title = element_text(size = axisFontSize, color = colrPrim, face = "italic", hjust = 0.5, margin = unit(c(0, 0, 4, 0), "pt") ),
legend.text.align = 0.55,
legend.background = element_blank(),
legend.justification = "center", 
legend.key = element_blank(),
legend.key.size = unit(10, "pt"),
legend.direction = "horizontal", 
legend.position = c(0.5, 0.32)

)
}

# Plot -------------------------------------------------------------------

plot <- ggplot() + 

# Min and Max temperature lines
geom_hline(data = dfTemperature, yintercept = absMin, size = 0.5, linetype = "dotted", color = colrSub) +
geom_hline(data = dfTemperature, yintercept = absMax, size = 0.5, linetype = "dotted", color = colrSub) +

#geom_linerange(data = dfTemperature, aes(x = date, ymin = min_temperaturec, ymax = max_temperaturec, color = mean_temperaturec), size = 1, alpha = 1) +
geom_segment(data = dfTemperature, aes(x = date, xend = date, y = min_temperaturec, yend = max_temperaturec, color = mean_temperaturec), size = 1, alpha = 1, lineend = "round") +

scale_colour_distiller(palette = "RdYlBu",
name = plotLegendTitle, 
limits = c(absMeanMin, absMeanMax),
labels = c(paste(absMeanMin, "°", sep = ""), paste(absMeanMean, "°", sep = ""), paste(absMeanMax, "°", sep = "")),
breaks = c(absMeanMin, absMeanMean, absMeanMax)
) + # use name = element_blank() to hide scale name

scale_x_date(labels = date_format("%b"), breaks = date_breaks("month")) + 
scale_y_continuous(limits = c(-absMax*2, absMax*1), labels = c(absMin, absMax), breaks = c(absMin, absMax)) +

guides(color = guide_colorbar(title.position = "top", barwidth = 8, barheight = 0.15, ticks = FALSE) ) +
labs(title = plotTitle, subtitle = plotSubtitle, caption = plotCaption, x = NULL, y = NULL) +
coord_polar() +

# Highlight dates with min and max temperatures
geom_point(aes( x = dayMax, y = absMax), shape = 16, size = 7, alpha = 0.75, color = brewer.pal(11, "RdYlBu")[2] ) +
geom_point(aes( x = dayMin, y = absMin), shape = 16, size = 7, alpha = 0.75, color = brewer.pal(11, "RdYlBu")[10] ) +

# annotate
annotate("text", x= dayMax, y = absMax, hjust = 0.5, vjust = 0.5, angle = 0, size = 2.5, fontface = "bold", color = "white", label = paste(absMax, "°", sep = "") ) +
annotate("text", x= dayMin, y = absMin, hjust = 0.5, vjust = 0.5, angle = 0, size = 2.5, fontface = "bold", color = "white", label = paste(absMin, "°", sep = "") ) +

annotate("text", x = dfTemperature$date[1], y = -60, hjust = 0.5, vjust = 0, size = 4, fontface = "plain", color = colrPrim, label = plotTitle ) +
annotate("text", x = dfTemperature$date[1], y = -70, hjust = 0.5, vjust = 0, size = 3, fontface = "italic", color = colrPrim, label = plotSubtitle ) +
themePlot()

plot

# Export -------------------------------------------------------------------

ggsave(plot, width = 10, height = 10, device = cairo_pdf, filename = paste("export/", plotTitle, ".pdf", sep = "") )
ggsave(plot, width = 10, height = 10, dpi = "retina", filename = paste("export/", plotTitle, ".png", sep = "") )

Keyshape prototype