The Chart Missing From ALL Spreadsheet Software
This example is inspired in the video (https://www.youtube.com/watch?v=5zBg9pH_6bE) by the Minute Physics channel. The idea is to used floating stacked bars to represent data such as temperature ranges. Check the video for a discussion on this specific visualization.
using Vizagrams
using DataFrames
using CSV
path = joinpath(pkgdir(Vizagrams), "docs", "src", "assets", "weather.csv")
df = DataFrame(CSV.File(path))
5×4 DataFrame
Row | Column1 | Ontario | England | Kentucky |
---|---|---|---|---|
String31 | Int64 | Int64 | Int64 | |
1 | winter mean low | -9 | 3 | -3 |
2 | annual mean low | 3 | 8 | 8 |
3 | annual mean | 8 | 12 | 14 |
4 | annual mean high | 13 | 16 | 20 |
5 | summer mean high | 27 | 24 | 30 |
Once we import the data, we must transform the dataframe such that each row contains a pair of temperature values for a given range.
df = stack(df, [:Ontario, :England, :Kentucky], variable_name=:location, value_name=:value)
coalg_pairwise(lst) = [(lst[i],lst[i+1]) for i in 1:length(lst)-1]
df = combine(groupby(df,:location)) do gdf
hcat(
DataFrame(coalg_pairwise(gdf[!,:Column1]),[:label_low,:label_high]),
DataFrame(coalg_pairwise(gdf[!,:value]),[:low_temp,:high_temp])
)
end
df[!,:label_temp] = df[!,:label_low] .* " | " .* df[!,:label_high]
df
12×6 DataFrame
Row | location | label_low | label_high | low_temp | high_temp | label_temp |
---|---|---|---|---|---|---|
String | String31 | String31 | Int64 | Int64 | String | |
1 | Ontario | winter mean low | annual mean low | -9 | 3 | winter mean low | annual mean low |
2 | Ontario | annual mean low | annual mean | 3 | 8 | annual mean low | annual mean |
3 | Ontario | annual mean | annual mean high | 8 | 13 | annual mean | annual mean high |
4 | Ontario | annual mean high | summer mean high | 13 | 27 | annual mean high | summer mean high |
5 | England | winter mean low | annual mean low | 3 | 8 | winter mean low | annual mean low |
6 | England | annual mean low | annual mean | 8 | 12 | annual mean low | annual mean |
7 | England | annual mean | annual mean high | 12 | 16 | annual mean | annual mean high |
8 | England | annual mean high | summer mean high | 16 | 24 | annual mean high | summer mean high |
9 | Kentucky | winter mean low | annual mean low | -3 | 8 | winter mean low | annual mean low |
10 | Kentucky | annual mean low | annual mean | 8 | 14 | annual mean low | annual mean |
11 | Kentucky | annual mean | annual mean high | 14 | 20 | annual mean | annual mean high |
12 | Kentucky | annual mean high | summer mean high | 20 | 30 | annual mean high | summer mean high |
We can now use Vizagrams to express the graphic.
plt = Plot(
config = (;yaxis=(;title="Temperature (°C)")),
data=df,
x = :location,
y = (field=:high_temp,scale_domain=(-10,40), scale_range=(0,200)),
low_y = (field=:low_temp,scale_domain=(-10,40), scale_range=(0,200)),
color = (field=:label_temp,scale_range=["#F28E2B","#4E79A7","#FFBE7D","#A0CBE8"]),
graphic =
∑() do row
S(:fill=>row.color,:stroke=>:white)*
Trail([[row.x,row.low_y],[row.x,row.y]],20)
end
)
draw(plt, height=400)