if(interactive()) {
######## Basic ########
lp <- l_plot(iris,
color = iris$Species,
glyph = "circle")
gp <- loon2ggplot(lp)
gp # a ggplot object
# Add a smooth layer
# Reset initial plot object
gp$data <- gp$layers[[1]]$data
gp$mapping <- gp$layers[[1]]$mapping
gp +
geom_smooth() +
# 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
if (FALSE) {
# The bins are constructed by `ggplot2::geom_rect()`
# Very limited manipulations can be made
# ERROR will be returned
gh1 + facet_wrap(~fill)
}
######## Argument `selectedOnTop` ########
p <- l_plot(iris, color = iris$Species)
p['selected'][iris$Petal.Length > 5] <- TRUE
g <- loon.ggplot(p)
g
# facet by "Species"
g + facet_wrap(iris$Species)
# 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)
# \donttest{
######## 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 Patchwork!")
# }
}
Run the code above in your browser using DataLab