Skip to content

Instantly share code, notes, and snippets.

@nathania
Last active March 14, 2019 02:32
Show Gist options
  • Save nathania/29988e8f986c788b555264fca8002495 to your computer and use it in GitHub Desktop.
Save nathania/29988e8f986c788b555264fca8002495 to your computer and use it in GitHub Desktop.
RStudio Community reply: How to apply a plotting function to a list of dataframes *and* a character vector
# https://community.rstudio.com/t/how-to-apply-a-plotting-function-to-a-list-of-dataframes-and-a-character-vector/26033
library(tidyverse)
library(scales)
library(naniar)
# original viz ------------------------------------------------------------
cols <- c('Good' = '#1a9641',
'OK' = '#a6d96a',
'Bad' = '#fdae61',
'Remove' = '#d7191c')
dummy_groups <- map2(names(cols), c(10, 3, 1, 1), ~ rep(.x, .y)) %>% unlist()
missingness_by_vore <- msleep %>%
gather(key ='feature', value = 'value', -vore) %>%
add_count(vore, feature,
wt = is.na(value),
name = 'num_missing') %>%
add_count(vore, feature,
name = 'num_rows') %>%
mutate(pct_missing = num_missing / num_rows) %>%
distinct(vore,
feature,
num_missing,
pct_missing) %>%
mutate(Group = map_chr(ntile(pct_missing, length(dummy_groups)),
~ pluck(dummy_groups, .x)))
ggplot(missingness_by_vore,
aes(x = feature,
y = num_missing,
fill = Group)) +
geom_col() +
geom_text(aes(label = scales::percent(pct_missing) %>%
modify_if(. == '0.0%', ~ '')),
nudge_y = 2, size = 2) +
scale_fill_manual(values = cols,
breaks = names(cols)) +
coord_flip() +
labs(x = 'Features',
y = 'Number of missing rows') +
facet_wrap(vars(vore)) +
theme_minimal()
# naniar -----------------------------------------------------------------
library(tidyverse)
library(scales)
library(naniar)
gg_miss_var(msleep, vore, show_pct = TRUE) + ylim(0, 100)
msleep2 <- group_nest(msleep, vore) %>%
mutate(missingness = map2(data, vore,
~ vis_miss(.x, cluster = TRUE) + labs(title = .y))) %>%
mutate(miss_upset = map(data,
gg_miss_upset))
msleep2$missingness[[2]]
msleep2$miss_upset[[2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment