Let’s make some nanoplots with the illness dataset. The columns beginning with ‘day’ all contain ordered measurement values, comprising seven individual daily results. Using cols_nanoplot() we create a new column to hold the nanoplots (with new_col_name = "nanoplots"), referencing the columns containing the data (with columns = starts_with("day")). It’s also possible to define a column label here using the new_col_label argument.
illness |> dplyr::slice_head(n =10) |>gt(rowname_col ="test") |>tab_header("Partial summary of daily tests performed on YF patient") |>tab_stubhead(label =md("**Test**")) |>cols_hide(columns =starts_with("norm")) |>fmt_units(columns = units) |>cols_nanoplot(columns =starts_with("day"),new_col_name ="nanoplots",new_col_label =md("*Progression*") ) |>cols_align(align ="center", columns = nanoplots) |>cols_merge(columns =c(test, units), pattern ="{1} ({2})") |>tab_footnote(footnote ="Measurements from Day 3 through to Day 8.",locations =cells_column_labels(columns = nanoplots) )
Partial summary of daily tests performed on YF patient
Test
Progression1
Viral load (copies per mL)
WBC (×109/L)
Neutrophils (×109/L)
RBC (×1012/L)
Hb (g/L)
PLT (×109/L)
ALT (U/L)
AST (U/L)
TBIL (µmol/L)
DBIL (µmol/L)
1 Measurements from Day 3 through to Day 8.
The previous table showed us some line-based nanoplots. We can also make very small bar plots with cols_nanoplot(). Let’s take the pizzaplace dataset and make a small summary table showing daily pizza sales by type (there are four types). This will be limited to the first ten days of pizza sales in 2015, so, there will be ten rows in total. We can use plot_type = "bar" to make bar plots from the daily sales counts in the chicken, classic, supreme, and veggie columns. Because we know there will always be four bars (one for each type of pizza) we can be a little creative and apply colors to each of the bars through use of the data_bar_fill_color argument in nanoplot_options().
Now we’ll make another table that contains two columns of nanoplots. Starting from the towny dataset, we first reduce it down to a subset of columns and rows. All of the columns related to either population or density will be used as input data for the two nanoplots. Both nanoplots will use a reference line that is generated from the median of the input data. And by naming the new nanoplot-laden columns in a similar manner as the input data columns, we can take advantage of select helpers (e.g., when using tab_spanner()). Many of the input data columns are now redundant because of the plots, so we’ll elect to hide most of those with cols_hide().
The sza dataset can, with just some use of dplyr and tidyr, give us a wide table full of nanoplottable values. We’ll transform the solar zenith angles to solar altitude angles and create a column of nanoplots using the newly calculated values. There are a few NA values during periods where the sun hasn’t risen (usually before 06:30 in the winter months) and those values will be replaced with 0 using missing_vals = "zero". We’ll also elect to create bar plots using the plot_type = "bar" option. The height of the plots will be bumped up to "2.5em" from the default of "2em". Finally, we will use nanoplot_options() to modify the coloring of the data bars.
sza |> dplyr::filter(latitude ==20& tst <="1200") |> dplyr::select(-latitude) |> dplyr::filter(!is.na(sza)) |> dplyr::mutate(saa =90- sza) |> dplyr::select(-sza) |> tidyr::pivot_wider(names_from = tst,values_from = saa,names_sort =TRUE ) |>gt(rowname_col ="month") |>tab_header(title ="Solar Altitude Angles",subtitle ="Average values every half hour from 05:30 to 12:00" ) |>cols_nanoplot(columns =matches("0"),plot_type ="bar",missing_vals ="zero",new_col_name ="saa",plot_height ="2.5em",options =nanoplot_options(data_bar_stroke_color ="GoldenRod",data_bar_fill_color ="DarkOrange" ) ) |>tab_options(table.width =px(400),column_labels.hidden =TRUE ) |>cols_align(align ="center",columns =everything() ) |>tab_source_note(source_note ="The solar altitude angle is the complement to the solar zenith angle. TMYK." )
Solar Altitude Angles
Average values every half hour from 05:30 to 12:00
jan
feb
mar
apr
may
jun
jul
aug
sep
oct
nov
dec
The solar altitude angle is the complement to
the solar zenith angle. TMYK.
You can use number and time streams as data for nanoplots. Let’s demonstrate how we can make use of them with some creative transformation of the pizzaplace dataset. A value stream is really a string with delimited numeric values, like this: "7.24,84.2,14". A value stream can also contain dates and/or datetimes, and here’s an example of that: "2020-06-02 13:05:13,2020-06-02 14:24:05,2020-06-02 18:51:37". Having data in this form can often be more convenient since different nanoplots might have varying amounts of data (and holding different amounts of data in a fixed number of columns is cumbersome). There are date and time columns in this dataset and we’ll use that to get x values denoting high-resolution time instants: the second of the day that a pizza was sold (this is true pizza analytics). We also have the sell price for a pizza, and that’ll serve as the y values. The pizzas belong to four different groups (in the type column) and we’ll group by that and create value streams with paste(..., collapse = ",") inside the dplyr::summarize() call. With two value streams in each row (having the same number of values) we can now make a gt table with nanoplots.
Notice that the columns containing the value streams are hid due to the default argument autohide = TRUE because, while useful, they don’t need to be displayed to anybody viewing a table. Since we have a lot of data points and a connecting line is not as valuable here, we also set show_data_line = FALSE in nanoplot_options(). It’s more interesting to see the clusters of the differently priced pizzas over the entire day. Specifying a currency in nanoplot_options() is a nice touch since the y values are sale prices in U.S. Dollars (hovering over data points gives correctly formatted values). Finally, having a reference line based on the median gives pretty useful information. Seems like customers preferred getting the "chicken"-type pizzas in large size!
Using the gibraltar dataset, let’s make a series of nanoplots across the meteorological parameters of temperature, humidity, and wind speed. We’ll want to customize the appearance of the plots across three columns and we can make this somewhat simpler by assigning a common set of options through nanoplot_options(). In this table we want to make comparisons across nanoplots in a particular column easier, so, we’ll set autoscale = TRUE so that there is a common y-axis scale for each of the parameters (based on the extents of the data).
Box plots can be generated, and we just need to use plot_type = "boxplot" to make that type of nanoplot. Using a small portion of the pizzaplace dataset, we will create a simple table that displays a box plot of pizza sales for a selection of days. By converting the string-time 24-hour-clock time values to the number of seconds elapsed in a day, we get continuous values that can be incorporated into each box plot. And, by supplying a function to the y_val_fmt_fn argument within nanoplot_options(), we can transform the integer seconds values back to clock times for display on hover.