############################################## # # Statistics Canada data on 33 Canadian cities # 1. minorityPercent ... visible minority %ages # setwd("~/Documents/Research/talks/SSC\ 2016/talk/Rcode") library(loon) source("minorityData.R") # START # Look at marginal distributions # # First, just for the proportion of the city population that is a "visible minority" l_hist(minorityPercent[,"% Minority"], title = paste("% Minority in 33 Canadian cities"), xlabel="percent of population", linkingGroup="minority", yshows="density", showScales=TRUE ) # END # START # Now show the same for the three minority groups # having largest percentages (in decreasing order): groups <- names(minorityPercent)[-c(12, 14, 15)] largest3 <- names(sort(apply(minorityPercent[,groups ], 2, max), decreasing = TRUE) )[1:3] # for (x in largest3) { l_hist(minorityPercent[,x], title = x, xlabel="percent of population", linkingGroup="minority", showScales=TRUE ) } # END # START # Now look at the population sizes # pop_p <- l_plot(x=minorityPercent[,"Log_10 Total pop"], y=minorityPercent[,"Log_10 Minority pop"], title="Population sizes", linkingGroup="minority", xlabel="Log_10 total pop", ylabel="Log_10 minority pop", itemlabel=rownames(minorityPercent), showItemlabels=TRUE ) # END # START # The map # First plot the points citiesLat <- minority[,"lat"] citiesLong <- minority[,"long"] p_map <- l_plot(citiesLong,citiesLat, xlabel="longitude", ylabel="latitude", linkingGroup="minority", itemlabel=rownames(minorityPercent), showItemlabels=TRUE) # # Get the map, add it to the plot (as a layer) # library(maps) id <- l_layer(p_map, map("world", "Canada", plot=FALSE, fill=TRUE), index="end") # Rescale the plot to the size of the map l_scaleto_layer(p_map, id) # # Add the text of the city names to the map l_glyph_add_text(p_map, text=row.names(minorityPercent), label="city names") # END # START ## # Serial axes # # Back to the raw data and just rename variables library(dplyr) minority_star <- minority %>% select("SE.Asian"=Southeast.Asian, Chinese = Chinese, Japanese = Japanese, "S.Asian" = South.Asian, Other = Visible.minority.not.included.elsewhere, Black = Black, "Multiple" = Multiple.visible.minority, Filipino = Filipino, Arab = Arab, Korean = Korean, "Latino" = Latin.American) # Get axes sequence library(PairViz) set.seed(1564) # otherwise axes sequence differs CorrelationMatrix <- cor(minority_star) VarDistance <- dist(t(minority_star)) # use minus the distance to produce the greatest # distinction between axes o <- order_tsp(-VarDistance) # west to east color mapping library(scales) west_east_cols <- (col_numeric(c("firebrick", "orange", "sienna", "darkgreen", "steelblue", "purple"), domain = NULL))(minority$long) s <- l_serialaxes(data=minority_star, linkingGroup = "minority", sequence=names(minority_star)[o], showGuides=FALSE, linewidth=5, color=west_east_cols, showArea=FALSE, itemlabel=rownames(minority), showItemlabels=TRUE) # END # START ## Produce grid of star glyphs p_stars <- with(minority, l_plot(rank(long), rank(long), title = "", color = west_east_cols, showScales = FALSE, showLabels=FALSE, size=3, linkingGroup = "minority", itemlabel=rownames(minority), showItemlabels=TRUE)) gstars <-l_glyph_add_serialaxes(p_stars , data=minority_star, sequence = names(minority_star)[o], scaling = 'observation', showArea=TRUE) p_stars['glyph'] <- gstars l_move_grid(p_stars, which='all') ## swapAxes neccessary to arrange west-east in the grid (columns) p_stars['swapAxes'] <- TRUE l_zoom(p_stars, .9) # # END # # START # # Add some glyphs to the map, # scale values within observations so <- l_glyph_add_serialaxes(p_map, data=minority_star, sequence = names(minority_star)[o], scaling = 'observation', showArea=TRUE, label="stars") p_map['glyph'] <- so # ## Create a Worldview p_map_ww <- l_worldview(activewidget=p_map) # # END #### BACK TO SLIDES