Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run length encoding stat #12

Closed
teunbrand opened this issue May 22, 2020 · 1 comment
Closed

Run length encoding stat #12

teunbrand opened this issue May 22, 2020 · 1 comment
Labels
enhancement New feature or request plan This probably will be worked out in the future

Comments

@teunbrand
Copy link
Owner

To do basically this, but then in a stat function:

library(ggplot2)

df <- data.frame(
  x = seq(0, 10, length.out = 100),
  y = sin(seq(0, 10, length.out = 100))
)

rle <- rle(df$y > 0.5)
start <- {end <- cumsum(rle$lengths)} - rle$lengths + 1

df2 <- data.frame(
  xmin = df$x[start],
  xmax = df$x[end],
  value = rle$values
)

ggplot(df) +
  geom_rect(data = df2, aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf, fill = value)) +
  geom_point(aes(x, y))

Created on 2020-05-22 by the reprex package (v0.3.0)

@teunbrand teunbrand added enhancement New feature or request plan This probably will be worked out in the future labels May 22, 2020
@teunbrand
Copy link
Owner Author

This now works:

library(ggplot2)
library(ggh4x)

df <- data.frame(
    x = seq(0, 10, length.out = 100),
    y = sin(seq(0, 10, length.out = 100)*2)
)

ggplot(df) +
    stat_rle(aes(x, label = y > 0.5),
             align = "center") +
    geom_point(aes(x, y))

Created on 2020-06-02 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request plan This probably will be worked out in the future
Projects
None yet
Development

No branches or pull requests

1 participant