TLDR: Creating weather radials withh highcharter and ggplot2

data-visualization
highcharts
ggplot2
minipost
Author

Joshua Kunst Fuentes

Published

March 24, 2016

I was surfing by the deep seas of the web and I found the Brice Pierre de la Briere’s blocks and I saw the weather radials which originally are a poster collection. Brice uses D3 and he used D3 very well and I love D3 but I’m in a rookie level to do something like him. D3 is not for everybody and surely not for me, I would love to lear more but family, work and R has priority over D3 so how can I do something like that. Well… We have R & highcharter. So let’s try.

We’ll use the same data as Brice https://www.wunderground.com/.

Code
``````library(tidyverse)
library(highcharter)
library(lubridate)

glimpse(df)``````
``````Rows: 365
Columns: 23
\$ date                         <chr> "2014-1-1", "2014-1-2", "2014-1-3", "2014…
\$ `Max TemperatureC`           <dbl> 13, 17, 18, 19, 19, 16, 16, 15, 16, 16, 1…
\$ `Mean TemperatureC`          <dbl> 9, 12, 12, 13, 13, 11, 12, 13, 13, 12, 11…
\$ `Min TemperatureC`           <dbl> 5, 6, 7, 6, 7, 7, 8, 11, 11, 9, 8, 8, 6, …
\$ `Dew PointC`                 <dbl> 7, 7, 8, 8, 3, 7, 11, 11, 10, 9, 11, 8, 7…
\$ `MeanDew PointC`             <dbl> 4, 4, 5, 6, 0, 4, 7, 9, 8, 8, 8, 6, 3, 3,…
\$ `Min DewpointC`              <dbl> 2, 1, 1, 1, -3, 0, 4, 8, 6, 6, 6, 2, 0, -…
\$ `Max Humidity`               <dbl> 83, 80, 80, 93, 65, 71, 86, 86, 89, 89, 9…
\$ `Mean Humidity`              <dbl> 69, 58, 59, 65, 45, 61, 70, 79, 73, 75, 8…
\$ `Min Humidity`               <dbl> 55, 36, 37, 36, 24, 51, 53, 72, 57, 60, 7…
\$ `Max Sea Level PressurehPa`  <dbl> 1023, 1022, 1019, 1019, 1023, 1025, 1023,…
\$ `Mean Sea Level PressurehPa` <dbl> 1022, 1020, 1016, 1016, 1021, 1023, 1022,…
\$ `Min Sea Level PressurehPa`  <dbl> 1020, 1018, 1014, 1015, 1019, 1022, 1021,…
\$ `Max VisibilityKm`           <dbl> 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 1…
\$ `Mean VisibilityKm`          <dbl> 15, 16, 16, 9, 16, 15, 14, 15, 15, 15, 16…
\$ `Min VisibilitykM`           <dbl> 11, 16, 14, 0, 16, 11, 11, 5, 8, 10, 10, …
\$ `Max Wind SpeedKm/h`         <dbl> 14, 10, 23, 24, 23, 14, 24, 32, 40, 24, 2…
\$ `Mean Wind SpeedKm/h`        <dbl> 1, 2, 6, 4, 8, 4, 7, 15, 19, 9, 10, 11, 4…
\$ `Max Gust SpeedKm/h`         <dbl> 16, 10, 26, 29, 24, 16, 27, 39, 50, 27, 3…
\$ Precipitationmm              <chr> "0.00", "0.00", "0.00", "0.00", "0.00", "…
\$ CloudCover                   <dbl> 3, 3, 3, 2, 0, 5, 5, 7, 5, 4, 4, 2, 1, 0,…
\$ Events                       <chr> NA, NA, NA, "Fog", NA, NA, "Rain", NA, NA…
\$ WindDirDegrees               <dbl> 290, 146, 297, 266, 125, 75, 304, 281, 28…``````
Code
``````df <- df %>%
rename_all(str_to_lower) %>%
rename_all(str_replace, "\\s+", "_") %>%
mutate(
id = seq(nrow(df)),
date2 = as.Date(ymd(date)),
tmstmp = datetime_to_timestamp(date2),
month = month(ymd(date))
)

dsmax <- df %>%
select(tmstmp, max_temperaturec)

dsmin <- df %>%
select(tmstmp, min_temperaturec)``````

## First try

Here we test and chart the data in the most simple way. A line time.

Code
``````highchart() %>%
hc_chart(type = "line") %>%
hc_xAxis(
type = "datetime",
tickInterval = 30 * 24 * 3600 * 1000,
labels = list(format = "{value: %b}")
) %>%
hc_yAxis(min = 0,
labels = list(format = "{value} C")) %>%
type = "line",
hcaes(tmstmp, max_temperaturec),
name = "max") %>%
type = "line",
hcaes(tmstmp, min_temperaturec),
name = "min")``````

Everything seems fine. We now a little bit about the data

## To polar coordinates and columnrange!

Now, we need to change to polar cordinates to get the “radial” part, this is get using the `polar = TRUE` argument in the `hc_chart` function. Then add the data in the `columnrange` type.

Code
``````# Some tooltips to make it a little *intercative*
x <- c("Min", "Mean", "Max")
y <- sprintf("{point.%s} ºC", c("min_temperaturec", "mean_temperaturec", "max_temperaturec"))
tltip <- tooltip_table(x, y)

hchart(
df,
type = "columnrange",
hcaes(x = date2, low = min_temperaturec, high = max_temperaturec, color = mean_temperaturec),
showInLegend = FALSE
) %>%
hc_chart(
polar = TRUE
) %>%
hc_xAxis(
gridLineWidth = 0.5,
type = "datetime",
tickInterval = 30 * 24 * 3600 * 1000,
labels = list(format = "{value: %b}")
) %>%
hc_yAxis(
max = 30,
min = -10,
labels = list(format = "{value} ºC"),
showFirstLabel = FALSE
) %>%
hc_tooltip(
useHTML = TRUE,
pointFormat = tltip
)``````

Yay :D! A beautiful chart same as the d3 version and only using R. So sweet! I’m happy with the result. This is not a standar chart but is a king of artistic. What do you think? Any other examples to test this type of chart?

## Bonus Track: {ggplot2} version

It’s really really easy to do this type of chart in ggplot2 using `geom_linerange` and `geom_polar`:

Code
``````library(scales)

ggplot(
df,
aes(
date2,
ymin = min_temperaturec,
ymax = max_temperaturec,
color = mean_temperaturec
)
) +
geom_linerange(size = 1.3, alpha = 0.75) +
scale_color_viridis_c(NULL, option = "A") +
scale_x_date(labels = date_format("%b"), breaks = date_breaks("month")) +
ylim(-10, 35) +
labs(
title = "San Francisco Wather Radial",
subtitle = "It would be nice if someone do this with the animation package",
caption = "Other example for ggplot2 vs base #boring but #fun",
x = NULL,
y = NULL
) +
coord_polar() +
theme(legend.position = "bottom")``````

Nice!

Searching I found someone do this:

Always exist someone who did what you did before you.

## Citation

BibTeX citation:
``````@online{kunstfuentes2016,
author = {Joshua Kunst Fuentes},
title = {How to: {Weather} {Radials}},
date = {2016-03-24},