Created
August 1, 2015 22:12
-
-
Save rpietro/911557b2579743aaee29 to your computer and use it in GitHub Desktop.
Script for tableplot vignette
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://cran.r-project.org/web/packages/tabplot/vignettes/tabplot-vignette.html | |
require(ggplot2) | |
data(diamonds) | |
is.na(diamonds$price) <- diamonds$cut == "Ideal" | |
is.na(diamonds$cut) <- (runif(nrow(diamonds)) > 0.8) | |
tableplot(diamonds) | |
tableplot(diamonds, select = c(carat, price, cut, color, clarity), sortCol = price) | |
tableplot(diamonds, select = c(carat, price, cut, color, clarity), sortCol = price, | |
from = 0, to = 5) | |
tableplot(diamonds, subset = price < 5000 & cut == "Premium") | |
tablePalettes() | |
tableplot(diamonds, pals = list(cut = "Set1(6)", color = "Set5", clarity = rainbow(8))) | |
diamonds$carat_class <- num2fac(diamonds$carat, n = 20) | |
diamonds$price_class <- num2fac(diamonds$price, n = 100) | |
tableplot(diamonds, select = c(carat, price, carat_class, price_class)) | |
# create large dataset | |
large_diamonds <- diamonds[rep(seq.int(nrow(diamonds)), 10), ] | |
system.time({ | |
p <- tablePrepare(large_diamonds) | |
}) | |
system.time({ | |
tableplot(p, plot = FALSE) | |
}) | |
system.time({ | |
tableplot(p, sortCol = price, nBins = 200, plot = FALSE) | |
}) | |
Although the first step takes a couple of seconds on a moderate desktop computer, the processing time to create a tableplot from the intermediate result, object p, is very short in comparison to the direct approach: | |
system.time({ | |
tableplot(large_diamonds, plot = FALSE) | |
}) | |
system.time({ | |
tableplot(large_diamonds, sortCol = price, nBins = 200, plot = FALSE) | |
}) | |
system.time({ | |
tableplot(p, sample = TRUE) | |
}) | |
# calculate normalized carats to be used as sample probabilities | |
carat.norm <- with(diamonds, carat/max(diamonds$carat)) | |
# draw samples | |
exp.diamonds <- diamonds[sample(1:nrow(diamonds), size = 10000, prob = carat.norm, | |
replace = TRUE), ] | |
chp.diamonds <- diamonds[sample(1:nrow(diamonds), size = 10000, prob = 1 - carat.norm, | |
replace = TRUE), ] | |
tp1 <- tableplot(exp.diamonds, plot = FALSE) | |
tp2 <- tableplot(chp.diamonds, plot = FALSE) | |
plot(tp2 - tp1) | |
tab <- tableplot(diamonds, plot = FALSE) | |
summary(tab) | |
plot(tab) | |
tableplot(diamonds, select = 1:7, fontsize = 14, legend.lines = 8, title = "Shine on you crazy Diamond", | |
fontsize.title = 18) | |
tab2 <- tableChange(tab, select_string = c("carat", "price", "cut", "color", | |
"clarity"), pals = list(cut = "Set1(2)")) | |
plot(tab2) | |
tableSave(tab, filename = "diamonds.png", width = 5, height = 3, fontsize = 6, | |
legend.lines = 6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment