breakWidth Parameter in Time (Duration) Scales¶

In time scales, the breakWidth parameter accepts a string specifying the interval between breaks, e.g., "6 hours", "30 min", "500 ms".

In [1]:
%useLatestDescriptors
%use lets-plot
In [2]:
val MS = 1L
val SECOND = 1000 * MS
val MINUTE = 60 * SECOND
val HOUR = 60 * MINUTE
val DAY = 24 * HOUR

// Generate 12 hours of data at 15-minute intervals
val rand = java.util.Random(11)
val time = (0 until 12 * HOUR step 15 * MINUTE).toList()
var cumSum = 0.0
val value = List(time.size) { cumSum += rand.nextGaussian(); cumSum }
val data = mapOf("time" to time, "value" to value)
In [3]:
// Automatic breaks (default)
letsPlot(data) { x = "time"; y = "value" } +
    geomLine() +
    scaleXTime() +
    ggtitle("Automatic breaks")
Out[3]:
0 3:00 6:00 9:00 12:00 -2 -1 0 1 2 3 Automatic breaks value time
In [4]:
// Fixed breaks: 1 hour
letsPlot(data) { x = "time"; y = "value" } +
    geomLine() +
    scaleXTime(breakWidth = "1 hour") +
    ggtitle("breakWidth = \"1 hour\"")
Out[4]:
0 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00 9:00 10:00 11:00 12:00 -2 -1 0 1 2 3 breakWidth = "1 hour" value time
In [5]:
// Fixed breaks: 2 hours
letsPlot(data) { x = "time"; y = "value" } +
    geomLine() +
    scaleXTime(breakWidth = "2 hours") +
    ggtitle("breakWidth = \"2 hours\"")
Out[5]:
0 2:00 4:00 6:00 8:00 10:00 12:00 -2 -1 0 1 2 3 breakWidth = "2 hours" value time
In [6]:
// Fixed breaks: 30 minutes
letsPlot(data) { x = "time"; y = "value" } +
    geomLine() +
    scaleXTime(breakWidth = "30 minutes") +
    ggtitle("breakWidth = \"30 minutes\"")
Out[6]:
-0:30 0 0:30 1:00 1:30 2:00 2:30 3:00 3:30 4:00 4:30 5:00 5:30 6:00 6:30 7:00 7:30 8:00 8:30 9:00 9:30 10:00 10:30 11:00 11:30 12:00 -2 -1 0 1 2 3 breakWidth = "30 minutes" value time
In [7]:
// Y-axis with breakWidth
letsPlot(data) { x = "value"; y = "time" } +
    geomLine() +
    scaleYTime(breakWidth = "2 hours") +
    ggtitle("scaleYTime with breakWidth = \"2 hours\"")
Out[7]:
-2 -1 0 1 2 3 0 2:00 4:00 6:00 8:00 10:00 12:00 scaleYTime with breakWidth = "2 hours" time value
In [8]:
// Short range: seconds
val timeSec = (0 until 2 * MINUTE step SECOND).toList()
var cumSumSec = 0.0
val valueSec = List(timeSec.size) { cumSumSec += rand.nextGaussian(); cumSumSec }
val dataSec = mapOf("time" to timeSec, "value" to valueSec)

letsPlot(dataSec) { x = "time"; y = "value" } +
    geomLine() +
    scaleXTime(breakWidth = "15 seconds") +
    ggtitle("breakWidth = \"15 seconds\"")
Out[8]:
0 0:00:15 0:00:30 0:00:45 0:01:00 0:01:15 0:01:30 0:01:45 0:02:00 -7 -6 -5 -4 -3 -2 -1 0 1 2 breakWidth = "15 seconds" value time
In [9]:
// Long range: days
val timeDays = (0 until 14 * DAY step 6 * HOUR).toList()
var cumSumDays = 0.0
val valueDays = List(timeDays.size) { cumSumDays += rand.nextGaussian(); cumSumDays }
val dataDays = mapOf("time" to timeDays, "value" to valueDays)

letsPlot(dataDays) { x = "time"; y = "value" } +
    geomLine() +
    scaleXTime(breakWidth = "1 day") +
    ggtitle("breakWidth = \"1 day\"")
Out[9]:
0 1d 2d 3d 4d 5d 6d 7d 8d 9d 10d 11d 12d 13d 14d -2 0 2 4 6 8 breakWidth = "1 day" value time
In [10]:
// 1 week breaks
letsPlot(dataDays) { x = "time"; y = "value" } +
    geomLine() +
    scaleXTime(breakWidth = "1 week") +
    ggtitle("breakWidth = \"1 week\"")
Out[10]:
0 7d 14d -2 0 2 4 6 8 breakWidth = "1 week" value time