Manipulate

This is an R Markdown document created with examples of how to work with manipulate. Based on Coursera’s Data Products course.


About manipulate

  • Manipulate is a really cool solution that is often all you need to quickly make interactive graphics
  • You can create a slider, checkbox, or picker (drop down) and have more than one

Basic example:

If you input this:

library(manipulate)
manipulate(plot(1:x), x=slider(1,100))

You will obtain the following figure on Rstudio, where you can move around the slider (the more you go to the right, the more points will be plotted):

simplestApp

library(manipulate)
library(UsingR)
myHist <-  function (mu) {
hist (galton$child,col="blue" ,breaks= 100)
lines(c(mu, mu),  c(0, 150), col= "red", lwd= 5)
mse <-  mean((galton$child - mu)^2)
text(63,  150, paste ("mu = ", mu))
text(63,  140, paste ("MSE = ", round (mse, 2)))
}
manipulate(myHist(mu), mu=slider (62, 74, step = 0.5))

Again you will get a slider:

simplestApp

Other examples:

Cool stuff to try:

  • a picker
manipulate(
  barplot(as.matrix(longley[,factor]), 
          beside = TRUE, main = factor),
  factor = picker("GNP", "Unemployed", "Employed"))

simplestApp

  • checkboxes!
manipulate(
  boxplot(Freq ~ Class, data = Titanic, outline = outline),
  outline = checkbox(FALSE, "Show outliers"))

simplestApp

  • multiple controls
manipulate(
  plot(cars, xlim = c(0, x.max), type = type, ann = label),
  x.max = slider(10, 25, step=5, initial = 25),
  type = picker("Points" = "p", "Line" = "l", "Step" = "s"),
  label = checkbox(TRUE, "Draw Labels"))

simplestApp