MyNixOS website logo
Description
Add Day/Night Patterns to 'ggplot2' Plots
It provides a custom 'ggplot2' geom to add day/night patterns to plots. It visually distinguishes daytime and nighttime periods. It is useful for visualizing data that spans multiple days and for highlighting diurnal patterns.

R-CMD-check CRAN status

ggdaynight

ggdaynight is an R package that adds a day/night pattern to ggplot2 plots. Daytime is represented by rectangles filled with a specified day_fill color and nighttime by rectangles filled with a specified night_fill color. The pattern is created along the x-axis, which must be a datetime variable.

Installation

You can install the released version of ggdaynight from CRAN:

install.packages("ggdaynight")

You can install the development version of ggdaynight from GitHub with:

# Install devtools if not already installed
if (!requireNamespace("devtools", quietly = TRUE)) {
  install.packages("devtools")
}

# Install ggdaynight from GitHub
devtools::install_github("GabrielSlPires/ggdaynight")

Usage

Basic Usage with Default Parameters

library(ggplot2)
library(ggdaynight)

ggplot(daynight_temperature, aes(datetime, temperature)) +
  geom_daynight() +
  geom_point() +
  theme_classic()

basic plot

Basic Usage with Faceting by Sensor

ggplot(daynight_temperature, aes(datetime, temperature)) +
  geom_daynight() +
  geom_point() +
  facet_wrap(vars(sensor)) +
  theme_classic()

facet plot

Usage with Lines and Color by Sensor

ggplot(daynight_temperature, aes(datetime, temperature, color = sensor)) +
  geom_daynight() +
  geom_line(linewidth = 1) +
  theme_classic()

color plot

Custom Day and Night Fill Colors, Custom Sunrise and Sunset Times, and Adjusted Alpha

ggplot(daynight_temperature, aes(datetime, temperature, group = sensor)) +
  geom_daynight(day_fill = "yellow", night_fill = "blue",
                sunrise = 5, sunset = 20, alpha = 0.5) +
  geom_line(linewidth = 1) +
  theme_classic()

custom plot

Function Details

geom_daynight

Adds a day/night pattern to a ggplot object.

Arguments
  • day_fill: The fill color for daytime rectangles. Defaults to "white".
  • night_fill: The fill color for nighttime rectangles. Defaults to "grey30".
  • sunrise: The hour at which daytime starts. Defaults to 6 (6 AM).
  • sunset: The hour at which nighttime starts. Defaults to 18 (6 PM).
  • ...: Additional arguments passed to geom_rect.
Value

A ggplot2 layer representing the day/night pattern.

Contributing

Please feel free to submit issues and pull requests to the repository. For major changes, please open an issue first to discuss what you would like to change.

Metadata

Version

0.1.3

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows