library(gginteractive)
## Loading required package: ggplot2
## Loading required package: htmltools
p1 <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
A normal ggplot. Next we pipe to a blank mesh, so that the plot is rendered as svg. In Rstudio this will appear in the viewer, rather than the plot window.
p1 %>% mesh_blank()
Now for an actually useful example, exploring the effect of the smoothing parameter span:
spans <- seq(.3, 1.5, by = .1)
p4 <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
lapply(spans, function(x) geom_smooth(method = "loess", span = x, se = FALSE))
controls <- 0:length(spans)
names(controls) <- c("none", spans)
p4 %>% mesh_geom("smooth", attr = "opacity",
control = radio(controls))
The gapminder example:
library(gapminder)
p2 <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
geom_point() + scale_x_log10()
p2 <- p2 %>% mesh_alpha(geom = "point", variable = "year", on = radio0)
p2
Convert back to a regular ggplot object:
p2 %>% unmesh()
Another example:
(ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
geom_point() + scale_x_log10()) %>%
mesh_alpha(geom = "point", variable = "continent", on = radio0)