Time tick issue on x-axis

I’m trying to plot a time-related histogram where the x-axis is times in a day, but I want the ticks to increase every 30 minutes instead of the crazy amount of ticks defaulted by Gadfly.test

Any other plotting package solution are also welcomed, Gadfly seems to be unable to handle Time from DateTime.

Plots should be able to do that, but it’d be easier to check if you could post an MWE.

Here’s a quick solution (probably can be improved on). It will work with DateTime or Time types:

import Cairo
using Dates, Gadfly

halfhour(x) = hour.(x) + (minute.(x).>30)*0.5

n = 1000
date1 = DateTime(2018,1,1) + Hour.(rand(0:23, n)) + Minute.(rand(0:59, n)) + Second.(rand(0:59,n))
time1 = Time.(date1) 

xticks = Guide.xticks(ticks=[0:2:24;])
pa = plot(x=halfhour.(date1), Geom.histogram(bincount=48), xticks)
pb = plot(x=halfhour.(time1), Geom.histogram(bincount=48), xticks)
draw(PNG(8inch,4inch), hstack(pa, pb))
1 Like

Just for the record, the inbuilt DateTime axis handling seems broken for histograms in Plots as well (so you could do something like the above, but it won’t do it automatically).

1 Like

With VegaLite.jl it is easy to bin by hour:

using VegaLite, DataFrames, Dates

n = 1000
df = DataFrame(time = DateTime(2018,1,1) + Hour.(rand(0:23, n)) + Minute.(rand(0:59, n)) + Second.(rand(0:59,n)))

df |> @vlplot(:bar, x="hours(time):o", y="count()")

visualization%20(2)

I don’t think one can aggregate by half hours, though… The vega-lite documentation generally describes the existing functionality.

I want to make a histogram of Dates. For example from 2017.1.1 till 2018.1.1. How would I do that? And how do I get more than one bar per month?

Here is the VegaLite.jl way:

using VegaLite, DataFrames, Dates

n = 1000

 df = DataFrame(time = rand(DateTime(2018,1,1):Hour(1):DateTime(2019,1,1), n))

df |> @vlplot(:bar, x="yearmonthdate(time):o", y="count()")

Note how the only difference to the previous example is that I now use the yearmonthdate time unit instead of the hours time unit. You can see all time units that are supported by vega-lite here.

You’d probably want to adjust the width of the bars in this example, otherwise the plot gets a bit wide :slight_smile:

1 Like

Thank you very much. I really like your work! Keep it up :+1:

1 Like