knitr::opts_chunk$set( echo = TRUE, tidy.opts = list(width.cutoff = 65), tidy = TRUE) set.seed(12314159) library(loon) data(minority) imageDirectory <- "/Users/rwoldford/Documents/Research/talks/MontrealRtutorial2018/Slides/img" dataDirectory <- "/Users/rwoldford/Documents/Research/talks/MontrealRtutorial2018/Slides/data" path_concat <- function(path1, path2, sep="/") {paste(path1, path2, sep = sep)} library(loon) data(minority) names(minority)[1:14] subset <- t(minority[c("Victoria", "Ottawa", "St. John\'s"),][,1:(length(names(minority))-4)]) knitr::kable(subset) minorityPercent <- data.frame(100*minority[, -c(11, 15:18)] / minority[, "Total.population"]) # Take the opportunity to shorten the Statistics Canada # names of the minorities as well: names(minorityPercent) <- c("% Arabic", "% Black", "% Chinese", "% Filipino", "% Japanese", "% Korean", "% Latino", "% Multiple", "% S. Asian", "% SE Asian", "% Other", "% Minority", "% W Asian") # The proportion of the city population that is a "visible minority" l_hist(minorityPercent$"% Minority", title = "% Minority in 33 Canadian cities", xlabel = "percent of population", linkingGroup = "minority", yshows = "frequency", showBinHandle = FALSE, showGuides = TRUE, showScales = TRUE ) # Order minorities by decreasing maximum percent groups <- names(minorityPercent)[-c(12, 14, 15)] bySize <- names(sort(apply(minorityPercent[,groups ], 2, max), decreasing = TRUE) ) # And now look at the three largest for (x in bySize[1:3]) { l_hist(minorityPercent[,x], title = x, xlabel = "percent of population", linkingGroup = "minority", showScales = TRUE, showGuides = TRUE, showStackedColors = TRUE, color = "grey" ) } # get a map: library(maps) canada_map <- map("world", "Canada", plot=FALSE, fill=TRUE) # plot the cities cities_map <- with(minority, l_plot(long, lat, xlabel = "longitude", ylabel="latitude", showLabels = TRUE, linkingGroup = "minority", itemLabel = rownames(minority), showItemLabels = TRUE) ) # Layer in the map of Canada landcol <- "cornsilk" canada_layer <- l_layer(widget = cities_map, x = canada_map, label = "Canada", color = landcol, index = "end") # Rescale the plot to the size of the map l_scaleto_layer(cities_map, canada_layer) # Could also add the text of the city names to the map as glyphs textGlyphs <- l_glyph_add_text(cities_map, text = row.names(minority), label = "city names") minPercentPairs <- l_pairs(data = minorityPercent, size = 2, glyph = "ocircle", linkingGroup = "minority", itemLabel = rownames(minority), showItemLabels = TRUE) # a compound loon object consisting of many plots names(minPercentPairs) [1:5] minPercentPairs$x2y1 minPercentPairs$x2y1["glyph"] <- "otriangle" # Back to the raw counts minority_only <- minority[, c( "Southeast.Asian", "Chinese", "Japanese", "South.Asian", "Visible.minority.not.included.elsewhere", "Black", "Multiple.visible.minority", "Filipino", "Arab", "Korean", "Latin.American")] # Again, shorten the variable names names(minority_only) <- c( "SE.Asian", "Chinese", "Japanese", "S.Asian", "Other", "Black", "Multiple", "Filipino", "Arab", "Korean", "Latino" ) s <- l_serialaxes(data = minority_only, axesLayout = "parallel", # Note choice of parallel linkingGroup = "minority", sequence = names(minority_only), showGuides = FALSE, linewidth = 2, scaling = "data", showArea = FALSE, itemLabel = rownames(minority), showItemLabels = TRUE) s_glyphs <- l_glyph_add_serialaxes(cities_map, data = minority_only, sequence = names(minority_only), scaling = "variable", showArea = TRUE, label = "serial_axes") cities_map['glyph'] <- s_glyphs barplot_imgs <- l_make_glyphs(data = lapply(1:nrow(minority_only), FUN = function(i) minority_only[i,]), # draw a coloured bar plot draw_fun = function(m) { par(mar=c(1,1,1,1)*.5) mat <- as.numeric(m/max(m)) barplot(height = mat, beside = FALSE, ylim = c(0,1), col = rainbow(length(mat)), axes= FALSE, axisnames=FALSE)}, width=120, height=120) # Can view the images using loon's viewer img_vwr <- l_imageviewer(barplot_imgs) # add the glyphs barplot_glyphs <- l_glyph_add_image(cities_map, barplot_imgs, "barplot") cities_map['glyph'] <- barplot_glyphs library(RnavGraphImageData) data(frey) # Here are the images frey.imgs <- l_image_import_array(frey, 28,20, img_in_row = FALSE, rotate = 90) frey_imgs_vwr <- l_imageviewer(frey.imgs) data <- t(frey) # images were in columns # get the principal components frey_pc <- prcomp(data) plot(frey_pc$sdev, type = "b", pch = 19, col = "blue", main = "Principal component scree", ylab = "singular values") abline(v=6, col = "firebrick", lty = 2) frey_pc_coords <- frey_pc$x[, 1:6] # create a navigation graph frey_pc_nav <- l_navgraph(frey_pc_coords, linkingGroup="frey") frey_glyphs <- l_glyph_add_image(frey_pc_nav$plot, images=frey.imgs, label="frey faces") # l_navigator_walk_path(frey_pc_nav$navigator, path = c("PC1:PC2", "PC2:PC3")) library(grid) grid.newpage() grid.loon(frey_pc_nav) ## library(dimRed) # dimension reduction package ## # Number of neighbours ## k_nbhrs <- 12 ## # Target dimension ## n_dims <- 20 ## # Dimension reduction via LLE ... this may take a while ## lle20 <- embed(data, "LLE", knn = k_nbhrs, ndim = n_dims) ## lle20data <- lle20@data@data ## l_serialaxes(lle20data, linkingGroup = "frey") ## # Get a set of "closures" which will calculate scagnostics. ## lle_scags2d <- scagnostics2d(lle20data) ## # Now a scatterplot matrix of THE PLOTS ## lle_nav <- l_ng_plots(measures=lle_scags2d, linkingGroup="frey", size = 1) ## ## lle_gl <- l_glyph_add_image(lle_nav$plot, ## images=frey.imgs, ## label="frey faces") ## ## lle_nav$plot["linkingGroup"] <- "frey" ## lle_nav$plots$x2y1["size"] <- 1