Abstract
Create a raster plot of line density using a 'ggplot2' statistic implementing the DenseLines algorithm, which "normalizes time series by the arc length to compute accurate densities" (Moritz and Fisher, 2018) <a href="https://doi.org/10.48550%2FarXiv.1808.06019" target="_top">doi:10.48550/arXiv.1808.06019</a>.
Introduction
Visualizing large-scale time series data often results in the "hairball" effect, where overlapping lines obscure patterns and distributions. Traditional methods like alpha blending or point-based binning fail to capture the continuous nature of lines accurately. Specifically, steep line segments can appear less dense than horizontal ones if only point counts are considered. The gglinedensity package addresses this by implementing the DenseLines algorithm within the ggplot2 framework, allowing for clear and accurate heatmaps of thousands of overlapping time series.
The DenseLines Algorithm
The core of gglinedensity is arc-length normalization. Instead of counting points, the algorithm calculates the length of line segments within each bin. This ensures that the visual weight of a trend is proportional to the space it occupies, regardless of its slope. This mathematically rigorous approach prevents the distortion of density in volatile versus stable data regions.
To handle the computational intensity of these calculations, the package utilizes a Rust-based backend. This provides high performance and memory safety while maintaining a seamless R interface for the user.
Integration with ggplot2
The package introduces two new statistics: stat_line_density() for standard ordered time series and stat_path_density() for more general paths. These functions integrate directly into the ggplot2 workflow, allowing users to map computed density values to aesthetics like color or size.
As shown in the documentation, users can control the granularity of the heatmap using the bins or binwidth arguments. The orientation parameter allows the algorithm to automatically determine the primary axis from the aesthetic mapping, ensuring flexibility across different plot types.
Key Results and Computed Variables
The package provides several computed variables for delayed evaluation:
count: Raw number of lines per bin.density: Arc-length normalized density.ncount: Count scaled to [0, 1].ndensity: Density scaled to [0, 1].
By mapping after_stat(ndensity) to the fill color of a raster, users can transform cluttered line plots into "Line Density Charts" that reveal the modal path and underlying distribution of the data.
Limitations
While powerful, gglinedensity has specific requirements:
- Ordering:
stat_line_density()requires data to be ordered along the orientation axis. Unordered data requiresstat_path_density(). - System Requirements: Compilation from source requires a modern Rust compiler (
rustc >= 1.70.0) andcargo, which may pose challenges in some IT environments.
Conclusion
gglinedensity offers a robust solution for high-dimensional time series visualization. By leveraging the DenseLines algorithm and a high-performance Rust backend, it enables R users to create accurate, slope-agnostic heatmaps that resolve the limitations of traditional line plotting.
Summary updated