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

plotting binned plots with non- binned plots #3

Open
cduncaceae opened this issue Mar 31, 2023 · 3 comments
Open

plotting binned plots with non- binned plots #3

cduncaceae opened this issue Mar 31, 2023 · 3 comments

Comments

@cduncaceae
Copy link

Is there a function within the linbin package that allows for the generation of non-binned plots aligned with binned plots within the plotted output?
I thought I could work around by making an object out of the linbin output and then plotting in ggplot, but converting the linbin plot out ouput into a GROB seems to have distorted the bin size in the image.
Fig-ggplot_linin

@ezwelty
Copy link
Owner

ezwelty commented Apr 1, 2023

That's a good question, and frankly, I have no idea. It isn't something I ever set out to support. I wonder if it would be possible to achieve what you want by using the (currently internal) plot_events_single function:

https://github.com/ezwelty/linbin/blob/3adf8ecdf583ed7c91b086cf6ac9ab2dbcca71aa/R/plot_events.R#LL132-L132C19

But probably the best solution is to make the plots directly in ggplot2 with geom_rect. Here is an example:

require(linbin)
require(ggplot2)

# Prepare data
e <- simple
bins <- seq_events(event_range(e), length.out = c(16, 4, 2))
binned_wide <- sample_events(e, bins, list(sum, c('x', 'y'), na.rm = TRUE))
binned_long <- tidyr::pivot_longer(
  binned_wide, cols = c('x', 'y'), names_to = 'variable'
)

# Plot with ggplot2
ggplot(binned_long, aes(xmin = from, xmax = to, ymin = 0, ymax = value)) +
  geom_rect() +
  facet_grid(variable ~ group)

image

@cduncaceae
Copy link
Author

cduncaceae commented Apr 4, 2023 via email

ezwelty added a commit that referenced this issue Apr 6, 2023
@ezwelty
Copy link
Owner

ezwelty commented Apr 6, 2023

That is what I meant when I wrote that plot_events_single is "currently internal". It is used by plot_events internally, but cannot be used externally without a change to the package.

I've just now updated the code on GitHub to make it possible for users to call plot_events_single. You'll need to install the development version of linbin:

install.packages("devtools") # if needed
devtools::install_github("ezwelty/linbin")

I would also like to adjust the ylim for the individual plots per bin size

As you probably saw, plot_events only supports either free ylim or the same fixed ylim for all plots. You could call plot_events once for each bin size (or plot_events_single for each plot), setting the ylim accordingly, and combine the plots. Or you can use ggplot2 with facet_grid's scales argument (https://ggplot2.tidyverse.org/reference/facet_grid.html). Modifying my example from above:

require(linbin)
require(ggplot2)

# Prepare data
e <- simple
bins <- seq_events(event_range(e), length.out = c(16, 4, 2))
binned_wide <- sample_events(e, bins, list(sum, c('x', 'y'), na.rm = TRUE))
binned_long <- tidyr::pivot_longer(
  binned_wide, cols = c('x', 'y'), names_to = 'variable'
)

# Plot with ggplot2
ggplot(binned_long, aes(xmin = from, xmax = to, ymin = 0, ymax = value)) +
  geom_rect() +
  facet_grid(variable ~ group, scales='free_y')

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