ezwelty / linbin

Binning and Plotting of Linearly Referenced Data
https://cran.r-project.org/package=linbin
2 stars 0 forks source link

plotting binned plots with non- binned plots #3

Open cduncaceae opened 1 year ago

cduncaceae commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Thank you so much for your response, Dr. Welty.

When I attempted to plot using the plot_events_simple I got this error message : Error: 'plot_events_single' is not an exported object from 'namespace:linbin'.

Is there something else required other than library (libin)?

I would also like to adjust the ylim for the individual plots per bin size. Is it possible to do this within the plot_events_simple function? Or would this be best to do within ggplot.

On Sat, Apr 1, 2023 at 2:07 AM Ethan Welty @.***> wrote:

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)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

ezwelty commented 1 year ago

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