r - fix plotly color scale breaks -


i have created heatmap using plotly's r interface, so:

df <- data.frame(date = rep(seq(ymd('2016-09-01'), ymd('2016-09-10'), by='day'), each=2),                  period = rep(c('lunch', 'dinner'), times=10),                  diff=rnorm(20))  df %>%   plot_ly(x = date,           y = period,           z = abs(diff),           type = "heatmap",           hoverinfo='none',           colors = rev(rcolorbrewer::brewer.pal(5, "rdylgn")),           showscale = f) %>%   add_trace(x = date,             y = period,             text = paste0(round(diff*100), '%'),             mode='text',             hoverinfo='none') 

which produces this:

heatmap

is there way can fix breaks of color scale? midpoint of color scale @ 20% , value exceeds 50% colored red @ end of scale.

well, heatmap supposed have colors vary value of element, force each element 1 of 5 groups based on whatever criteria want , assign 5 colors accordingly. then, grouping passed z. z should number though, not factor, can convert grouping integer:

# create 5 levels or groups diff variable # choose breaks desired difflevel <- cut(abs(df$diff),                  breaks = c(0, 0.0625, .125, .3, .5, inf))  df %>%   plot_ly(x = date,           y = period,           z = as.integer(difflevel),           type = "heatmap",           hoverinfo='none',           colors = rev(rcolorbrewer::brewer.pal(5, "rdylgn")),           showscale = f) %>%   add_trace(x = date,             y = period,             text = paste0(round(diff*100), '%'),             mode='text',             hoverinfo='none') 

another approach set zmax value 0.5 (and zauto = false). won't set center of colorscale .2, works well, , establish cutoff of 0.5, above red. allows pass in abs(diff) directly:

df %>%   plot_ly(x = date,           y = period,           z = abs(diff),           zauto = false,  # set false!           zmax = 0.5,                         type = "heatmap",           hoverinfo='none',           colors = rev(rcolorbrewer::brewer.pal(5, "rdylgn")),           showscale = f) %>%   add_trace(x = date,             y = period,             text = paste0(round(diff*100), '%'),             mode='text',             hoverinfo='none') 

one final method work custom colorscale. found tricky colorscale argument in righ format. there may better way this, wasn't able come elegant.

custompalette <- colorramppalette(c("red", "yellow", "green"))(61)  # put 30 values between 0 , .2 , and 30 between .2 , 1. # , so, palette has 61 colors   customcolors <- setnames(   data.frame(breaks =  c(seq(0, .2, length.out = 31), seq(.21, 1, length.out = 30)),              colors = as.character(               apply(col2rgb(rev(custompalette)), 2, function(ii)                 paste("rgb(", paste(ii, collapse = ","),")", sep = ""))),              stringsasfactors = false),   null) # not perfect, seems work decently  # initialize list collist <- list()  collist[[dim(customcolors)[1]]] <- 0  # format custom color list for(i in seq_len(dim(customcolors)[1])) {   collist[[i]] <- c(customcolors[i, 1], customcolors[i, 2]) }  # set zmax = 0.5 , zauto = false df %>%   plot_ly(x = date,           y = period,           z = abs(diff),           zauto = false,           zmax = 0.5,           type = "heatmap",           hoverinfo='none',           colorscale =  collist,           showscale = f) %>%   add_trace(x = date,             y = period,             text = paste0(round(diff*100), '%'),             mode='text',             hoverinfo='none') 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -