r - Heatmap colors not working in plotly -
i'm trying print out grid of heatmaps using plotly r in shiny. want give them custom color scale, it's not behaving how want to. when use colors= option when building plotly chart, seems use distribution of values, rather zmin , zmax gave assign colors.
in example code below, can see give each plot same colorscale (colorscale) using colors= option. works how expect when have distributed set of data, in first, second, , fourth row of plots.
however, in third row, plots have skewed data, can see scales different else - have blue , red, skip on white in middle, instead having purple.
in actual code, causing big problem chart has lot of values around middle, extremes on either end - want values in middle appear white, show there no change, instead purple, making harder pick out important values (the extreme ones).
is there way force color assignment behave how want to?
thanks, cliff
server.r
if (!require("pacman")) install.packages("pacman") pacman::p_load(shiny,data.table,plotly) colorscale <- colorramp(c("darkblue","cornflowerblue","white","sandybrown","firebrick")) ncodenames <- c("a","b","c","d","e","f","g","h","i","j","k","l") means = c(rnorm(600,0,2.5),runif(600,-5,5),runif(130,-4,-3.9),runif(70,4.5,5),rnorm(150,-3),rnorm(50,4),rnorm(180,-2.5),runif(20,4.93,4.98),runif(300,-4,3),rnorm(300,3.5)) dt <- data.table(age=rep(rep(c(11:20),times=20),times=12),composite=rep(rep(c(81:100),each=10),times=12),mean=means,n_code=rep(ncodenames,each=200)) sub<-dt[n_code=="a"] shinyserver(function(input, output) { for(ncode in ncodenames){ local({ ncode = ncode output[[paste0("grid",ncode)]] <- renderplotly({ sub <- dt[n_code == ncode] p <- plot_ly(data=sub, x=~age, y=~composite, z=~mean, type="heatmap", zmin=-5,zmax=5, colors = colorscale, colorbar=list(thickness="15"))%>% layout(title=ncode,xaxis=list(type="category",tickvals=c(11,15,20)),yaxis=list(title="",ticks="")) }) }) } }) ui.r
if (!require("pacman")) install.packages("pacman") pacman::p_load(shiny, plotly) ncodenames <- c("a","b","c","d","e","f","g","h","i","j","k","l") shinyui(navbarpage( "e-n matrics: proportion of e-code resulting in each n-code", tabpanel("grid", lapply(c(1:4), function(i) fluidrow( lapply(c(1:3), function(j) column(4, plotlyoutput(paste0("grid",ncodenames[(i-1)*3+j])))) )) #fluidrow(column(4,plotlyoutput(paste0("grid",ncodenames[(1-1)*3+1]))),column(4,plotly)) ) ))
i encountered similar issues colorscale in r plotly heatmap. when data z argument has screwed distribution, few colors specified in colorscale used plotly.
i found work-around solution creating new variable according quantiles of original variable, , pass z argument. here r code general idea. need customize make work specific problem.
library(plotly) library(rcolorbrewer) # create dataframe z has skewed distribution set.seed(1) df = data.frame(x = rep(1:50, 20) , y = rep(1:20,each =50), z = rgamma(1000, 2, 0.5)) # check distribution of z plot_ly(data = df, x = ~z, type = "histogram")%>% layout(title = "histogram of z") # original heatmap # pass column z screwed distribution z argument plot_ly(data=df, x=~x, y=~y, z=~z, type="heatmap", colors = "spectral") %>% layout(title = "original heatmap") # data processing work # find unique quantiles of z quantiles = unique(quantile(df$z, seq(0,1,0.1))) # create dummy column z1 of discrete values using quantiles cut off # ideas arrage data subgroups of same size df$z1= cut(df$z, breaks = c(quantiles[1]-1,quantiles[-1]), right = true, labels = false) # check distribution of z1 plot_ly(data = df, x = ~z1, type = "histogram")%>% layout(title = "histogram of z1") # new heatmap # passes new column z1 z argument plot_ly(data=df, x=~x, y=~y, z=~z1, type="heatmap", # make sure hovering on displays original z text =~z, hoverinfo = "text", # use color palettes rcolorbrewer, # or customized colorscale colors = "spectral", # map label of colorbar quantiles colorbar=list(tickmode="array", tickvals = 1:(length(quantiles)-1), ticktext = round(quantiles,2)[-1], title = "z")) %>% layout(title = "new heat map") below original heatmap , new heatmap generated plotly. new heatmap uses more colors "spectral" palette differetiate between smaller values.
hope helps!
update on april 3,2017
i opened request on r plotly repository capability transform color scale.

Comments
Post a Comment