Open the file rstudioTraining/manipulate/manipulate.R
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)
)
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])
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.
# 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")
)
Change which variables are plotted by turning the variable into a picker:
hist(homes[, "price"])
plot(homes[, "rating"], homes[, "price"])
(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)
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.