KopfLab / ggstackplot

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

Line breaks when y values have NAs #13

Closed rabergj closed 1 year ago

rabergj commented 1 year ago

Line breaks appear when a given x value doesn't have a corresponding y value for each variable.

# library(ggstackplot)
ggstackplot(
  mtcars |> 
    add_row(mpg = 22, wt = 5, qsec = NA, drat = NA, disp = NA),
  x = mpg,
  y = c(`weight [g]` = wt, qsec, drat, disp),
  color = c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3"),
  overlap = c(0.1, 0.3, 0)
)

image

Came across this when playing with my own data. Since the x values (Age) are generally different for different proxy records, this generates a lot of unwanted line breaks.

image

sebkopf commented 1 year ago

@rabergj fixed in version 0.2.1, please test the new remove_na parameter, e.g.

library(ggstackplot)
library(dplyr)

# default
mtcars |> 
  add_row(mpg = 22, wt = 5, qsec = NA) |>
  ggstackplot(
    x = mpg, y = c(wt, qsec),
    color = c("#E41A1C", "#377EB8")
  )

AXWPwkWcmbzOAAAAAElFTkSuQmCC

# explicit `remove_na` = FALSE
mtcars |> 
  add_row(mpg = 22, wt = 5, qsec = NA) |>
  ggstackplot(
    x = mpg, y = c(wt, qsec),
    color = c("#E41A1C", "#377EB8"),
    remove_na = FALSE
  )
#> Warning: There was 1 warning in `dplyr::mutate()`.
#> ℹ In argument: `gtable = map(.data$plot_w_theme, ggplot2::ggplotGrob)`.
#> Caused by warning:
#> ! Removed 1 rows containing missing values (`geom_point()`).

h+foyKginXSnwAAAABJRU5ErkJggg==