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

Combined plot with uniform scale and scale breaks #797

Open
lucolotto opened this issue Oct 10, 2022 · 2 comments
Open

Combined plot with uniform scale and scale breaks #797

lucolotto opened this issue Oct 10, 2022 · 2 comments

Comments

@lucolotto
Copy link

Hi Indrajeet! Sorry to bother you.
I've been using extensively your package and now I have a quick question that is related to having a scale Y with breaks.
Assume I have the following code (from the gapminder package, with the life expectancy of one year artificially increased to simulate the need for a break in the Y axis):

set.seed(123)
library(ggstatsplot)

## let's split the data frame and create a list by years of interest
year_list <- gapminder::gapminder %>%
  dplyr::filter(year %in% c(1967, 1987, 2007), continent != "Oceania") %>%
  split(f = .$year, drop = TRUE)

year_list$`1987` = year_list$`1987` %>%   #Artificially increase the life expectancy to make the need for a break in Y axis
  mutate(lifeExp = lifeExp+100)

ggplot.component = list(scale_y_continuous(limits = c(30, 200)))
## creating a list of plots
plot_list <- purrr::pmap(
  .l = list(
    data = year_list,
    x = "continent",
    y = "lifeExp",
    
    xlab = "Continent",
    ylab = "Life expectancy",
    title = list(
      "Year: 1967",
      "Year: 1987",
      "Year: 2007"
    ),
    ggplot.component = list(ggplot.component, ggplot.component, ggplot.component)
  ),
  .f = ggbetweenstats
)

combine_plots(
  plotlist = plot_list,
  annotation.args = list(title = "Changes in life expectancy across continents (1967-2007)"),
  plotgrid.args = list(ncol = 3)
)

To have a break in the Y axis I found the package ggbreak (https://cran.r-project.org/web/packages/ggbreak/vignettes/ggbreak.html) that should easily do the job via the scale_y_break function. But it is incompatible with functions that arrange multiple plots (see FAQs at the bottom of the link).

Plus, If I try to run the following:

plot_list$`1987` + scale_y_break(c(50,120))

I get the following error:

Error in italic("F") : italic supports only flextable objects.

I am aware I am asking something about the ggbreak package but I was wondering if you could help me out trying to have a plot which combines multiple plots, has uniform Y axis and has a break in the Y axis to help visualise the data in the plot in a proper way (maybe there is a simpler way that does not require ggbreak?)

Thanks a lot
Luca

@IndrajeetPatil
Copy link
Owner

Thanks. I can reproduce. Need to dig a bit deeper as to why {ggbreak} can't find the plotmath functions.

library(ggstatsplot)
library(ggbreak)

set.seed(123)
ggbetweenstats(
  data = iris,
  x = Species,
  y = Sepal.Length,
  ggplot.component = list(scale_y_break(c(5, 7)))
)
#> Error in italic("F"): could not find function "italic"

Created on 2023-07-20 with reprex v2.0.2

Trace:

Error:
! could not find function "italic"
---
Backtrace:1. ├─base (local) `<fn>`(x)
 2. └─ggbreak:::print.ggbreak(x)
 3.   ├─base::suppressWarnings(grid.draw.ggbreak(x, ...))
 4.   │ └─base::withCallingHandlers(...)
 5.   └─ggbreak:::grid.draw.ggbreak(x, ...)
 6.     └─ggbreak:::set_label(g, totallabs = totallabs, p2 = x)
 7.       ├─base::do.call(labs, totallabs)
 8.       └─ggplot2 (local) `<fn>`(...)
 9.         └─rlang::dots_list(...)

@IndrajeetPatil
Copy link
Owner

@GuangchuangYu Do you see any obvious issues here before I dig into it deeper?

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

2 participants