user2165907 user2165907 - 2 months ago 8
R Question

ggplot2/manipulate checkbox on facets

Would you know a way to interactively select facets in ggplot2?
I tried manipulate and checkbox, without success...

library(ggplot2)
library(manipulate)

manipulate(
ggplot(subset(iris, Species %in% c(cb1, cb2, cb3)), aes(x = Petal.Width, y = Petal.Length)) +
facet_grid(. ~ Species) +
geom_point(),
cb1 = checkbox(TRUE, "setosa"),
cb2 = checkbox(TRUE, "versicolor"),
cb3 = checkbox(TRUE, "virginica")
)

Answer

You could try,

vector=c("setosa","versicolor","virginica")

manipulate(ggplot(subset(iris, Species %in% vector[c(cb1, cb2, cb3)]), aes(x = Petal.Width, y = Petal.Length)) +
    facet_grid(. ~ Species) +
    geom_point(),
    cb1 = checkbox(TRUE, "setosa"),
    cb2 = checkbox(TRUE, "versicolor"),
    cb3 = checkbox(TRUE, "virginica")
)