KopfLab / ggstackplot

R package for stacked ggplots
https://ggstackplot.kopflab.org
Other
0 stars 0 forks source link

Feature request: multiple series on same y axis #11

Closed rabergj closed 1 year ago

rabergj commented 1 year ago

The current version allows for each plot in the stack to have just one time series, from what I can tell. It'd be great to be able to plot multiple records with the same y axis without having them show up as separate plots in the stack.

Example from my own work:

xifeng_plot_final

sebkopf commented 1 year ago

this is already possible with regular facetwrap or are you thinking of overlapping plots? either way I think I got a solution for this

rabergj commented 1 year ago

I was thinking with overlapping plots. As in b) of the example figure. Is that already doable and I'm not seeing it?

sebkopf commented 1 year ago

I think it requires the fix to #13 (remove_na = TRUE) to work properly as well as a change to the color parameter (no aesthetics assigned if it isn't set) so make sure you have version 0.2.1. Here's a reprex to test it out - basically, you need to overwrite the ggplot template and define your color aesthetic either at the top or geom level. However, it does raise that question about the legend again (#10). Anyways, please close the issue if you feel this does the job @rabergj

library(ggstackplot)
library(dplyr)
library(ggplot2)

synth_data <- 
  mtcars |>
  cross_join(
    tibble(
      calib = factor(c("a", "b", "c")),
      diff = c(-0.8, 1, 0.8)
    )
  ) |>
  mutate(
    across(c(wt, qsec, drat), ~.x * diff),
    wt = ifelse(calib == "b", wt, NA),
    drat = ifelse(calib != "a", drat, NA)
  ) 

synth_data |>
  ggstackplot(
    x = mpg,
    y = c(wt, qsec, drat),
    template = ggplot() +
      aes(color = calib) +
      geom_line() +
      # the drop = FALSE is key, otherwise you'll loose color matching
      scale_color_brewer(palette = "Set1", drop = FALSE) +
      theme_stackplot()
  )

Created on 2023-06-08 with reprex v2.0.2

rabergj commented 1 year ago

That worked! Both the remove_na argument and the color choices are working, from what I can tell. 🙏