热图颜色在图中不起作用

Cli*_*iff 5 r heatmap shiny plotly

我正在尝试使用图表中的R in Shiny打印出一组热图.我想给他们一个自定义的色标,但它不是我想要它的表现.当我在构建我的绘图图表时使用colors =选项时,它似乎使用值的分布,而不是我给它分配颜色的zmin和zmax.

在下面的示例代码中,您可以看到我使用colors =选项为每个绘图提供相同的色阶(colorScale).当我有一个分布良好的数据集时,这就像我在第一,第二和第四行图中所期望的那样.

但是,在第三行中,图表的数据非常偏斜,您可以看到比例看起来与其他一切不同 - 它们有蓝色和红色,但跳过中间的白色,而不是紫色.

在我的实际代码中,这对于中间有很多值的图表造成了很大的问题,两端都有一些极端 - 我希望中间的那些值显示为白色,表明没有变化,但相反,它们是紫色的,这使得更难以挑选出重要的价值观(极端的价值观).

有没有办法强制颜色分配表现我想要的方式?

谢谢,克利夫

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=""))
    })
  })
}
})
Run Code Online (Sandbox Code Playgroud)

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))
  )
  ))
Run Code Online (Sandbox Code Playgroud)

Ali*_* Wu 5

我在R plotly heatmap中遇到了与色阶相似的问题.当z参数的数据具有螺旋分布时,图表中仅使用颜色标度中指定的几种颜色.

我通过根据原始变量的分位数创建一个新变量找到了一个解决方案,并将其传递给z参数.这是一般想法的R代码.您需要对其进行自定义以使其适用于特定问题.

library(plotly)
library(RColorBrewer)

# create a dataframe where z has a 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 the column z with screwed distribution to z argument
plot_ly(data=df, x=~x, y=~y, z=~z, type="heatmap",  
        colors = "Spectral") %>% 
    layout(title = "original heatmap")

# some data processing work 

# find unique quantiles of z 
quantiles = unique(quantile(df$z, seq(0,1,0.1)))

# create a dummy column z1 of discrete values using the quantiles as cut off
# the ideas is to arrage the data to subgroups of roughly the 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 the new column z1 to z argument 
plot_ly(data=df, x=~x, y=~y, z=~z1, type="heatmap",  
        # make sure hovering over displays original z
        text =~z, hoverinfo = "text", 
        # use the color palettes from RColorBrewer, 
        # or your customized colorscale
        colors = "Spectral",  
        # map the label of the colorbar back to the quantiles
        colorbar=list(tickmode="array", tickvals = 1:(length(quantiles)-1), ticktext = round(quantiles,2)[-1], title = "z")) %>% 
layout(title = "new heat map")
Run Code Online (Sandbox Code Playgroud)

下面是原始热图和由plotly生成的新热图.新的热图使用"光谱"调色板中的更多颜色来区分较小的值.

在此输入图像描述

希望这可以帮助!


2017年4月3日更新

我在R plotly存储库上打开了一个请求,用于转换色阶的能力.

https://github.com/ropensci/plotly/issues/920