|
| 1 | + |
| 2 | +# This uses l_make_glyphs to build standard periodic table labels |
| 3 | +# for glyphs of the elements data from loon.data package. |
| 4 | +# |
| 5 | + |
| 6 | +if(requireNamespace("loon.data", quietly = TRUE)) { |
| 7 | + local({ |
| 8 | + data("elements", package = "loon.data") |
| 9 | + |
| 10 | + # A draw function for each element |
| 11 | + draw_element_box <- function(symbol, |
| 12 | + name, number, |
| 13 | + mass_number, |
| 14 | + mass, col) { |
| 15 | + if (missing(col)) col <- "white" |
| 16 | + oldPar <- par(bg = col, mar = rep(1, 4)) |
| 17 | + # |
| 18 | + # Following line included *only* for demo |
| 19 | + devAskNewPage(ask = FALSE) |
| 20 | + |
| 21 | + plot(NA, xlim = c(0,1), ylim = c(0, 1), axes=FALSE, ann = FALSE) |
| 22 | + text(0.5, 0.6, labels = symbol, cex = 18) |
| 23 | + text(0.15, 1, labels = number, cex = 6, adj= c(0.5,1)) |
| 24 | + text(0.5, 0.25, labels = name, cex = 6) |
| 25 | + text(0.5, 0.11, labels = mass_number, cex = 3) |
| 26 | + text(0.5, 0.01, labels = mass, cex = 3) |
| 27 | + box() |
| 28 | + |
| 29 | + par(oldPar) |
| 30 | + } |
| 31 | + |
| 32 | + # Get the categories |
| 33 | + colIDs <- paste(elements$Category, elements$Subcategory) |
| 34 | + # Get a loon palette function |
| 35 | + colFn <- color_loon() |
| 36 | + # Get colors identified with categories |
| 37 | + tableCols <- colFn(colIDs) |
| 38 | + # |
| 39 | + # A function to an element box image for each element. |
| 40 | + |
| 41 | + make_element_boxes <- function(elements, |
| 42 | + cols, |
| 43 | + width = 500, |
| 44 | + height = 500) { |
| 45 | + if (missing(cols)) cols <- rep("white", nrow(elements)) |
| 46 | + listOfElements <- lapply(1:nrow(elements), |
| 47 | + FUN = function(i) { |
| 48 | + list(vals = elements[i,], |
| 49 | + col = cols[i]) |
| 50 | + }) |
| 51 | + # glyphs created here |
| 52 | + l_make_glyphs(listOfElements, |
| 53 | + draw_fun = function(element){ |
| 54 | + x <- element$vals |
| 55 | + col <- element$col |
| 56 | + draw_element_box(symbol = x$Symbol, |
| 57 | + name = x$Name, |
| 58 | + number = x$Number, |
| 59 | + mass_number = x$Mass_number, |
| 60 | + mass = x$Mass, |
| 61 | + col = col) |
| 62 | + }, |
| 63 | + width = width, |
| 64 | + height = height) |
| 65 | + } |
| 66 | + |
| 67 | + # Construct the glyphs |
| 68 | + boxGlyphs <- make_element_boxes(elements, cols = tableCols) |
| 69 | + |
| 70 | + readline("Hit <Return> to scroll through the elements") |
| 71 | + # Look at the images |
| 72 | + l_imageviewer(boxGlyphs) |
| 73 | + |
| 74 | + readline("Hit <Return> to lay them out in the periodic table.") |
| 75 | + |
| 76 | + # Get a couple of plots |
| 77 | + periodicTable <- l_plot(x = elements$x, y = elements$y, |
| 78 | + xlabel = "", ylabel = "", |
| 79 | + title = "Periodic Table of the Elements", |
| 80 | + linkingGroup = "elements", |
| 81 | + color = tableCols) |
| 82 | + |
| 83 | + # Add the images as possible glyphs |
| 84 | + |
| 85 | + bg <- l_glyph_add_image(periodicTable, |
| 86 | + images = boxGlyphs, |
| 87 | + label = "Symbol box") |
| 88 | + |
| 89 | + # Set this to be the glyph |
| 90 | + periodicTable['glyph'] <- bg |
| 91 | + |
| 92 | + message("Enlarge the window so that all elements can be seen. \n") |
| 93 | + |
| 94 | + readline( |
| 95 | + "Hit <Return> to construct a plot of each element's Density versus its Mass.") |
| 96 | + # |
| 97 | + # Get a second plot that shows the periodicity |
| 98 | + # |
| 99 | + # First some itemlabels |
| 100 | + elementLabels <- with(elements, |
| 101 | + paste(" ", Number, Symbol, "\n", |
| 102 | + " ", Name, "\n", |
| 103 | + " ", Mass |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + periodicPlot <- l_plot(x = elements$Mass, y = elements$Density, |
| 108 | + xlabel = "Mass", ylabel = "Density", |
| 109 | + itemLabel = elementLabels, |
| 110 | + showItemLabels = TRUE, |
| 111 | + linkingGroup = "elements", |
| 112 | + color = tableCols) |
| 113 | + |
| 114 | + |
| 115 | + bg2 <- l_glyph_add_image(periodicPlot, |
| 116 | + images = boxGlyphs, |
| 117 | + label = "Symbol box") |
| 118 | + |
| 119 | + |
| 120 | + readline( |
| 121 | + "Hit <Return> to show the noble gases by their 'Symbol Box' glyph") |
| 122 | + |
| 123 | + nobleGases <- elements$Subcategory == "Noble gas" |
| 124 | + periodicPlot["glyph"][nobleGases] <- "glyph0" |
| 125 | + readline( |
| 126 | + "Hit <Return> to show the reactive nonmetals by their 'Symbol Box' glyph") |
| 127 | + reactiveNonMetals <- elements$Subcategory == "Reactive nonmetal" |
| 128 | + periodicPlot["glyph"][reactiveNonMetals] <- "glyph0" |
| 129 | + |
| 130 | + readline("Hit <Return> to focus on the noble gases and the reactive nonmetals") |
| 131 | + periodicPlot["active"] <- nobleGases | reactiveNonMetals |
| 132 | + l_scaleto_active(periodicPlot) |
| 133 | + periodicPlot["active"] <- TRUE |
| 134 | + |
| 135 | + readline("Hit <Return> to return plot to closed circles and scale.") |
| 136 | + periodicPlot["glyph"] <- "ccircle" |
| 137 | + l_scaleto_world(periodicPlot) |
| 138 | + |
| 139 | + message(paste0("Suggestions: \n", |
| 140 | + " - Brush/Select in either plot to explore the periodicity of the elements. \n", |
| 141 | + " - On the table brushing whole rows or whole columns is interesting. \n", |
| 142 | + " - Try selecting by colour and setting the glyph to the symbol boxes. \n", |
| 143 | + " - So activating only selected colours and panning and zooming in the Mass/Density plot. \n", |
| 144 | + " - Activate only selected colours and then try panning and zooming in the Mass/Density plot. \n")) |
| 145 | + |
| 146 | + message() |
| 147 | + |
| 148 | + |
| 149 | + }) # End local |
| 150 | +} |
0 commit comments