Interactive Graphics with the manipulate Package

Open the example files

Open the file rstudioTraining/manipulate/manipulate.R

Run an example

To test out a manipulate plot, run the following code from manipulate.R:

library(manipulate)

# slider
manipulate(
  plot(rnorm(x), rnorm(x)),
  x=slider(0, 1000, initial=1, step=100)
)

Try it yourself

Use the previous example as an outline and build a new manipulate plot with data from homePrice.csv. An example plot would be the following

# using homePrice.csv
homes <- read.csv("~/Desktop/rstudioTraining/data/homePrice.csv")
plot(homes$price[1:100])

plot of chunk unnamed-chunk-2

How can we add a manipulate slider to this? Perhaps vary the amount of data that is shown by replacing 100 with a manipulate variable.

Experiment with each of the options for manipulate:

# picker
manipulate(
  barplot(as.matrix(longley[,factor]), 
          beside = TRUE, main = factor),
  factor = picker("GNP", "Unemployed", "Employed")
)
# checkbox
manipulate(
  boxplot(Freq ~ Class, data = Titanic, outline = outline),
  outline = checkbox(FALSE, "Show outliers")
)

Try using these with homePrice.csv as well. Here are some hints:

Change which variables are plotted by turning the variable into a picker:

hist(homes[, "price"])

plot of chunk unnamed-chunk-6

plot(homes[, "rating"], homes[, "price"])

plot of chunk unnamed-chunk-6

(Optional) Combine mulitple manipulate calls together. Also try changing some of the more advanced examples to include manipulate.

avePrice <- rep(0, 5)
for (i in 1:5) {
    avePrice[i] <- mean(subset(homes, rating == i)$price)
}

plot(homes$rating, homes$price, main = "Breakdown of Price by Rating", 
    xlab = "Rating", ylab = "Price", las = 2, labels = FALSE)
axis(1)
axis(2, at = c(0, 2e+05, 4e+05, 6e+05, 8e+05), labels = c("0", "200k", 
    "400k", "600k", "800k"), las = 2)

points(1:5, avePrice, col = "red", pch = 19)
text(1:4, avePrice[1:4], round(avePrice[1:4], -3), pos = 4)
text(5, avePrice[5], round(avePrice[5], -3), pos = 2)

(Optional) Or even with ggplot2 examples

library(ggplot2)
p <- qplot(rating, price, color = age, data = homes, main = "Breakdown of Price by Rating", 
    xlab = "Rating")
p <- p + opts(plot.title = theme_text(size = 23))
p <- p + opts(axis.title.x = theme_text(size = 16, vjust = -0.05))
p <- p + opts(axis.title.y = theme_text(size = 16))
p <- p + opts(legend.title = theme_text(size = 14))
p <- p + scale_y_continuous(name = "Price", labels = c("200k", "400k", 
    "600k", "800k"))
p <- p + geom_point()

print(p)

plot of chunk unnamed-chunk-10

Explore the mosaicManip package

Open the file rstudioTraining/manipulate/mosaicManip.R

Install the mosaicManip package and run some examples:

library("mosaicManip")

# model fitting and term selection
data(KidsFeet)
mFit(width ~ length, data = KidsFeet)

# hypothesis testing
mHypTest()

Look up the help for one of the examples provided in the file. Use this to help you build a new manipulate plot with the homePrice.csv data. One idea is the following:

`?`(mFit)
mFit(price ~ age, data = homes)

What else can you come up with for this function or others.