Skip to content

seasmith/ggdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

ggdf

ggplot2 dream functions

Table of Contents

Intent

Document functions I have no real intention of implementing but would love to see in ggplot2 or some extension.

Geoms

geom_sf_density

2D-density for sfc POINT objects. Output can be one of point density or contoured lines.

# Data has units of [m] (meter)
data %>%
  ggplot() +
  geom_sf_density(aes(fill = stat(level)), 
                  grid.cell = c(1e3, 1e3),
                  geom = "polygon", 
                  contour = FALSE) +
  scale_fill_viridis()

geom_sf_binned

Bin counts for sfc POINT objects.

geom_sf_krige

Need to read more into variogram modelling before a current and idealized implementation can be fully realized.

library(ggplot2)
library(dplyr)
library(gstat)

# Current implementation ----------------------------------------------
# WIP

# Idealized implementation --------------------------------------------

# WIP
df %>%
  ggplot() +
  geom_sf_krige(aes(formula = x ~ y))

geom_line_shape/geom_path_shape

Inspired by D3's curve API. Allows user to specify interpolation method (spline, step, step-before, step-after, etc) and closure (do start and end points connect).

library(ggplot2)
library(dplyr)

# Current implementation ----------------------------------------------
# WIP

# Idealized implementation --------------------------------------------
# WIP
df %>%
  ggplot(aes(x, y)) +
  geom_line_shape(method = "step", args = list(when = "after"))
  
# -- OR --
df %>%
  ggplot(aes(x, y)) +
  geom_line_shape(stat = "step_after")

# default stat is "step_middle"
df %>%
  ggplot(aes(x, y)) +
  geom_line_shape()

# connect controls whether to connect start and end point
df %>%
  ggplot(aes(x, y)) +
  geom_line_shape(stat = "xspline", connect = TRUE)

Facets

facet_break

Breaks a time series chart into multiple facets (one column, many rows).

library(ggplot2)
library(dplyr)

# Current implementation ----------------------------------------------

txhousing %>%
    filter(city == "Abilene") %>%
    mutate(date = paste(year, month, "1", sep = "-"),
           date = as.Date(date),
           rbreak = ceiling(seq(1, 187) / 34)) %>%
    ggplot(aes(date, sales)) +
    geom_line() +
    facet_wrap(vars(rbreak), scales = "free_x", ncol = 1)

    
# Idealized implementation --------------------------------------------

txhousing %>%
    ggplot(aes(date, sales)) +
    geom_line() +
    facet_break(vars(date), breaks = 6)

facet_kmeans

Performs kmeans clustering on two variables; geom_point() compliments this facet.

# Current Implementation ----------------------------------------------

# Number of clusters
n <- 2:4 

kdata <- iris[, c("Sepal.Length", "Petal.Length")]

# Run kmeans
klusters <- lapply(n, kmeans, x = kdata)

# Extract clusters
clusters <- Reduce(cbind, lapply(klusters, `[[`, "cluster"))

colnames(clusters) <- paste0("k", n)

kdata_long <- cbind(kdata, clusters) %>%
  tidyr::gather(... = k2:k4)

kdata_long %>%
  ggplot(aes(x = Sepal.Length, 
             y = Petal.Length, 
             color = factor(value))) +
  geom_point() +
  facet_wrap(vars(key))


# Idealized Implementation --------------------------------------------

iris %>%
  ggplot(aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point() +
  facet_kmeans(
    k = 2:4,  # passed on to kmeans()
    aesthetics = c("color") # or "fill" if pch = 21
    )

# Also facet_sf_kmeans()...wip
# Can also extend to dbscan::dbscan, cluster::pam, etc.

About

ggplot2 dream functions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published