Data visualization

for incomplete datasets in R

Hanne Oberman

PhD candidate at Utrecht University

Missingness

Case study

set.seed(123)
library(palmerpenguins)
library(mice)
library(ggmice)
library(ggplot2)

Penguin populations

Code
ggplot(penguins, aes(x = year, fill = sex, group = sex)) +
  geom_bar(position = "dodge") + 
  scale_x_continuous(breaks = c(2007, 2008, 2009)) +
  theme_classic()

Penguin populations

Code
ggplot(penguins, aes(x = year, fill = sex, group = sex)) +
  geom_bar(position = "dodge") + 
  facet_wrap(~species) + 
  scale_x_continuous(breaks = c(2007, 2008, 2009)) +
  theme_classic()

Incomplete data

str(penguins)
tibble [344 × 8] (S3: tbl_df/tbl/data.frame)
 $ species          : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ island           : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ bill_length_mm   : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
 $ bill_depth_mm    : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
 $ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
 $ body_mass_g      : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
 $ sex              : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
 $ year             : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...

Incomplete data

visdat::vis_dat(penguins)

Response indicator

is.na(penguins)
       species island bill_length_mm bill_depth_mm flipper_length_mm
  [1,]   FALSE  FALSE          FALSE         FALSE             FALSE
  [2,]   FALSE  FALSE          FALSE         FALSE             FALSE
  [3,]   FALSE  FALSE          FALSE         FALSE             FALSE
  [4,]   FALSE  FALSE           TRUE          TRUE              TRUE
  [5,]   FALSE  FALSE          FALSE         FALSE             FALSE
  [6,]   FALSE  FALSE          FALSE         FALSE             FALSE
  [7,]   FALSE  FALSE          FALSE         FALSE             FALSE
  [8,]   FALSE  FALSE          FALSE         FALSE             FALSE
  [9,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [10,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [11,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [12,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [13,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [14,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [15,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [16,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [17,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [18,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [19,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [20,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [21,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [22,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [23,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [24,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [25,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [26,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [27,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [28,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [29,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [30,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [31,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [32,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [33,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [34,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [35,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [36,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [37,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [38,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [39,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [40,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [41,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [42,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [43,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [44,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [45,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [46,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [47,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [48,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [49,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [50,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [51,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [52,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [53,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [54,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [55,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [56,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [57,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [58,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [59,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [60,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [61,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [62,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [63,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [64,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [65,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [66,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [67,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [68,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [69,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [70,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [71,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [72,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [73,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [74,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [75,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [76,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [77,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [78,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [79,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [80,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [81,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [82,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [83,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [84,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [85,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [86,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [87,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [88,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [89,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [90,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [91,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [92,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [93,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [94,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [95,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [96,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [97,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [98,]   FALSE  FALSE          FALSE         FALSE             FALSE
 [99,]   FALSE  FALSE          FALSE         FALSE             FALSE
[100,]   FALSE  FALSE          FALSE         FALSE             FALSE
[101,]   FALSE  FALSE          FALSE         FALSE             FALSE
[102,]   FALSE  FALSE          FALSE         FALSE             FALSE
[103,]   FALSE  FALSE          FALSE         FALSE             FALSE
[104,]   FALSE  FALSE          FALSE         FALSE             FALSE
[105,]   FALSE  FALSE          FALSE         FALSE             FALSE
[106,]   FALSE  FALSE          FALSE         FALSE             FALSE
[107,]   FALSE  FALSE          FALSE         FALSE             FALSE
[108,]   FALSE  FALSE          FALSE         FALSE             FALSE
[109,]   FALSE  FALSE          FALSE         FALSE             FALSE
[110,]   FALSE  FALSE          FALSE         FALSE             FALSE
[111,]   FALSE  FALSE          FALSE         FALSE             FALSE
[112,]   FALSE  FALSE          FALSE         FALSE             FALSE
[113,]   FALSE  FALSE          FALSE         FALSE             FALSE
[114,]   FALSE  FALSE          FALSE         FALSE             FALSE
[115,]   FALSE  FALSE          FALSE         FALSE             FALSE
[116,]   FALSE  FALSE          FALSE         FALSE             FALSE
[117,]   FALSE  FALSE          FALSE         FALSE             FALSE
[118,]   FALSE  FALSE          FALSE         FALSE             FALSE
[119,]   FALSE  FALSE          FALSE         FALSE             FALSE
[120,]   FALSE  FALSE          FALSE         FALSE             FALSE
[121,]   FALSE  FALSE          FALSE         FALSE             FALSE
[122,]   FALSE  FALSE          FALSE         FALSE             FALSE
[123,]   FALSE  FALSE          FALSE         FALSE             FALSE
[124,]   FALSE  FALSE          FALSE         FALSE             FALSE
[125,]   FALSE  FALSE          FALSE         FALSE             FALSE
[126,]   FALSE  FALSE          FALSE         FALSE             FALSE
[127,]   FALSE  FALSE          FALSE         FALSE             FALSE
[128,]   FALSE  FALSE          FALSE         FALSE             FALSE
[129,]   FALSE  FALSE          FALSE         FALSE             FALSE
[130,]   FALSE  FALSE          FALSE         FALSE             FALSE
[131,]   FALSE  FALSE          FALSE         FALSE             FALSE
[132,]   FALSE  FALSE          FALSE         FALSE             FALSE
[133,]   FALSE  FALSE          FALSE         FALSE             FALSE
[134,]   FALSE  FALSE          FALSE         FALSE             FALSE
[135,]   FALSE  FALSE          FALSE         FALSE             FALSE
[136,]   FALSE  FALSE          FALSE         FALSE             FALSE
[137,]   FALSE  FALSE          FALSE         FALSE             FALSE
[138,]   FALSE  FALSE          FALSE         FALSE             FALSE
[139,]   FALSE  FALSE          FALSE         FALSE             FALSE
[140,]   FALSE  FALSE          FALSE         FALSE             FALSE
[141,]   FALSE  FALSE          FALSE         FALSE             FALSE
[142,]   FALSE  FALSE          FALSE         FALSE             FALSE
[143,]   FALSE  FALSE          FALSE         FALSE             FALSE
[144,]   FALSE  FALSE          FALSE         FALSE             FALSE
[145,]   FALSE  FALSE          FALSE         FALSE             FALSE
[146,]   FALSE  FALSE          FALSE         FALSE             FALSE
[147,]   FALSE  FALSE          FALSE         FALSE             FALSE
[148,]   FALSE  FALSE          FALSE         FALSE             FALSE
[149,]   FALSE  FALSE          FALSE         FALSE             FALSE
[150,]   FALSE  FALSE          FALSE         FALSE             FALSE
[151,]   FALSE  FALSE          FALSE         FALSE             FALSE
[152,]   FALSE  FALSE          FALSE         FALSE             FALSE
[153,]   FALSE  FALSE          FALSE         FALSE             FALSE
[154,]   FALSE  FALSE          FALSE         FALSE             FALSE
[155,]   FALSE  FALSE          FALSE         FALSE             FALSE
[156,]   FALSE  FALSE          FALSE         FALSE             FALSE
[157,]   FALSE  FALSE          FALSE         FALSE             FALSE
[158,]   FALSE  FALSE          FALSE         FALSE             FALSE
[159,]   FALSE  FALSE          FALSE         FALSE             FALSE
[160,]   FALSE  FALSE          FALSE         FALSE             FALSE
[161,]   FALSE  FALSE          FALSE         FALSE             FALSE
[162,]   FALSE  FALSE          FALSE         FALSE             FALSE
[163,]   FALSE  FALSE          FALSE         FALSE             FALSE
[164,]   FALSE  FALSE          FALSE         FALSE             FALSE
[165,]   FALSE  FALSE          FALSE         FALSE             FALSE
[166,]   FALSE  FALSE          FALSE         FALSE             FALSE
[167,]   FALSE  FALSE          FALSE         FALSE             FALSE
[168,]   FALSE  FALSE          FALSE         FALSE             FALSE
[169,]   FALSE  FALSE          FALSE         FALSE             FALSE
[170,]   FALSE  FALSE          FALSE         FALSE             FALSE
[171,]   FALSE  FALSE          FALSE         FALSE             FALSE
[172,]   FALSE  FALSE          FALSE         FALSE             FALSE
[173,]   FALSE  FALSE          FALSE         FALSE             FALSE
[174,]   FALSE  FALSE          FALSE         FALSE             FALSE
[175,]   FALSE  FALSE          FALSE         FALSE             FALSE
[176,]   FALSE  FALSE          FALSE         FALSE             FALSE
[177,]   FALSE  FALSE          FALSE         FALSE             FALSE
[178,]   FALSE  FALSE          FALSE         FALSE             FALSE
[179,]   FALSE  FALSE          FALSE         FALSE             FALSE
[180,]   FALSE  FALSE          FALSE         FALSE             FALSE
[181,]   FALSE  FALSE          FALSE         FALSE             FALSE
[182,]   FALSE  FALSE          FALSE         FALSE             FALSE
[183,]   FALSE  FALSE          FALSE         FALSE             FALSE
[184,]   FALSE  FALSE          FALSE         FALSE             FALSE
[185,]   FALSE  FALSE          FALSE         FALSE             FALSE
[186,]   FALSE  FALSE          FALSE         FALSE             FALSE
[187,]   FALSE  FALSE          FALSE         FALSE             FALSE
[188,]   FALSE  FALSE          FALSE         FALSE             FALSE
[189,]   FALSE  FALSE          FALSE         FALSE             FALSE
[190,]   FALSE  FALSE          FALSE         FALSE             FALSE
[191,]   FALSE  FALSE          FALSE         FALSE             FALSE
[192,]   FALSE  FALSE          FALSE         FALSE             FALSE
[193,]   FALSE  FALSE          FALSE         FALSE             FALSE
[194,]   FALSE  FALSE          FALSE         FALSE             FALSE
[195,]   FALSE  FALSE          FALSE         FALSE             FALSE
[196,]   FALSE  FALSE          FALSE         FALSE             FALSE
[197,]   FALSE  FALSE          FALSE         FALSE             FALSE
[198,]   FALSE  FALSE          FALSE         FALSE             FALSE
[199,]   FALSE  FALSE          FALSE         FALSE             FALSE
[200,]   FALSE  FALSE          FALSE         FALSE             FALSE
[201,]   FALSE  FALSE          FALSE         FALSE             FALSE
[202,]   FALSE  FALSE          FALSE         FALSE             FALSE
[203,]   FALSE  FALSE          FALSE         FALSE             FALSE
[204,]   FALSE  FALSE          FALSE         FALSE             FALSE
[205,]   FALSE  FALSE          FALSE         FALSE             FALSE
[206,]   FALSE  FALSE          FALSE         FALSE             FALSE
[207,]   FALSE  FALSE          FALSE         FALSE             FALSE
[208,]   FALSE  FALSE          FALSE         FALSE             FALSE
[209,]   FALSE  FALSE          FALSE         FALSE             FALSE
[210,]   FALSE  FALSE          FALSE         FALSE             FALSE
[211,]   FALSE  FALSE          FALSE         FALSE             FALSE
[212,]   FALSE  FALSE          FALSE         FALSE             FALSE
[213,]   FALSE  FALSE          FALSE         FALSE             FALSE
[214,]   FALSE  FALSE          FALSE         FALSE             FALSE
[215,]   FALSE  FALSE          FALSE         FALSE             FALSE
[216,]   FALSE  FALSE          FALSE         FALSE             FALSE
[217,]   FALSE  FALSE          FALSE         FALSE             FALSE
[218,]   FALSE  FALSE          FALSE         FALSE             FALSE
[219,]   FALSE  FALSE          FALSE         FALSE             FALSE
[220,]   FALSE  FALSE          FALSE         FALSE             FALSE
[221,]   FALSE  FALSE          FALSE         FALSE             FALSE
[222,]   FALSE  FALSE          FALSE         FALSE             FALSE
[223,]   FALSE  FALSE          FALSE         FALSE             FALSE
[224,]   FALSE  FALSE          FALSE         FALSE             FALSE
[225,]   FALSE  FALSE          FALSE         FALSE             FALSE
[226,]   FALSE  FALSE          FALSE         FALSE             FALSE
[227,]   FALSE  FALSE          FALSE         FALSE             FALSE
[228,]   FALSE  FALSE          FALSE         FALSE             FALSE
[229,]   FALSE  FALSE          FALSE         FALSE             FALSE
[230,]   FALSE  FALSE          FALSE         FALSE             FALSE
[231,]   FALSE  FALSE          FALSE         FALSE             FALSE
[232,]   FALSE  FALSE          FALSE         FALSE             FALSE
[233,]   FALSE  FALSE          FALSE         FALSE             FALSE
[234,]   FALSE  FALSE          FALSE         FALSE             FALSE
[235,]   FALSE  FALSE          FALSE         FALSE             FALSE
[236,]   FALSE  FALSE          FALSE         FALSE             FALSE
[237,]   FALSE  FALSE          FALSE         FALSE             FALSE
[238,]   FALSE  FALSE          FALSE         FALSE             FALSE
[239,]   FALSE  FALSE          FALSE         FALSE             FALSE
[240,]   FALSE  FALSE          FALSE         FALSE             FALSE
[241,]   FALSE  FALSE          FALSE         FALSE             FALSE
[242,]   FALSE  FALSE          FALSE         FALSE             FALSE
[243,]   FALSE  FALSE          FALSE         FALSE             FALSE
[244,]   FALSE  FALSE          FALSE         FALSE             FALSE
[245,]   FALSE  FALSE          FALSE         FALSE             FALSE
[246,]   FALSE  FALSE          FALSE         FALSE             FALSE
[247,]   FALSE  FALSE          FALSE         FALSE             FALSE
[248,]   FALSE  FALSE          FALSE         FALSE             FALSE
[249,]   FALSE  FALSE          FALSE         FALSE             FALSE
[250,]   FALSE  FALSE          FALSE         FALSE             FALSE
[251,]   FALSE  FALSE          FALSE         FALSE             FALSE
[252,]   FALSE  FALSE          FALSE         FALSE             FALSE
[253,]   FALSE  FALSE          FALSE         FALSE             FALSE
[254,]   FALSE  FALSE          FALSE         FALSE             FALSE
[255,]   FALSE  FALSE          FALSE         FALSE             FALSE
[256,]   FALSE  FALSE          FALSE         FALSE             FALSE
[257,]   FALSE  FALSE          FALSE         FALSE             FALSE
[258,]   FALSE  FALSE          FALSE         FALSE             FALSE
[259,]   FALSE  FALSE          FALSE         FALSE             FALSE
[260,]   FALSE  FALSE          FALSE         FALSE             FALSE
[261,]   FALSE  FALSE          FALSE         FALSE             FALSE
[262,]   FALSE  FALSE          FALSE         FALSE             FALSE
[263,]   FALSE  FALSE          FALSE         FALSE             FALSE
[264,]   FALSE  FALSE          FALSE         FALSE             FALSE
[265,]   FALSE  FALSE          FALSE         FALSE             FALSE
[266,]   FALSE  FALSE          FALSE         FALSE             FALSE
[267,]   FALSE  FALSE          FALSE         FALSE             FALSE
[268,]   FALSE  FALSE          FALSE         FALSE             FALSE
[269,]   FALSE  FALSE          FALSE         FALSE             FALSE
[270,]   FALSE  FALSE          FALSE         FALSE             FALSE
[271,]   FALSE  FALSE          FALSE         FALSE             FALSE
[272,]   FALSE  FALSE           TRUE          TRUE              TRUE
[273,]   FALSE  FALSE          FALSE         FALSE             FALSE
[274,]   FALSE  FALSE          FALSE         FALSE             FALSE
[275,]   FALSE  FALSE          FALSE         FALSE             FALSE
[276,]   FALSE  FALSE          FALSE         FALSE             FALSE
[277,]   FALSE  FALSE          FALSE         FALSE             FALSE
[278,]   FALSE  FALSE          FALSE         FALSE             FALSE
[279,]   FALSE  FALSE          FALSE         FALSE             FALSE
[280,]   FALSE  FALSE          FALSE         FALSE             FALSE
[281,]   FALSE  FALSE          FALSE         FALSE             FALSE
[282,]   FALSE  FALSE          FALSE         FALSE             FALSE
[283,]   FALSE  FALSE          FALSE         FALSE             FALSE
[284,]   FALSE  FALSE          FALSE         FALSE             FALSE
[285,]   FALSE  FALSE          FALSE         FALSE             FALSE
[286,]   FALSE  FALSE          FALSE         FALSE             FALSE
[287,]   FALSE  FALSE          FALSE         FALSE             FALSE
[288,]   FALSE  FALSE          FALSE         FALSE             FALSE
[289,]   FALSE  FALSE          FALSE         FALSE             FALSE
[290,]   FALSE  FALSE          FALSE         FALSE             FALSE
[291,]   FALSE  FALSE          FALSE         FALSE             FALSE
[292,]   FALSE  FALSE          FALSE         FALSE             FALSE
[293,]   FALSE  FALSE          FALSE         FALSE             FALSE
[294,]   FALSE  FALSE          FALSE         FALSE             FALSE
[295,]   FALSE  FALSE          FALSE         FALSE             FALSE
[296,]   FALSE  FALSE          FALSE         FALSE             FALSE
[297,]   FALSE  FALSE          FALSE         FALSE             FALSE
[298,]   FALSE  FALSE          FALSE         FALSE             FALSE
[299,]   FALSE  FALSE          FALSE         FALSE             FALSE
[300,]   FALSE  FALSE          FALSE         FALSE             FALSE
[301,]   FALSE  FALSE          FALSE         FALSE             FALSE
[302,]   FALSE  FALSE          FALSE         FALSE             FALSE
[303,]   FALSE  FALSE          FALSE         FALSE             FALSE
[304,]   FALSE  FALSE          FALSE         FALSE             FALSE
[305,]   FALSE  FALSE          FALSE         FALSE             FALSE
[306,]   FALSE  FALSE          FALSE         FALSE             FALSE
[307,]   FALSE  FALSE          FALSE         FALSE             FALSE
[308,]   FALSE  FALSE          FALSE         FALSE             FALSE
[309,]   FALSE  FALSE          FALSE         FALSE             FALSE
[310,]   FALSE  FALSE          FALSE         FALSE             FALSE
[311,]   FALSE  FALSE          FALSE         FALSE             FALSE
[312,]   FALSE  FALSE          FALSE         FALSE             FALSE
[313,]   FALSE  FALSE          FALSE         FALSE             FALSE
[314,]   FALSE  FALSE          FALSE         FALSE             FALSE
[315,]   FALSE  FALSE          FALSE         FALSE             FALSE
[316,]   FALSE  FALSE          FALSE         FALSE             FALSE
[317,]   FALSE  FALSE          FALSE         FALSE             FALSE
[318,]   FALSE  FALSE          FALSE         FALSE             FALSE
[319,]   FALSE  FALSE          FALSE         FALSE             FALSE
[320,]   FALSE  FALSE          FALSE         FALSE             FALSE
[321,]   FALSE  FALSE          FALSE         FALSE             FALSE
[322,]   FALSE  FALSE          FALSE         FALSE             FALSE
[323,]   FALSE  FALSE          FALSE         FALSE             FALSE
[324,]   FALSE  FALSE          FALSE         FALSE             FALSE
[325,]   FALSE  FALSE          FALSE         FALSE             FALSE
[326,]   FALSE  FALSE          FALSE         FALSE             FALSE
[327,]   FALSE  FALSE          FALSE         FALSE             FALSE
[328,]   FALSE  FALSE          FALSE         FALSE             FALSE
[329,]   FALSE  FALSE          FALSE         FALSE             FALSE
[330,]   FALSE  FALSE          FALSE         FALSE             FALSE
[331,]   FALSE  FALSE          FALSE         FALSE             FALSE
[332,]   FALSE  FALSE          FALSE         FALSE             FALSE
[333,]   FALSE  FALSE          FALSE         FALSE             FALSE
[334,]   FALSE  FALSE          FALSE         FALSE             FALSE
[335,]   FALSE  FALSE          FALSE         FALSE             FALSE
[336,]   FALSE  FALSE          FALSE         FALSE             FALSE
[337,]   FALSE  FALSE          FALSE         FALSE             FALSE
[338,]   FALSE  FALSE          FALSE         FALSE             FALSE
[339,]   FALSE  FALSE          FALSE         FALSE             FALSE
[340,]   FALSE  FALSE          FALSE         FALSE             FALSE
[341,]   FALSE  FALSE          FALSE         FALSE             FALSE
[342,]   FALSE  FALSE          FALSE         FALSE             FALSE
[343,]   FALSE  FALSE          FALSE         FALSE             FALSE
[344,]   FALSE  FALSE          FALSE         FALSE             FALSE
       body_mass_g   sex  year
  [1,]       FALSE FALSE FALSE
  [2,]       FALSE FALSE FALSE
  [3,]       FALSE FALSE FALSE
  [4,]        TRUE  TRUE FALSE
  [5,]       FALSE FALSE FALSE
  [6,]       FALSE FALSE FALSE
  [7,]       FALSE FALSE FALSE
  [8,]       FALSE FALSE FALSE
  [9,]       FALSE  TRUE FALSE
 [10,]       FALSE  TRUE FALSE
 [11,]       FALSE  TRUE FALSE
 [12,]       FALSE  TRUE FALSE
 [13,]       FALSE FALSE FALSE
 [14,]       FALSE FALSE FALSE
 [15,]       FALSE FALSE FALSE
 [16,]       FALSE FALSE FALSE
 [17,]       FALSE FALSE FALSE
 [18,]       FALSE FALSE FALSE
 [19,]       FALSE FALSE FALSE
 [20,]       FALSE FALSE FALSE
 [21,]       FALSE FALSE FALSE
 [22,]       FALSE FALSE FALSE
 [23,]       FALSE FALSE FALSE
 [24,]       FALSE FALSE FALSE
 [25,]       FALSE FALSE FALSE
 [26,]       FALSE FALSE FALSE
 [27,]       FALSE FALSE FALSE
 [28,]       FALSE FALSE FALSE
 [29,]       FALSE FALSE FALSE
 [30,]       FALSE FALSE FALSE
 [31,]       FALSE FALSE FALSE
 [32,]       FALSE FALSE FALSE
 [33,]       FALSE FALSE FALSE
 [34,]       FALSE FALSE FALSE
 [35,]       FALSE FALSE FALSE
 [36,]       FALSE FALSE FALSE
 [37,]       FALSE FALSE FALSE
 [38,]       FALSE FALSE FALSE
 [39,]       FALSE FALSE FALSE
 [40,]       FALSE FALSE FALSE
 [41,]       FALSE FALSE FALSE
 [42,]       FALSE FALSE FALSE
 [43,]       FALSE FALSE FALSE
 [44,]       FALSE FALSE FALSE
 [45,]       FALSE FALSE FALSE
 [46,]       FALSE FALSE FALSE
 [47,]       FALSE FALSE FALSE
 [48,]       FALSE  TRUE FALSE
 [49,]       FALSE FALSE FALSE
 [50,]       FALSE FALSE FALSE
 [51,]       FALSE FALSE FALSE
 [52,]       FALSE FALSE FALSE
 [53,]       FALSE FALSE FALSE
 [54,]       FALSE FALSE FALSE
 [55,]       FALSE FALSE FALSE
 [56,]       FALSE FALSE FALSE
 [57,]       FALSE FALSE FALSE
 [58,]       FALSE FALSE FALSE
 [59,]       FALSE FALSE FALSE
 [60,]       FALSE FALSE FALSE
 [61,]       FALSE FALSE FALSE
 [62,]       FALSE FALSE FALSE
 [63,]       FALSE FALSE FALSE
 [64,]       FALSE FALSE FALSE
 [65,]       FALSE FALSE FALSE
 [66,]       FALSE FALSE FALSE
 [67,]       FALSE FALSE FALSE
 [68,]       FALSE FALSE FALSE
 [69,]       FALSE FALSE FALSE
 [70,]       FALSE FALSE FALSE
 [71,]       FALSE FALSE FALSE
 [72,]       FALSE FALSE FALSE
 [73,]       FALSE FALSE FALSE
 [74,]       FALSE FALSE FALSE
 [75,]       FALSE FALSE FALSE
 [76,]       FALSE FALSE FALSE
 [77,]       FALSE FALSE FALSE
 [78,]       FALSE FALSE FALSE
 [79,]       FALSE FALSE FALSE
 [80,]       FALSE FALSE FALSE
 [81,]       FALSE FALSE FALSE
 [82,]       FALSE FALSE FALSE
 [83,]       FALSE FALSE FALSE
 [84,]       FALSE FALSE FALSE
 [85,]       FALSE FALSE FALSE
 [86,]       FALSE FALSE FALSE
 [87,]       FALSE FALSE FALSE
 [88,]       FALSE FALSE FALSE
 [89,]       FALSE FALSE FALSE
 [90,]       FALSE FALSE FALSE
 [91,]       FALSE FALSE FALSE
 [92,]       FALSE FALSE FALSE
 [93,]       FALSE FALSE FALSE
 [94,]       FALSE FALSE FALSE
 [95,]       FALSE FALSE FALSE
 [96,]       FALSE FALSE FALSE
 [97,]       FALSE FALSE FALSE
 [98,]       FALSE FALSE FALSE
 [99,]       FALSE FALSE FALSE
[100,]       FALSE FALSE FALSE
[101,]       FALSE FALSE FALSE
[102,]       FALSE FALSE FALSE
[103,]       FALSE FALSE FALSE
[104,]       FALSE FALSE FALSE
[105,]       FALSE FALSE FALSE
[106,]       FALSE FALSE FALSE
[107,]       FALSE FALSE FALSE
[108,]       FALSE FALSE FALSE
[109,]       FALSE FALSE FALSE
[110,]       FALSE FALSE FALSE
[111,]       FALSE FALSE FALSE
[112,]       FALSE FALSE FALSE
[113,]       FALSE FALSE FALSE
[114,]       FALSE FALSE FALSE
[115,]       FALSE FALSE FALSE
[116,]       FALSE FALSE FALSE
[117,]       FALSE FALSE FALSE
[118,]       FALSE FALSE FALSE
[119,]       FALSE FALSE FALSE
[120,]       FALSE FALSE FALSE
[121,]       FALSE FALSE FALSE
[122,]       FALSE FALSE FALSE
[123,]       FALSE FALSE FALSE
[124,]       FALSE FALSE FALSE
[125,]       FALSE FALSE FALSE
[126,]       FALSE FALSE FALSE
[127,]       FALSE FALSE FALSE
[128,]       FALSE FALSE FALSE
[129,]       FALSE FALSE FALSE
[130,]       FALSE FALSE FALSE
[131,]       FALSE FALSE FALSE
[132,]       FALSE FALSE FALSE
[133,]       FALSE FALSE FALSE
[134,]       FALSE FALSE FALSE
[135,]       FALSE FALSE FALSE
[136,]       FALSE FALSE FALSE
[137,]       FALSE FALSE FALSE
[138,]       FALSE FALSE FALSE
[139,]       FALSE FALSE FALSE
[140,]       FALSE FALSE FALSE
[141,]       FALSE FALSE FALSE
[142,]       FALSE FALSE FALSE
[143,]       FALSE FALSE FALSE
[144,]       FALSE FALSE FALSE
[145,]       FALSE FALSE FALSE
[146,]       FALSE FALSE FALSE
[147,]       FALSE FALSE FALSE
[148,]       FALSE FALSE FALSE
[149,]       FALSE FALSE FALSE
[150,]       FALSE FALSE FALSE
[151,]       FALSE FALSE FALSE
[152,]       FALSE FALSE FALSE
[153,]       FALSE FALSE FALSE
[154,]       FALSE FALSE FALSE
[155,]       FALSE FALSE FALSE
[156,]       FALSE FALSE FALSE
[157,]       FALSE FALSE FALSE
[158,]       FALSE FALSE FALSE
[159,]       FALSE FALSE FALSE
[160,]       FALSE FALSE FALSE
[161,]       FALSE FALSE FALSE
[162,]       FALSE FALSE FALSE
[163,]       FALSE FALSE FALSE
[164,]       FALSE FALSE FALSE
[165,]       FALSE FALSE FALSE
[166,]       FALSE FALSE FALSE
[167,]       FALSE FALSE FALSE
[168,]       FALSE FALSE FALSE
[169,]       FALSE FALSE FALSE
[170,]       FALSE FALSE FALSE
[171,]       FALSE FALSE FALSE
[172,]       FALSE FALSE FALSE
[173,]       FALSE FALSE FALSE
[174,]       FALSE FALSE FALSE
[175,]       FALSE FALSE FALSE
[176,]       FALSE FALSE FALSE
[177,]       FALSE FALSE FALSE
[178,]       FALSE FALSE FALSE
[179,]       FALSE  TRUE FALSE
[180,]       FALSE FALSE FALSE
[181,]       FALSE FALSE FALSE
[182,]       FALSE FALSE FALSE
[183,]       FALSE FALSE FALSE
[184,]       FALSE FALSE FALSE
[185,]       FALSE FALSE FALSE
[186,]       FALSE FALSE FALSE
[187,]       FALSE FALSE FALSE
[188,]       FALSE FALSE FALSE
[189,]       FALSE FALSE FALSE
[190,]       FALSE FALSE FALSE
[191,]       FALSE FALSE FALSE
[192,]       FALSE FALSE FALSE
[193,]       FALSE FALSE FALSE
[194,]       FALSE FALSE FALSE
[195,]       FALSE FALSE FALSE
[196,]       FALSE FALSE FALSE
[197,]       FALSE FALSE FALSE
[198,]       FALSE FALSE FALSE
[199,]       FALSE FALSE FALSE
[200,]       FALSE FALSE FALSE
[201,]       FALSE FALSE FALSE
[202,]       FALSE FALSE FALSE
[203,]       FALSE FALSE FALSE
[204,]       FALSE FALSE FALSE
[205,]       FALSE FALSE FALSE
[206,]       FALSE FALSE FALSE
[207,]       FALSE FALSE FALSE
[208,]       FALSE FALSE FALSE
[209,]       FALSE FALSE FALSE
[210,]       FALSE FALSE FALSE
[211,]       FALSE FALSE FALSE
[212,]       FALSE FALSE FALSE
[213,]       FALSE FALSE FALSE
[214,]       FALSE FALSE FALSE
[215,]       FALSE FALSE FALSE
[216,]       FALSE FALSE FALSE
[217,]       FALSE FALSE FALSE
[218,]       FALSE FALSE FALSE
[219,]       FALSE  TRUE FALSE
[220,]       FALSE FALSE FALSE
[221,]       FALSE FALSE FALSE
[222,]       FALSE FALSE FALSE
[223,]       FALSE FALSE FALSE
[224,]       FALSE FALSE FALSE
[225,]       FALSE FALSE FALSE
[226,]       FALSE FALSE FALSE
[227,]       FALSE FALSE FALSE
[228,]       FALSE FALSE FALSE
[229,]       FALSE FALSE FALSE
[230,]       FALSE FALSE FALSE
[231,]       FALSE FALSE FALSE
[232,]       FALSE FALSE FALSE
[233,]       FALSE FALSE FALSE
[234,]       FALSE FALSE FALSE
[235,]       FALSE FALSE FALSE
[236,]       FALSE FALSE FALSE
[237,]       FALSE FALSE FALSE
[238,]       FALSE FALSE FALSE
[239,]       FALSE FALSE FALSE
[240,]       FALSE FALSE FALSE
[241,]       FALSE FALSE FALSE
[242,]       FALSE FALSE FALSE
[243,]       FALSE FALSE FALSE
[244,]       FALSE FALSE FALSE
[245,]       FALSE FALSE FALSE
[246,]       FALSE FALSE FALSE
[247,]       FALSE FALSE FALSE
[248,]       FALSE FALSE FALSE
[249,]       FALSE FALSE FALSE
[250,]       FALSE FALSE FALSE
[251,]       FALSE FALSE FALSE
[252,]       FALSE FALSE FALSE
[253,]       FALSE FALSE FALSE
[254,]       FALSE FALSE FALSE
[255,]       FALSE FALSE FALSE
[256,]       FALSE FALSE FALSE
[257,]       FALSE  TRUE FALSE
[258,]       FALSE FALSE FALSE
[259,]       FALSE FALSE FALSE
[260,]       FALSE FALSE FALSE
[261,]       FALSE FALSE FALSE
[262,]       FALSE FALSE FALSE
[263,]       FALSE FALSE FALSE
[264,]       FALSE FALSE FALSE
[265,]       FALSE FALSE FALSE
[266,]       FALSE FALSE FALSE
[267,]       FALSE FALSE FALSE
[268,]       FALSE FALSE FALSE
[269,]       FALSE  TRUE FALSE
[270,]       FALSE FALSE FALSE
[271,]       FALSE FALSE FALSE
[272,]        TRUE  TRUE FALSE
[273,]       FALSE FALSE FALSE
[274,]       FALSE FALSE FALSE
[275,]       FALSE FALSE FALSE
[276,]       FALSE FALSE FALSE
[277,]       FALSE FALSE FALSE
[278,]       FALSE FALSE FALSE
[279,]       FALSE FALSE FALSE
[280,]       FALSE FALSE FALSE
[281,]       FALSE FALSE FALSE
[282,]       FALSE FALSE FALSE
[283,]       FALSE FALSE FALSE
[284,]       FALSE FALSE FALSE
[285,]       FALSE FALSE FALSE
[286,]       FALSE FALSE FALSE
[287,]       FALSE FALSE FALSE
[288,]       FALSE FALSE FALSE
[289,]       FALSE FALSE FALSE
[290,]       FALSE FALSE FALSE
[291,]       FALSE FALSE FALSE
[292,]       FALSE FALSE FALSE
[293,]       FALSE FALSE FALSE
[294,]       FALSE FALSE FALSE
[295,]       FALSE FALSE FALSE
[296,]       FALSE FALSE FALSE
[297,]       FALSE FALSE FALSE
[298,]       FALSE FALSE FALSE
[299,]       FALSE FALSE FALSE
[300,]       FALSE FALSE FALSE
[301,]       FALSE FALSE FALSE
[302,]       FALSE FALSE FALSE
[303,]       FALSE FALSE FALSE
[304,]       FALSE FALSE FALSE
[305,]       FALSE FALSE FALSE
[306,]       FALSE FALSE FALSE
[307,]       FALSE FALSE FALSE
[308,]       FALSE FALSE FALSE
[309,]       FALSE FALSE FALSE
[310,]       FALSE FALSE FALSE
[311,]       FALSE FALSE FALSE
[312,]       FALSE FALSE FALSE
[313,]       FALSE FALSE FALSE
[314,]       FALSE FALSE FALSE
[315,]       FALSE FALSE FALSE
[316,]       FALSE FALSE FALSE
[317,]       FALSE FALSE FALSE
[318,]       FALSE FALSE FALSE
[319,]       FALSE FALSE FALSE
[320,]       FALSE FALSE FALSE
[321,]       FALSE FALSE FALSE
[322,]       FALSE FALSE FALSE
[323,]       FALSE FALSE FALSE
[324,]       FALSE FALSE FALSE
[325,]       FALSE FALSE FALSE
[326,]       FALSE FALSE FALSE
[327,]       FALSE FALSE FALSE
[328,]       FALSE FALSE FALSE
[329,]       FALSE FALSE FALSE
[330,]       FALSE FALSE FALSE
[331,]       FALSE FALSE FALSE
[332,]       FALSE FALSE FALSE
[333,]       FALSE FALSE FALSE
[334,]       FALSE FALSE FALSE
[335,]       FALSE FALSE FALSE
[336,]       FALSE FALSE FALSE
[337,]       FALSE FALSE FALSE
[338,]       FALSE FALSE FALSE
[339,]       FALSE FALSE FALSE
[340,]       FALSE FALSE FALSE
[341,]       FALSE FALSE FALSE
[342,]       FALSE FALSE FALSE
[343,]       FALSE FALSE FALSE
[344,]       FALSE FALSE FALSE

Response indicator

naniar::vis_miss(penguins)

Missingness rate

colSums(is.na(penguins))
          species            island    bill_length_mm     bill_depth_mm 
                0                 0                 2                 2 
flipper_length_mm       body_mass_g               sex              year 
                2                 2                11                 0 

Missingness rate

VIM::aggr(penguins, numbers = TRUE, prop = FALSE)

Missing data pattern

md.pattern(penguins)
    species island year bill_length_mm bill_depth_mm flipper_length_mm
333       1      1    1              1             1                 1
9         1      1    1              1             1                 1
2         1      1    1              0             0                 0
          0      0    0              2             2                 2
    body_mass_g sex   
333           1   1  0
9             1   0  1
2             0   0  5
              2  11 19

Missing data pattern

plot_pattern(penguins)

Imputation workflow

Imputation models

pred <- quickpred(penguins, mincor = 0.4)
meth <- make.method(penguins)
plot_pred(pred, method = meth, square = FALSE)

Correlation

plot_corr(penguins, square = FALSE)

Scatter plot

ggmice(penguins, aes(sex, flipper_length_mm)) +
  geom_point(size = 2)

Faceted distribution

ggmice(penguins, aes(flipper_length_mm)) +
  geom_histogram(fill = "white") +
  facet_grid(factor(
    is.na(sex), 
    levels = c(TRUE, FALSE), 
    labels = c("missing sex", "observed sex")
    ) ~ .)

Faceted scatter plot

ggmice(penguins, aes(sex, flipper_length_mm)) +
  geom_point(size = 2) +
  facet_wrap(~species)

Faceted distribution

ggmice(penguins, aes(flipper_length_mm)) +
  geom_histogram(fill = "white") +
  facet_grid(factor(
    is.na(sex), 
    levels = c(TRUE, FALSE), 
    labels = c("missing sex", "observed sex")
    ) ~ species)

Adjust imputation models

pred["sex", c("species", "flipper_length_mm")] <- 1
meth["sex"] <- "pmm"
plot_pred(pred, method = meth)

Impute

imp <- mice(
  penguins, 
  pred = pred, 
  method = meth, 
  m = 20,
  print = FALSE)
plot_trace(imp)

Stripplot

ggmice(imp, aes(x = .imp, y = flipper_length_mm)) +
  geom_point() +
  labs(x = "Imputation number")

Stripplot

ggmice(imp, aes(x = .imp, y = sex)) +
  geom_jitter(width = 0.05) +
  labs(x = "Imputation number")

Scatter plot

ggmice(imp, aes(sex, flipper_length_mm)) +
  geom_point() +
  facet_grid(~species)

Scatter plot with boxplot

ggmice(imp, aes(sex, flipper_length_mm)) +
  geom_point() +
  geom_boxplot() +
  facet_grid(~species)

Faceted distribution

ggmice(imp, aes(sex, group = .imp)) +
  geom_histogram(fill = "white", stat="count") +
  facet_grid(~ species)

Populations after imputation

Code
ggplot(complete(imp), aes(x = year, fill = sex, group = sex)) +
  geom_bar(position = "dodge") +
  facet_wrap( ~ species) +
  scale_x_continuous(breaks = c(2007, 2008, 2009)) +
  theme_classic()

Thank you!