ggh4x icon indicating copy to clipboard operation
ggh4x copied to clipboard

Geom suggestion

Open tjebo opened this issue 2 years ago • 1 comments

Hi Teun, I wondered if this is something you'd be interested in: https://stackoverflow.com/questions/67644237/alternating-color-of-individual-dashes-in-a-geom-line

I've packaged this in my own little not so safe ggtrail, but I thought maybe you'd like it and if so, I am happy to pull request.

Below I'm just copying the reprex from that thread for your convenience

library(ggplot2)
## attaching just for demonstration purpose
library(patchwork)

# geom_colorpath
# @description lines with alternating color "just for the effect".
# @name colorpath
# @examples
# @export
StatColorPath <- ggproto("StatColorPath", Stat,
                         compute_group = function(data, scales, params,
                                                  n_seg = 20, n = 100, cols = c("black", "white")) {
                           # interpolate
                           d <- approx(data$x, data$y, n = n)
                           # create start and end points for segments
                           d2 <- data.frame(
                             x = head(d$x, -1), xend = d$x[-1],
                             y = head(d$y, -1), yend = d$y[-1]
                           )
                           # create vector of segment colors
                           d2$color <- rep(cols, each = ceiling((n - 1) / n_seg), length.out = n - 1)
                           d2
                         },
                         required_aes = c("x", "y")
)

# @rdname colorpath
# @import ggplot2
# @inheritParams ggplot2::layer
# @inheritParams ggplot2::geom_segment
# @param n_seg number of segments along line, according to taste
# @param n number of points at which interpolation takes place
#   increase if line takes sharp turns
# @param cols vector of alternating colors
# @export
geom_colorpath <- function(mapping = NULL, data = NULL, geom = "segment",
                           position = "identity", na.rm = FALSE, show.legend = NA,
                           inherit.aes = TRUE, cols = c("black", "white"),
                           n_seg = 20, n = 100, ...) {
  layer(
    stat = StatColorPath, data = data, mapping = mapping, geom = geom,
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, cols = cols, n = n, n_seg = n_seg,...)
  )
}

## examples
dat <- data.frame(x = seq(2,10, 2), y = seq(4,20, 4))

p1 <- ggplot(dat, aes(x = x, y = y)) +
  geom_colorpath()+
  ggtitle("Default colors")
p2 <- ggplot(dat, aes(x, y)) +
  geom_colorpath(cols = c("red", "blue"))+
  ggtitle("Two colors")

p3 <- ggplot(dat, aes(x, y)) +
  geom_colorpath(cols = c("red", "blue", "green"))+
  ggtitle("Three colors")

p4 <- ggplot(dat, aes(x, y)) +
  geom_colorpath(cols = c("red", "blue", "green", "white"))+
  ggtitle("Four colors")

wrap_plots(mget(ls(pattern = "p[1-9]")))

air_df <- data.frame(x = 1: length(AirPassengers), y = c(AirPassengers))

a1 <- ggplot(air_df, aes(x, y)) +
  geom_colorpath(cols = c("red", "blue", "green"))+
  ggtitle("Works also with more complex curves")

a2 <- ggplot(air_df, aes(x, y)) +
  geom_colorpath(cols = c("red", "blue", "green"), n_seg = 150)+
  ggtitle("... more color segments")

a1 / a2

Created on 2022-06-22 by the reprex package (v2.0.1)

tjebo avatar Jun 22 '22 13:06 tjebo

Hi Tjebo!

I'm sorry for not responding more quickly to this (have been busy lately). The stat seems pretty cool, and maybe this is my lack of imagination, but I can't really see how this might be used in data visualisation. If you're happy to host it in {ggtrail} then it's nice and available for people who want to use it :)

Best, Teun

teunbrand avatar Aug 13 '22 07:08 teunbrand

@teunbrand Sorry I never replied ... no worries at all. more of a fun geom :)

tjebo avatar Mar 19 '23 23:03 tjebo