Skip to content

Commit

Permalink
added a palette argument to visdat
Browse files Browse the repository at this point in the history
I’ve created a draft template for the colours to as described in issue
#26 .
  • Loading branch information
njtierney committed May 31, 2016
1 parent 9c5d04b commit b24cecc
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,6 +1,6 @@
Package: visdat
Title: preliminary visualisation of data
Version: 0.0.4.9100
Version: 0.0.4.9500
Authors@R: person("Nicholas", "Tierney", email = "nicholas.tierney@gmail.com", role = c("aut", "cre"))
Description: visdat makes it easy to visualise your whole dataset so that you
can quickly identify problems visually.
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
@@ -1,6 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(fingerprint)
export(guess_type)
export(vis_dat)
export(vis_dat_ly)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
@@ -1,3 +1,11 @@
# visdat 0.0.4.9500 (2016/05/31)
=========================

## NEW FEATURE (under development)

- `vis_dat` gains a "palette" argument in line with [issue 26](https://github.com/njtierney/visdat/issues/26), drawn from http://colorbrewer2.org/, there are currently three arguments, "default", "qual", and "cb_safe". "default" provides the ggplot defaults, "qual" uses some colour blind **unfriendly** colours, and "cb_safe" provides some colours friendly for colour blindness.


# visdat 0.0.4.9400 (2016/05/30)
=========================

Expand Down
71 changes: 70 additions & 1 deletion R/vis_dat.R
Expand Up @@ -6,10 +6,14 @@
#'
#' @param sort_type logical TRUE/FALSE. When TRUE (default), it sorts by the type in the column to make it easier to see what is in the data
#'
#' @param palette character "default", "qual" or "cb_safe". "default" (the default) provides the stock ggplot scale for separating the colours. "qual" uses an experimental qualitative colour scheme for providing distinct colours for each Type. "cb_safe" is a set of colours that are appropriate for those with colourblindness. "qual" and "cb_safe" are drawn from http://colorbrewer2.org/.
#'
#' @export
vis_dat <- function(x,
sort_type = TRUE) {
sort_type = TRUE,
palette = "default") {

# x = airquality
if (sort_type == TRUE) {

# arrange by the columns with the highest missingness
Expand Down Expand Up @@ -39,6 +43,7 @@ vis_dat <- function(x,
d$value <- tidyr::gather_(x, "variables", "value", names(x))$value

# do the plotting
vis_dat_plot <-
ggplot(data = d,
aes_string(x = "variables",
y = "rows",
Expand All @@ -53,4 +58,68 @@ vis_dat <- function(x,
y = "Observations") +
scale_x_discrete(limits = type_order_index) +
guides(fill = guide_legend(title = "Type"))

if (palette == "qual"){

# qualitative, 6 colours
vis_dat_palette <- c("#e41a1c", # red
"#ffff33", # yellow
"#ff7f00", # Orange
"#377eb8", # blue
"#4daf4a", # Green
"#984ea3") # Purple

vis_dat_plot +
scale_fill_manual(limits = c("character",
"date",
"factor",
"integer",
"logical",
"numeric"),
breaks = c("character", # red
"date", # orange
"factor", # yellow
"integer", # light blue
"logical", # mid blue
"numeric"), # dark blue
values = vis_dat_palette,
na.value = "grey")
# continue reading here:
# http://docs.ggplot2.org/current/discrete_scale.html

} else if (palette == "cb_safe"){

# # diverging, 6 colours, colour-blind safe
vis_dat_palette <- c('#d73027', # red
'#fc8d59', # orange
'#fee090', # yellow
'#e0f3f8', # light blue
'#91bfdb', # mid blue
'#4575b4') # dark blue

vis_dat_plot +
scale_fill_manual(limits = c("character",
"date",
"factor",
"integer",
"logical",
"numeric"),
breaks = c("character", # red
"date", # orange
"factor", # yellow
"integer", # light blue
"logical", # mid blue
"numeric"), # dark blue
values = vis_dat_palette,
na.value = "grey")
# continue reading here:
# http://docs.ggplot2.org/current/discrete_scale.html

} else if (palette == "default"){

# regular palette
vis_dat_plot

}

}
4 changes: 3 additions & 1 deletion man/vis_dat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b24cecc

Please sign in to comment.