KopfLab / ggstackplot

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

X axes not synched #17

Closed rabergj closed 1 year ago

rabergj commented 1 year ago

Seems that a recent fix (remove_na? colors?) generated another issue - the x axes are no longer synched.

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)
  ) |> 
  #  ***changing the x axis for a subset of the data***
  mutate(mpg = ifelse(calib == "a", mpg * 2, mpg))

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()
  )

image

sebkopf commented 1 year ago

@rabergj they actually are, try it with both_axes = TRUE - the problem is that the legends are not lined up (don't think that's possible) and since the axis ticks and labels have different widths, the legends get pushed out. This leads back to issue #10 - how do we deal with legends?

Close this one though?

rabergj commented 1 year ago

@sebkopf sorry, let me give a little more detail for the issue.

In the first case, the red line has a maximum value of x ~34:

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),
    both_axes = T,
    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()
  )

image

If I double the x values for just that red line, though, the x-axis doesn't update to reflect that. It still looks like the maximum value for the red line is ~34, even though it should now be ~68. It looks like it's just compressed that middle plot to fit the original axis.

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)
  ) |> 
  # changing the x axis for a subset of the data
  mutate(mpg = ifelse(calib == "a", mpg * 2, mpg))

synth_data |>
  ggstackplot(
    x = mpg,
    y = c(wt, qsec, drat),
    both_axes = T,
    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()
  )

image

sebkopf commented 1 year ago

oh i see what you mean! yes, the x axis at present assume identical range, i'll make sure to fix that

sebkopf commented 1 year ago

btw @rabergj the way to really show this problem is by turning simplify_shared_axis = FALSE to get all x-axes displayed (btw happy for input about all argument names too, if they don't make sense to any of you, they won't make sense to other users either)

sebkopf commented 1 year ago

question @rabergj implement common shared axis range (i.e. range synched x axis) only if simplify_shared_axis = TRUE or always? (is there a point in having stacked plots with unsynched x axis or should folks just use a facet wrap at that point?)

sebkopf commented 1 year ago

@rabergj this should be fixed in version 0.2.6, please test and reopen if not