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

Incremental graphs in ggplot2 #24

Open
weiyangtham opened this issue Mar 6, 2019 · 2 comments
Open

Incremental graphs in ggplot2 #24

weiyangtham opened this issue Mar 6, 2019 · 2 comments

Comments

@weiyangtham
Copy link

I am a fan of showing graphs layer by layer in slideshows. I find that my workflow ends up being something like

  1. Make the final version of the graph that I want
  2. Deconstruct the graph into the desired increments

This ends up involving a lot of copying and pasting. It's not the worst thing since ggplot syntax fits well with the layering idea, but I have found it to be enough work that I don't always do it. I've thought it would be nice to have some functions that can take a plot and show you just the parts you want. Roughly,

p = ggplot(mtcars, aes(mpg, wt)) + geom_point()
show_x_axis(p) # only shows x-axis
show_one_point(p) # only show one point so you can explain it
@maurolepore
Copy link

Sounds cool! Have you thought about how to implement it?
My first (and only idea) would be to edit the final call to the ggplot with the rlang package by @lionel- et al.

@wlandau
Copy link

wlandau commented Mar 7, 2019

Yeah, this sounds awesome! Such a natural consequence of ggplot2's layering paradigm.

In fact, ggplot2 objects already have a layers element.

library(ggplot2)
pl <- ggplot(mtcars) +
  geom_line(aes(x = wt, y = mpg)) +
  ggtitle("mpg vs wt") +
  geom_point(aes(x = wt, y = mpg, color = as.factor(cyl))) +
  ggtitle("mpg vs wt with line")

pl

pl$layers
#> [[1]]
#> mapping: x = ~wt, y = ~mpg 
#> geom_line: na.rm = FALSE
#> stat_identity: na.rm = FALSE
#> position_identity 
#> 
#> [[2]]
#> mapping: x = ~wt, y = ~mpg, colour = ~as.factor(cyl) 
#> geom_point: na.rm = FALSE
#> stat_identity: na.rm = FALSE
#> position_identity

# Remove a layer.
pl$layers <- pl$layers[-1]

pl

Created on 2019-03-07 by the reprex package (v0.2.1)

But that's not what we want because both frames have "with line" in the title. Hence, @maurolepore's idea of computing on the language. A couple more ideas:

  1. Walk the abstract syntax tree (AST), discover + signs, and make a new increment for each one.
  2. Define increments manually, which would still require walking the AST to discover calls to increment().
pl <- ggplot(mtcars) +
  geom_line(aes(x = wt, y = mpg)) +
  ggtitle("mpg vs wt") +
  increment() +
  geom_point(aes(x = wt, y = mpg, color = as.factor(cyl))) +
  ggtitle("mpg vs wt with line") +
  increment() 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants