vallenderlab / MicrobiomeR

A comprehensive and customizable R package for microbiome analysis.
https://vallenderlab.github.io/MicrobiomeR/
MIT License
20 stars 4 forks source link

Standardize colors across figures #35

Closed sdhutchins closed 5 years ago

sdhutchins commented 6 years ago

Use paletter to generate palettes from a figure (particularly heat trees) to ensure that colors are similar across plots.

Install paletter

devtools::install_github("AndreaCirilloAC/paletter")

The file should be in jpeg format :exclamation:

library(paletter)
colours_vector <- create_palette(image_path ="output/Class_heat_tree.jpg",
                                 number_of_colors = 15,
                                 type_of_variable = "categorical")

:exclamation: Update

paletter wouldn't run the above code on my machine. Everything froze.

sdhutchins commented 6 years ago

It could also be worthwhile to take a look at the ggsci package. It has some good palettes.

grabear commented 6 years ago

https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html This one is supposed to be really good for colorblindness too.

grabear commented 6 years ago

Here's my R code to create the color palette we looked at:

> x <- colorRampPalette(unique(c(rev(viridis::viridis(800)[300:800]), viridis::magma(800))))
> pal <- x(20)
> pie(rep(1, length(pal)), col=pal)

image

sdhutchins commented 6 years ago

Awesome.

sdhutchins commented 5 years ago

This is what I added in to utils.R @grabear

https://github.com/vallenderlab/stress-microbiome-analysis/blob/b20282e5ac1cd815697263afca6e5cc9aa7f8298/code/utils.R#L22-L29

grabear commented 5 years ago

Ideas

grabear commented 5 years ago

Here's what I'm adding to the MicrobiomeR package:

#' @title Viridis Palette Function
#' @description A function that returns a color palette function based off of the veridis package.
#' @param viridis_number The total number of colors used to generate the viridis palette. Default: 800
#' @param viridis_range Tne range of colors in the viridis palette to use. Default: c(300, 800)
#' @param magma_number The total number of colors used to generate the magma palette. Default: 500
#' @param magma_range The range of colors in the magma palette to use. Default: c(0, 500)
#' @return The output of this function is another function, which takes a number to generate a color
#' palette as a character vector.
#' @details The primary purpose of this function is to return a palette-function for generating virdis style
#' color palettes.  By taking the viridis::viridis() and the viridis::magma() colors, and manipulating
#' them, this function can help create a unique set of colors that you can distinguish on a busy plot.
#' The hopes of this function is to help improve plots that use more than 20 colors.  Use the provided
#' example to view the color palette.
#' @examples
#' \dontrun{
#' if(interactive()){
#'  # Use the default values
#'  > pal_func <- viridis_palette_func()
#'
#'  # Get a palette with 20 colors
#'  > pal <- pal_func(20)
#'
#'  # Make a pie plot of the colros.
#'  > pie(rep(1, length(pal)), col=pal)
#'  }
#' }
#' @export
#' @family Color Palettes
#' @rdname virdis_palette_func
#' @seealso
#'  \code{\link[viridis]{reexports}}
#'  \code{\link[MicrobiomeR]{get_color_palette}}
#' @importFrom viridis viridis magma
virdis_palette_func <- function(viridis_number=800, viridis_range=c(300,800), magma_number=500, magma_range=c(0, 500)) {
  v_min = viridis_range[1]
  v_max = viridis_range[2]
  m_min = magma_range[1]
  m_max = magma_range[2]
  colorRampPalette(
    unique(c(
      rev(viridis::viridis(viridis_number)[v_min:v_max]), viridis::magma(magma_number)[m_min:m_max])
      )
    )
}

get_color_palette <- function(pal_func=virdis_palette_func, color_no=20, display=TRUE, ...) {
  func_params <- list(...)
  pal_func <- pal_func(func_params)
  pal <- pal_func(color_no)
  if (display){
    pie(rep(1, length(pal)), col=pal)
  }
  return(pal)
}
sdhutchins commented 5 years ago

Me likey @grabear

sdhutchins commented 5 years ago

I'm excited to test it out. Haven't done so yet.

grabear commented 5 years ago

https://github.com/thomasp85/scico Is looking really good now too.

scico provides 17 different palettes, all of which are perceptually uniform and colourblind safe. An overview can be had with the scico_palette_show() function:

sdhutchins commented 5 years ago

Here's a sample of the viridis function used on a stacked bar chart:

image

grabear commented 5 years ago

I like it. What do you think? @sdhutchins

grabear commented 5 years ago

@sdhutchins Did you do that with your new workflow?

sdhutchins commented 5 years ago

For whatever reason, my GitHub notifications are being odd...

But yes! that's with the new workflow!!! Only those 12 phyla!!!

sdhutchins commented 5 years ago

@sdhutchins Did you do that with your new workflow?

@grabear

I thought it would look slightly better lol In general, I love the colors, but something is throwing it off for me. I think stacked bar charts just make it harder to pick palettes.

sdhutchins commented 5 years ago

image

Currently playing around

sdhutchins commented 5 years ago

image

sdhutchins commented 5 years ago

image

sdhutchins commented 5 years ago

image

sdhutchins commented 5 years ago

Just wrote this function in a newly created palettes.R.

# Import Libraries
source("code/global.R")
suppressMessages(library(viridis))
suppressMessages(library(scico))

get_palettes <- function() {
  viridis_palettes <- list(
    eight = colorRampPalette(unique(c(rev(viridis::viridis(800)[300:800]), viridis::magma(800))))(8),
    fifteen = colorRampPalette(unique(c(rev(viridis::viridis(800)[300:800]), viridis::magma(800))))(15),
    twentyfive = colorRampPalette(unique(c(rev(viridis::viridis(800)[300:800]), viridis::magma(800))))(25)
  )
  scico_palettes <- list(eight = scico::scico(8, palette = 'batlow'),
                         fifteen = scico::scico(15, palette = 'batlow'),
                         twentyfive = scico::scico(25, palette = 'batlow'))
  palettes <- list(viridis = viridis_palettes, scico = scico_palettes)
  return(palettes)
}

# Get a list of a list of a list of palettes by package -----
palettes <- get_palettes()

It'll return the palettes list to the global environment for us to use.

Example

> palettes$viridis$eight
[1] "#FDE725" "#8DD645" "#28AE7F" "#100B2E" "#5D177F" "#C63C73" "#FD9768" "#FCFDBF"

It makes it easier to just use this with scale_fill_manual(values = palettes$viridis$eight)

sdhutchins commented 5 years ago

Kind of interesting..this is the scico palette "batlow"

image

sdhutchins commented 5 years ago

I still like spectral from the RColorBrewer package

image

sdhutchins commented 5 years ago

Using the dichromat package

dichromat::colorschemes$Categorical.12

image

sdhutchins commented 5 years ago

I kind of like this one...

image

It was generated via ggthemes

pal <- ggthemes::tableau_color_pal(palette = "Classic Cyclic", type = "regular")(12)
grabear commented 5 years ago

Did you decide on one?

sdhutchins commented 5 years ago

Did you decide on one?

...No...

:cry: