You are on page 1of 25
16122, 12223 PM ‘A Introduction to Visibly = visibly? An Introduction to Visibly Michael Clark 2020-10-28 Source: vignettes/intro.And (hilps://cithub.com/m-clark/visibly/lob/master/vignettes/intro Rnd) Palettes & Themes One can get started by creating a palette. This uses the colortools package to provide evenly spaced colors, given a starting color. library (https://rdrr.io/r/base/library html) (visibly (https://github.com/m-clark/visibay, create_palette (../reference/create_palette.html)(‘papayawhip’, plot = TRUE) hitpsim-clark github otsiblyarteletinto: Atal 125 116122, 12223 PM ‘Aa Introduction to Visi = visio! Spapayawhip [1] “papayawhip” Scomplenentary [1] “#FFEFDS" Sanalogous [1] “#FFEFDS" “#FAFFDS" “#FFDADS Ssplit_conplentary [1] “#FFEFDS" "#DSFAFF" "#DADSFF” Striadic [1] “#FFEFDS" "#DSFFEF" "#EFDSFF" Ssquare [1] “#FFEFDS" "#DSFFDA" Stetradic [1] “#FFEFDS" "“#ESFFDS" "#DSESFF" "#EFDSFF" Jere is a palette based on the blue in the R logo. palettes$Rblue $Rblue (1) “#1f65b7" Scomplementary [1] "#16507" #b7701f" ‘monochromatic (1) “#1f65b7" "waa719e" "#507491" Sanalogous (1) *#1F6507" #241707" "#1 fb2b7" Ssplit_complementary [1] “#1f65b7" “#b2b71f" "#b7241F" Striadic [1] "#1f65b7" "#66b71f" "#7166" Stetradic [1] “#1f6567" “#>7761F" "“#eeb7IF" “#701Fb7" hitpsim-clark github atsibyartclestinto: mal 2705 (8/16/22, 12:23 PM ‘An Introduction to Visibly « visibly! The palette creator can create some decent categorical distinctions without too much fuss. The following also demonstrates one of the themes, which has no grid/gray, and de-bolds the black font while leaving text clear, even the fainter version will pass web standards for contrast against a white background, As shown, you can still fiddle with the theme beyond that. pal = create_palette (../reference/create_palette.html) ('#ff5500", name = ‘orange_you_glad_you_have_this.color') Library (https://rdrr.io/r/base/Library.html) (ggplot2 (http: //ggplot2.tidyverse.org)) ggplot (https: //ggplot2.tidyverse.org/reference/ggplot.html)(mtcars, aes (https://ggplot2 geom_point (https: //ggplot2.tidyverse.org/reference/geom_point.html) (aes (https://ggplot scale_color_manual (https://ggplot2.tidyverse.org/reference/scale_manual.html) (values = ‘theme_clean (../reference/theme_clean.html) () vo os e factor(cyl) @: @: @ 7 qe hitpsim-clark github otsiblyarteletinto: Atal 35 116122, 12223 PM ‘A Introduction to Visibly = visibly? library (https: Inder .io/r/base/library html) (dplyr (https: //dplyr.tidyverse.org)) mtcars %% mutate (https://dplyr. tidyverse.org/reference/mutate html) (cyl = factor (https://rdrr.ic tidyext : :num_by (https://m-clark .github.io/tidyext/reference/num_by html) (wt, cyl) %>% ggplot (https: //ggplot2.tidyverse.org/reference/ggplot tml) (aes (https: //ggplot2.tidyve geom_col (https: //ggplot2.tidyverse .org/reference/geom_bar.html) (aes (https://ggplot2.ti scale_fill_manual (https: //ggplot2. tidyverse.org/reference/scale_manual.html) (values = ¢ ‘theme_clean (../reference/theme.clean.html)() + ‘theme (https: //ggplot2.tidyverse.org/reference/ theme .html) ( legend. key.size = unit (https://rdrr.io/r/grid/unit.ntml)(.@15, ‘npe'), axis.title.y = element_text (https: //ggplot2.tidyverse.org/reference/element .html) (siz ) Mean, oyl on Another way to create palettes is with a function that works with colorgorical (httpi//vrl.cs.brown.edu/color) It connects to that website to create colors based on things like whether they perceptually go together aesthetically, whether they are more or less perceptually distinct, or even if their name is unique hitpsim-clark github otsiblyarteletinto: Atal 425 116122, 12223 PM ‘Aa Introduction to Visi = visio! colorgorical (../reference/colorgorical html) ( n= 6, pairPreference = 1, startPalette = list (https://rdrr.io/r/base/list html) (c (https://rdrr.io/r/base/c. html} output = ‘hex’ [1] "#982808" "#95C857" "¥334D37" "#4EF185" "#378811" "#7FE7D3" 16 colorgorical (../reference/colorgorical html) ( n= 10, perceptualDifference = .5, startPalette = list (https://rdrr.io/r/base/list.html)(c (https://rdrr.io/r/base/c.html} output = ‘hex’ [1] "#8026800" "#057381" "#77CE3F" "#DBGEAC' hitpsim-clark github atsibyartclestinto: mal 5125 16122, 12223 PM ‘Aa Introduction to Visi = visibiy! To get a starting palette from an R or hexadecimal value, try col2lab col21ab('dodgerblue' ) L a b [1,1 59.30679 9.612¢98 -63.4918 You can check your to see if your chosen color combinations would pass web standards for contrast, sensitivity, For example, if you use blue for your hyperlinks and consider it against a white background for a webpage it passes, color_contrast_checker(foreground = ‘blue’) ratio AA AALarge AAA AAALarge 1 8.59 pass pass pass —pass However, if you consider the background as the surrounding text, perhaps not, color_contrast_checker(foreground = ‘blue’, background = ‘black’ ) ratio AA AALarge AAA AAALarge 1 2.44 fail fail fail fail Default ggplot2 colors don't contrast well with the background. Ack! hitpsim-clark github otsiblyarteletinto: Atal 8125 116122, 12223 PM ‘Aa Introduction to Visi = visio! iris ®% ggplot (https: //ggplot2.tidyverse.org/reference/ggplot.html)(aes (https: //ggplot2.tidyve geom_point (https: //ggplot2.tidyverse.org/reference/geom_point .html) () gs Species 3 + tose z 3 © vericokr 2 4 + sen 10 PetalLength scales: :show_col (https://scales.r-1ib.org//reference/show_col.html) (scales: :hue_pal (httr hitpsim-clark github atsibyartclestinto: mal 728 16122, 12223 PM ‘An Introduction to Visibly = visibly color.contrast_checker( foreground = '#F8766D', background ratio AA AALarge AAA AAALarge 1 2.25 fail fail fail — fail color.contrast_checker( foreground = '#06BA38", background ratio AA AALarge AAA AAALarge 12.17 fail fail fail — fail color_contrast_checker( foreground = '#619CFF", background ratio AA AALarge AAA AAALarge 1 2.28 fail fail fail fail. “gray92") *“gray92") “gray92") However, if you want palettes that are colorblind-safe, print-safe etc,, especially for continuous scales, you should use packages like viridis (https.//github.corn/sjmgamnier/viridis) and scico (https://github.corn/thomasp85/scico). Descriptive Visualization People often want to (and should) examine correlation matrices as a prelude to modeling. One problem is ‘that itis difficult to make much sense from the matrix when there are more than a few columns in the original data, as there are too many pairwise values present, However a little reshuffling of the rows/colurnns, and visually depicting the values by color can make patterns pop out quickly. hitpsim-clark github atsibyartclestinto: mal 825, sr, 12:29 Pm ‘An inroducton to Vib = visibly! You have plenty of options for this sort of endeavor, such as corrplot, psych, and heatmaply. However, they offer only static plots, or reorder based on a cluster analysis. | originally had a function (https://github.corn/m-clark/heatf) that would provide some interactivity, but while utilizing a factor analytic approach. I've now deprecated that standalone package and brought the functionality here. The corr_heat function provides an easy way to visualize your correlations. data (https://rdrr.io/r/utils/data.html)("bfi’, package = ‘visibly’) cor_matrix = cor (https://rdrr.io/r/stats/cor.html) (bfi, use='pair’) corr_heat (../reference/corr_heat .html) (cor_matrix) cormat 1 os NI N2.N3 NS N@ E2 E4 E1 ES E3 C2 C4 C3 C5 C1 AB AZ AS Ad At 03.0501 0204 The psych package is used to for the factor analysis, which is better suited to survey data and similar settings, where iterns are of the same type, and you can supply argurnents to its fa function. The plot comes from plotly, and hovering over the squares provides the value. corr_heat (../reference/corr_heat html) (cor_matrix, n.factors = 2, psych.opts = list (https: //rdrr.io/r/base/list.html)(fm = 'ml', rot = ‘oblimin') hitpsim-clark github atsiblyfartelestinto: hal 925 116122, 1223 PM ‘A Introduction to Visibly = visibly? a NI N2.N3 NS C4 C2 C5 C3 C1 A3 AZ AS Ad Al E2 El E4 N4 ES E3. 0301050204 The color scheme is based on the previously mentioned scico package. corr_heat (../reference/corr_heat.html)(cor_matrix, pal = 'broc’) ws we te ws ne 2 & = 5 5 & ¢ S ’ sa iS & s hitpsim-clark github otsibyarteletinto. Nal cormat 1 os cormat 1 0s 116122, 1223 PM ‘A Introduction to Visibly = visibly! We can see that while there might be the usual ‘Big Five’ personality structure, it ray also really just be two underlying constructs- neuroticism vs. the rest, at least for this sample. It occurred to me while updating this functionality that a 3-d approach might actually be useful here (it isn’t the vast majority of the time). When looking at the previous structure in 3d, we probably would only think about two factors of items. P = corr_heat (../reference/corr_heat.html)(cor_matrix, three_d = TRUE, diagonal = NA) P cormat 1 os ° of 05 ah = ot ° at hitpsim-clark github atsibyartclestinto: mal 1128 16122, 12223 PM ‘A Introduction to Visibly = visibly? There are multiple orderings available to help bring out the structure. This may be particularly useful it you are not dealing with measurement error type data, p = corr-heat (../reference/corr_heat.html)(cor (https://rdrr.io/r/stats/cor html) (mtca, , P cormat 1 os Who knew the mtcars data set could actually be interesting! Plotting Model Results Ihave sore visualizations for plotting uncertainty intervals for fixed and random effects of mixed models. These require the scico package, as well as Ime4 and brms which are used to do the mixed models in the first place. hitpsim-clark github atsibyartclestinto: mal 1208 16122, 12223 PM ‘An Introduction to Visibly = visibly! Im and gim | don't really use them aside for demonstration, but in this case the Im and glm objects served as a baseline. fit_lm = Im (https://rdrr.io/r/stats/Im.html)(mpg ~ ., mtcars) plot_coefficients (../reference/plot_coefficients.html) (fit_1m) we _——— cao a on = ve 9 isp 6 we = eer ————— rat ———— ~ soc a om __ 4 4 o a Cootint Change the palette, order and more. fit_lm = Im (https://rdrr.io/r/stats/Im.html)(mpg ~ ., mtcars) plot_coefficients (../reference/plot_coefficients html) ( fitLin, palette = ‘oslo’, order = ‘decreasing’, sd_multi = 1, keep_intercept = TRUE, ref_line = ¢ (https://rdrr.io/r/base/c.html) (-1:1) hitpsim-clark github atsibyartclestinto: mal 1325 16122, 12223 PM i at see (torent Data only ‘A Introduction to Visibly = visibly? Ey Ccootint You may want to do your own visualization, or go beyond the default settings. Rather than providing a couple dozen arguments for you to tweak, just so you can still feel like you can't get it just quite right. Here's the data, do with it what you will plot_coefficients(fit-Im, plot = FALSE) Coefficient am gsec drat gear vs disp hp - cyl -8 carb -8 a wt -3 Mixed models 82022689 -1.59307422 01333524 6 2 4 3 .31776281 -3.8912544@ 4.52678003, a 202148212 @. 1 1 8. value vill ui_u -63352799 -28273034 -05785711 -64193294 82104075 -0.64064884 78711097 -2.48363516 65541302 -2.33110691 02237976 06501928 20148720 85692425 50413253, eagesa24 02205504 97860625 -45808574 07352467 11144048 19941925 71536393 hitpsim-clark github atsibyartclestinto: mal 1428 ‘116122, 12:23 PM ‘An Introduction to Visibly «iio | do a lot of mixed models, which is my main reason for providing this functionality, so I've started to create some ways to plot the results of the separate fixed and random effects. Well use Ime4. library (https://rdrr.io/r/base/Library.html)(1me4 (https: //github.com/Ime4/1me4/)) fit_mer = Imer (https://rdrr.i0/pkg/lme4/man/ mer .html) (Reaction ~ Days + (Days|Subject), plot_coefficients (../reference/plot_coefficients html) (fit_mer) aye ———~ Cooficent And now the random effects. Fewer options can be found here, but this is intentional as you'd want them ordered and the fixed effect style would make things messy whenever there are many random effects, which is often the case. With multiple random effects, a list of ggplot objects is returned plot_coefficients(fit_mer, ranef = TRUE, which_ranef = 'Subject') $* (Intercept) hitpsim-clark github atsibyartclestinto: mal 1525 16122, 12223 PM ‘An Introduction to Visibly = visibly cootent 309 io sto si 360 334 335 s71 08 aéo 351 382 372 333 a2 331 330 337 SDays cout -10- | | | 385 360 sio ato 331 381 371 siz 333 a80 sia sia 372 362 sto 360 337 308 hitpsim-clark github atsibyartclestinto: mal 1825 sr, 12:29 Pm ‘An inroducton to Vib = visibly! Now fora different palette. Again, these only apply to fixed effects visualizations, due to the number of categories typically seen with random effects and how the visualization is created fit_mer2 = Imer (https://rdrr.io/pkg/Ime4/man/Imer html) (count ~ zAge + zBase * Trt + (1 | patient), data = brns::epilepsy (https: //rdrr.io/pkg/brms/man/epilepsy .html) ) plot_coefficients (../reference/plot_coefficients .html)(fit_mer2, palette = ‘berlin’) TT 2hge- —— sBasesTAt~ gg Base a o 8 to Cootint You can use patchwork to put them into one graph. plots = plot_coefficients (../reference/plot_coefficients.html)(fit_mer, ranef = TRUE, ws library (https://rdrr.io/r/base/Library.html) (patchwork (https: //patchwork.data-imaginist plots[[1]] + plots[[2]] hitpsim-clark github atsibyartclestinto: mal 1108 16122, 12223 PM ‘A Introduction to Visibly = visibly? borms Ilike brms quite a bit and use it a lot. It served as the rnotivation for these coefficient plots, as did bayesplot, and tidybayes, library (https://rdrr.io/r/base/library html) (brms (https: //github.com/paul-buerkner/braw, fit_brms_two = brm (https://rdrr.io/pkg/brms/man/brm.html) (count ~ zAge + zBase * Trt + (1 | patient) + (1 | obs), data = epilepsy, family = poisson) plot_coefficients (../reference/plot_coefficients html) (fit_brms_two) hitpsim-clark github atsibyartclestinto: mal 18128 116122, 1223 PM ‘A Introduction to Visibly = visibly? tet —————— _~ 2ge- ——— 2BaseTAt= a ————_ Base —— 3 ° 3 é é And now the random effects. plot_coefficients (../reference/plot_coefficients.html)(fit_brms_two, ranef = TRUE, whic. ‘theme (https: //ggplot2. tidyverse.org/reference/theme.html) (axis.text.x = element_text (t hitpsim-clark github atsibyartclestinto: mal 1925 16122, 12223 PM ‘A Introduction to Visibly = visibly? 20- | Cooffcent GAM Some very rudimentary plotting of basic GAM effects provided by the mgev are provided. | really ike the package, but am not crazy for the default plots, and some offered by some other packages are okay, but may not be polished enough or do the smooth plots Id eventually ike to do. library(mgev) d = gamSim() Gu & Wahba 4 term additive model gam_model = gam(y ~ x@ + s(x1) + s(x2, bs='gp") + s(x3, bs="ps'), data=d) plot_gam(gam_model, main_var = x2) hitpsim-clark github atsibyartclestinto: mal 20125 16122, 12223 PM ‘A Introduction to Visibly = visibly? f aio 02s ob ois bo ‘awe plot_gam (../reference/plot_gam.html) (gam_model, main_var = vars (https://dplyr .tidyverse.org/reference/vars.html) (x1, x2, x3), neol = 1, line_color = palettes$RbluesRblue, ribbon_color = palettes$RblueScomplementary|[2]) hitpsim-clark github atsibyartclestinto: mal 21128 116122, 1223 PM ‘A Introduction to Visibly = visibly? ft ya 10 5 e 000 025 080 ors 1.00 2 10° S 00 025, 050 075 1.00 8 10° 65° 60- 000 025, 050 075 plot_gam.check (.. /reference/plot_gam_check html) (gam_model) sample residuals 5 2 theoretical linear prosictor ensiy density 0125+ oss 0.100- oors- 10 1) ted values .050- y poe 0.028: 0.00- 5 é 5 to 15 reside ‘value hitpsim-clark github atsibyartclestinto: mal 225 116122, 1223 PM ‘A Introduction to Visibly = visibly? 2d smooths ‘Some initial functionality is provided for continuous-by-continuous interactions and continuous-by- categorical interaction plots, d = gamsim(2, scale = .1)Sdata Bivariate smoothing example gam_model = gam(y ~ s(x, z, bs='gp'), data=d) plot_gan_2d(gam_nodel, main_var = x, second_var = z, n_plot = 260) 100" o75- prediction os 050- 04 02 oo 025- 0.00- 000 025, 050 075 1.00 In 3d, plot_gam_d (../reference/plot_gam_3d. html) (gam_model, mainvar = x, second_var = z, n_plot = 208) hitpsim-clark github atsibyartclestinto: mal 2325 116122, 1223 PM A categorical moderator. d = gamSim(4) Factor ‘by’ variable example ‘An Introduction to Visibly = visibly! pred_mat 06 04 02 gam_model = gam(y ~ s(x2, fac, bs='fs'), data=d) plot_gam_by(gam_model, main_var = x2, by_var = fac) hitpsim-clark github atsibyartclestinto: mal 2425 16122, 12223 PM ‘A Introduction to Visibly = visibly? prodicion fac —2 Developed by ARS (https://m-clark.github.io). Site built with pkgdown (https://pkgdown.ib.org/) 16.1 hitpsim-clark github atsibyartclestinto: mal 25125

You might also like