when running those commands in R, R stuido may be aborted the seeesion which sometimes out of our control, so be patient when it happens
examples’ order based on the dimensionality of the projection
for better view purpose, you may want to open a new windows. For Windows platform is windowns(), for Unix is is X11(), for Mac is quartz().
the reason there are no plots after the codes is tourr plots are not dynamic in html or pdf, so I will run those codes in R script.
This is a useful paper about tourr, click here to check it out.
library(tourr)
library(TeachingDemos)
library(colorspace)
library(skimr)
library(geozoo) #high dim shapes, and codes to generate them
library(tidyverse)
library(randomForest)
library(ochRe)#install_github("ropenscilabs/ochRe")
library(rlang)
library(clusterfly)
library(reshape)
Animate a 1d tour path with a density plot or histogram.
This pins the center of distribution to the same place, and makes it easier to focus on the shape of the distribution.
animate_dist(flea[, 1:6])
animate_dist(flea[, 1:6], center = FALSE)
animate_dist(flea[, 1:6], method = "hist")
it’s a 2D tour path with a scatterplot
axes position of the axes: center, bottomleft or off
center if TRUE, centers projected data to (0,0). This pins the center of data cloud and make it easier to focus on the changing shape rather than position.
half_range half range to use when calculating limits of projected. If not set, defaults to maximum distance from origin to each row of data.
edges A two column integer matrix giving indices of ends of lines.
animate(flea[, 1:6], tour_path=grand_tour(), display=display_xy())
animate(flea[, 1:6], tour_path=grand_tour(),display=display_xy(axes = "bottomleft"))
animate(flea[, 1:6], tour_path=grand_tour(),display=display_xy(half_range = 0.5))
##specify edges
edges <- matrix(c(1:5, 2:6), ncol = 2)
animate(flea[, 1:6], grand_tour(),
display_xy(axes = "bottomleft", edges = edges))
pal <- rainbow_hcl(length(levels(flea$species)))
col <- pal[as.numeric(flea$species)]
animate_xy(flea[,-7], col=col)
grand tour picks a new p×d projection matrix at random
guided_tour pick one that is more interesting, Over time, this leads to picking projections that are closer to the current projection, so that we eventually converge to a single maximally interesting projection.
planned_tour is most constrained, already know where we want to go and simply cycle through a pre-specified set of frames
dependence_tour combines n independent 1-D tours, a numeric vector that specifies which 1-D tour each variable should be assigned to
local_tour alternates between a specified starting position and nearby random projections. This allows us to inspect the local neighborhood of a projection.
##little_tour is a special case of a planned tour,
it provides a smooth sequence between views of all d-dimensional sets of variables in the data
animate_xy(flea[, 1:6], tour_path=little_tour())
##Sphering is often useful in conjunction with the guided tour, as it removes simpler patterns that may conceal more interesting findings.
animate_xy(flea[, 1:3], tour_path=guided_tour(holes), sphere = TRUE)
col <- pal[as.numeric(flea$species)]
animate_xy(flea[, 1:6], guided_tour(lda_pp(flea$species)), sphere = TRUE, col=col,
axes = "bottomleft")
##specifies that the first, third, and fifth variables will be displayed with a 1-D tour on the first axis, and the second, fourth, and sixth with a 1-D tour on the second axis.
animate_xy(flea[, 1:6], dependence_tour(c(1, 2, 1, 2, 1, 2)), axes = "bottomleft")
show a 3-D object on a 2-D display
This display uses depth cues of occlusion (closer points occlude further away points), size (closer points are bigger) and saturation (distant points are hazier and less saturated).
animate_depth(flea[, 1:6])
1.Parallel coordinates tour path animation. Animate a nD tour path with a parallel coordinates plot.
##3D
animate_pcp(flea[, 1:6], grand_tour(3))
##5D
animate_pcp(flea[, 1:6], grand_tour(5))
A smoothened version of pcp
the Andrews plot defines a finite Fourier series
animate_andrews(flea[, 1:6], grand_tour(d = 3))
animate_andrews(flea[, 1:6], grand_tour(d = 6))
animate_andrews(flea[, 1:6], guided_tour(cmass))
Animate a nD tour path with a scatterplot matrix.
The lines show the observations, and the points, the values of the projection matrix
animate_scatmat(flea[, 1:6], grand_tour(2))
animate_scatmat(flea[, 1:6], guided_tour(cmass))
Chernoff faces tour path animation.
Animate a nD tour path with Chernoff’s faces can display up to 18 dimensions.
animate_faces(flea[1:6, 1:6], grand_tour(6))
animate_faces(flea[1:6, 1:6], guided_tour(cmass))
animate_stars(flea[1:10, 1:6], grand_tour(5))
animate_stars(flea[1:10, 1:6], grand_tour(5),
col.stars = rep("grey80", 10), radius = FALSE)
1.The following two examples are taking from Dr Di Cook’s talk in Singapore on Feb 2018. Click here to go the orginal talk’s pdf.
data(olive)
olive_sub <- olive %>% filter(region==1) %>%
select(-region) %>% mutate(area=factor(area))
olive_rf <- randomForest(area~., data=olive_sub)
votes <- f_composition(olive_rf$votes)
colnames(votes) <- paste0("V", 1:3)
votes <- cbind(votes, area=olive_sub$area)
sp3 <- simplex(p = 3)
colnames(sp3$points) <- paste0("V", 1:3)
sp3$points <- cbind(sp3$points, area=c(5,5,5,5))
sp3$edges <- as.matrix(sp3$edges)
votes <- rbind(sp3$points, votes)
pal <- ochre_palettes$lorikeet[c(2,3,4,6)]
col <- pal[votes[,4]]
animate(votes[,-4], grand_tour(),
display_xy(axes = "bottomleft", col=col, edges=sp3$edges))
data(flea)
plot(hclust(dist(rescaler(flea[,-7])), method="single"), hang=-1)
plot(hclust(dist(rescaler(flea[,-7])), method="ward.D2"), hang=-1)
flea_hc_s <- hierfly(flea, method="single")
flea_hc_w<- hierfly(flea)
pal <- ochre_palettes$lorikeet[c(2,3,4,5)]
col <- c(pal[as.numeric(flea$species)],
rep(pal[4], nrow(flea_hc$data)-nrow(flea)))
pch <- c(rep(16,nrow(flea)),
rep(4, nrow(flea_hc$data)-nrow(flea)))
t1 <- save_history(flea_hc$data[, 1:6], max = 40)
animate(flea_hc_s$data[, 1:6], planned_tour(t1),
display_xy(axes = "bottomleft",
col=col, pch=pch, edges=flea_hc_s$edges))
animate(flea_hc$data[, 1:6], planned_tour(t1),
display_xy(axes = "bottomleft",
col=col, pch=pch, edges=flea_hc$edges))
if you can install rggobi successfully, then you can follow this. Otherwise, you still can learn something by watching me doing it.
there is a book about ggobi in R, if you are interested, I cam email you.
library(rggobi)
g <- ggobi(iris)
clustering <- hclust(dist(iris[,1:4]),
method="average")
glyph_colour(g[1]) <- cuttree(clustering, 3)