loon2ggplot: Turn a 'loon' widget to a 'ggplot' object

View source: R/loon2ggplot.R

loon2ggplot.l_compoundR Documentation

Turn a loon widget to a ggplot object

Description

Create a ggplot object from a loon widget

Usage

## S3 method for class 'l_compound'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_facet_ggplot'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_facet_grid'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_facet_wrap'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_layer_graph'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_layer_histogram'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_layer_scatterplot'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_pairs'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_patchwork'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_serialaxes'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'zenLoon'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## Default S3 method:
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_plot'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_hist'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

## S3 method for class 'l_plot3D'
loon2ggplot(
  target,
  asAes = TRUE,
  selectedOnTop = TRUE,
  showNearestColor = FALSE,
  ...
)

Arguments

target

a loon or a vector that specifies the widget, layer, glyph, navigator or context completely. The widget is specified by the widget path name (e.g. '.l0.plot'), the remaining objects by their ids.

asAes

logical; set aesthetics attributes, i.e. 'color', 'fill' as variables (default TRUE) or general visual properties (FALSE). See details

selectedOnTop

logical and default is TRUE; whether to display the selected points on top. See details.

showNearestColor

logical and default is FALSE; if TRUE, the legend of color and fill (hex code) would be converted to the R built-in color names. For some hex codes, there are no precise matching. Consequently, these colors will be converted to the R built-in color names which are the "nearest" of these hex codes.

...

arguments used inside loon2ggplot(), not used by this method

Details

In ggplot2, typically, there are two ways to set the aesthetic attributes, either take them as variables asAes = TRUE (set in the function aes()) or constants asAes = FALSE. The main benefits to consider them as variables are that 1. legend could be displayed; 2. convenient for further analysis.

In loon, when points were selected (highlighted), the order would be changed so that the highlighted points would be displayed at the front. To turn the loon plot static, if selectedOnTop = TRUE, the points would be partitioned into two groups – one group representing the un-highlighted points, and the other group representing the highlighted points. The un-highlighted group would be drawn first, then the selected group; if selectedOnTop = FALSE, no partition would be applied so that the displayed order remained. However, the highlighted points could be displayed at the back. See examples.

Value

a ggplot object (or a patchwork object, a extension of ggplot2)

Examples

if(interactive()) {
######## Basic ########
lp <- l_plot(iris,
             color = iris$Species,
             glyph = "circle")
gp <- loon2ggplot(lp)
gp # a ggplot object

# add smooth layer, grouped by color
gp +
  geom_smooth(aes(color = color)) +
  # give meaningful legend label names
  scale_color_manual(
    # make sure the order is correct
    values = unique(hex12tohex6(lp['color'])),
    labels = c("setosa", "versicolor", "virginica")
  )

# histogram
lh <- l_hist(mtcars$mpg,
             color = factor(mtcars$gear))

gh0 <- loon2ggplot(lh)
# facet by `fill`
gh0 + facet_wrap(~fill)

######## Argument `asAes` ########
gh1 <- loon2ggplot(lh, asAes = FALSE)
gh1
## Not run: 
# The bins are constructed by `ggplot2::geom_rect()`
# Very limited manipulations can be made
# ERROR
gh1 + facet_wrap(~fill)

## End(Not run)

######## Argument `selectedOnTop` ########
p <- l_plot(iris, color = iris$Species)
p['selected'][iris$Petal.Length > 5] <- TRUE
g <- loon.ggplot(p)
# It looks correct.
g
# facet by "Species"
## Not run: 
g + facet_wrap(iris$Species)

## End(Not run)
# Something is wrong here. There is a pink point (at least one)
# in species "versicolor"! It is because after points are
# highlighted, the displayed order has been changed.
# Set `selectedOnTop` as FALSE, as in
loon.ggplot(p, selectedOnTop = FALSE) +
  facet_wrap(iris$Species)


######## l_patchwork --> ggplot ########
library(patchwork)
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) +
   geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
design <- c(
  area(1,1),
  area(1,2),
  area(2,1,2,2)
)
pp <- p1 + p2 + p3 + plot_layout(design = design)

# turn a patchwork obj to a loon (l_compound)
lp <- ggplot2loon(pp)
# turn a loon (l_compound) back to a patchwork
plp <- loon2ggplot(lp)
plp # almost identical to pp


######## zneplots --> ggplot ########
library(zenplots)
stopifnot(packageVersion("zenplots") > "1.0.4")
zen <- zenplots::zenplot(iris, plot1d = "density", pkg = "loon")
ggzen <- loon.ggplot(zen)
ggzen +
  patchwork::plot_annotation(title = "This is a ggplot")

}

great-northern-diver/loon.ggplot documentation built on March 20, 2023, 8:03 p.m.