design2colors | R Documentation |
Convert experiment design into categorical colors
design2colors(
x,
group_colnames = NULL,
lightness_colnames = NULL,
class_colnames = NULL,
ignore_colnames = NULL,
preset = "dichromat2",
phase = 1,
rotate_phase = -1,
class_pad = 2,
end_hue_pad = 0,
hue_offset = 0,
desat = c(0, 0.4),
dex = c(2, 5),
Crange = NULL,
Lrange = NULL,
color_sub = NULL,
color_sub_max = NULL,
na_color = "grey75",
shuffle_colors = FALSE,
force_consistent_colors = TRUE,
plot_type = c("table", "list", "none"),
return_type = c("list", "df", "vector"),
verbose = FALSE,
debug = c("none", "cardinality"),
...
)
x |
|
group_colnames |
|
lightness_colnames |
|
class_colnames |
|
preset |
|
phase , rotate_phase |
|
class_pad |
|
end_hue_pad |
|
desat |
|
dex |
|
Crange , Lrange |
|
color_sub |
|
color_sub_max |
|
na_color |
|
shuffle_colors |
|
force_consistent_colors |
|
plot_type |
|
return_type |
|
verbose |
|
debug |
|
... |
additional arguments are passed to downstream functions. |
The general goal is to assign categorical colors relevant to the experimental design of an analysis. The basic logic:
Assign categorical colors to experimental groups.
Shade these colors light-to-dark based upon secondary factors.
For step 1 above, optionally assign similar color hues by class.
When there are multiple factors in a design, the general guidance:
Define group_colnames
using the first two factors in the design.
Define class_colnames
using one of these two factors.
Values in group_colnames
will be assigned rainbow categorical colors,
with extra spacing between each class. Values in one class will be
assigned similar color hues, for example one class may be red/orange,
another class may be blue/purple.
Optionally choose another factor to use as lightness_colnames
.
When there are multiple unique values per group, they will be
shaded from light to dark within the group color hue.
It is sometimes helpful to create a column for class_colnames
,
for example when a series of treatments can be categorized
by the type of treatment (agonists, antagonists, inhibitors,
controls).
Franky, we tend to try a few combinations until the output seems
intuitive. Then we assign specific values from other columns
using color_sub
. Typically for numeric
columns we assign
a color to the colname, and for categorical
colors we assign
colors to values in the column.
Version 0.0.69.900 and higher: When the cardinality of group/class values is not 1-to-many, either the group/class assignments are switched in order to create 1-to-many cardinality, or a combination of the two vectors is used to create the appropriate cardinality.
By default, the unique rownames are used as if they were groups,
then colors are assigned using the same logic as usual. Any
other column whose values are 1-to-1 match with rownames will
inherit the same colors, otherwise character
and factor
columns will be assigned categorical colors, and numeric
columns will be assigned a color gradient.
At its simplest a set of groups can be assigned categorical colors.
colors should be visibly distinct from one another
colors should generally be distinct across forms of color-blindness
colors should be consistent across plots, figures, tables
Finally, colors may be pre-defined using a named vector of colors. These colors will be propagated to other entries in the table.
The light-to-dark gradient is intended for ordered sub-divisions, for example:
across time points in a time series
across treatment doses in an ordered series
across ordered measurements first-to-last
The group classification is intended to assign color hues for similar groups:
antagonists, agonists, untreated
treated, untreated
wildtype, mutant form 1, mutant form 2, etc.
For example, antagonists may be assigned colors blue-to-purple; agonists may be assigned colors red-to-orange; with a pronounced color hue "gap" between antagonists and agonists.
Finally, other annotations associated with samples are assigned categorical colors, visibly distinct from other color assignments.
For entries associated with only one design color, for example "Sample_ID", "Sample Name", "Lane Number", or "Well Number", they inherit the design color.
For entries associated with more than one design color, for example "Batch", "Date", or perhaps "Dose", they will be assigned a unique color.
additional annotations unique to design colors inherit the design colors
additional categorical colors should not duplicate existing colors
Assign "additional factors" to colors based upon class
Currently "additional factors" are only tested by class_group and class_group_lightness.
It could be useful to test versus class
alone (if supplied)
Goal would be to assign color hue using the mean color hue in the class.
Otherwise the class may be assigned a color inconsistent with the range of color hues.
Handle numeric columns by applying color gradient
A truly numeric column (not just integer index values) could
use circlize::colorRamp2()
to apply color gradient
output depends upon argument return_type
:
"list"
: returns a list
of colors defined by colnames(x)
,
suitable for use with ComplexHeatmap::HeatmapAnnotation()
for example.
"df"
: returns data.frame
of colors with same dimensions as the
input x
. Suitable for use with jamba::imageByColors()
for example.
"vector"
: returns character
vector of R colors, whose names represent
values in x
, where the values should be substituted with the color.
Suitable for use with ggplot2::color_manual(values=colors)
.
In all cases, the attributes()
of the returned object also includes
colors in the other two formats: "color_sub"
, "color_df"
, and
"color_list"
.
Other jam color functions:
assign_numeric_colors()
,
df_to_numcolors()
,
mean_hue()
,
print_color_list()
,
quick_complement_color()
df <- data.frame(
genotype=rep(c("WT", "GeneAKO", "GeneBKO"), c(4, 8, 8)),
treatment=rep(rep(c("control", "treated"), each=2), 5),
class=rep(c("WT", "KO"), c(4, 16)),
time=c(rep("early", 4),
rep(rep(c("early", "late"), each=4), 2)))
df$sample_group <- jamba::pasteByRow(df[,c("genotype", "treatment", "time")])
df$sample_name <- jamba::makeNames(df$sample_group);
df$age <- sample(40:80, size=nrow(df));
df
dfc <- design2colors(df,
group_colnames="genotype",
lightness_colnames="treatment",
class_colnames="class",
color_sub=c(age="dodgerblue"))
# same as above except assign colors to columns and some values
dfc <- design2colors(df,
group_colnames="sample_group",
lightness_colnames="treatment",
class_colnames="genotype",
class_pad=5,
preset="dichromat2",
color_sub=c(KO="darkorchid3",
treatment="navy",
time="dodgerblue"))
# same as above except assign specific group colors
dfc <- design2colors(df,
group_colnames="genotype",
lightness_colnames="treatment",
class_colnames="class",
preset="dichromat2",
color_sub=c(
WT="gold",
KO="purple3",
age="firebrick",
GeneAKO="firebrick3",
GeneBKO="dodgerblue",
treatment="navy",
time="darkorchid4"))
dfc2 <- design2colors(df,
group_colnames="genotype",
lightness_colnames=c("time", "treatment"),
class_colnames="class",
preset="dichromat2")
dfc3 <- design2colors(df,
group_colnames=c("genotype"),
lightness_colnames=c("time", "treatment"),
class_colnames="genotype",
preset="dichromat2")
df1 <- df;
df2 <- subset(df, time %in% "early");
df12 <- rbind(df1, df2);
dfc12 <- design2colors(df12,
group_colnames="genotype",
lightness_colnames=c("time", "treatment"),
class_colnames="class",
preset="dichromat",
color_sub=c(
treatment="steelblue",
time="dodgerblue"
))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.