热图颜色在绘图中不起作用 [英] Heatmap colors not working in plotly

查看:104
本文介绍了热图颜色在绘图中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Shiny中的R来绘制热图网格.我想给他们一个自定义的色阶,但是它不符合我的期望.当我在构建绘图图表时使用 colors = 选项时,似乎使用的是值的分布,而不是我赋予它的zmin和zmax来分配颜色.

I'm trying to print out a grid of heatmaps using plotly for R in Shiny. I want to give them a custom color scale, but it's not behaving how I want it to. When I use the colors= option when building my plotly chart, it seems to use the distribution of values, rather than the zmin and zmax that I gave it to assign colors.

在下面的示例代码中,您可以看到我使用colors =选项为每个绘图赋予了相同的色标(colorScale).当我拥有一组分布良好的数据时,如在第一排,第二排和第四排中一样,这可以达到预期效果.

In my example code below, you can see that I give each plot the same colorscale (colorScale) using the colors= option. This works how I expect when I have a well distributed set of data, as in the first, second, and fourth row of plots.

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

However, in the third row, where the plots have very skewed data, you can see that the scales look different than everything else - they have the blue and red, but skip over the white in the middle, instead having purple.

在我的实际代码中,这给图表带来了一个大问题,该图表在中间有很多值,并且两端都有些极端-我希望中间的那些值显示为白色,以表明有没什么变化,但是它们是紫色的,这使得挑选重要的值(极端的值)变得更加困难.

In my actual code, this is causing a big problem for a chart that has a lot of values around the middle, with some extremes on either end - I want those values in the middle to appear white, to show that there was no change, but instead they are purple, making it harder to pick out the important values (the extreme ones).

有没有一种方法可以强制颜色分配按照我希望的方式运行?

Is there a way to force the color assignment to behave how I want it to?

谢谢, 悬崖

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))
  )
  ))

推荐答案

我在R plotly热图中的色标遇到了类似的问题.当z自变量的数据具有固定分布时,仅会使用色标中指定的几种颜色.

I encountered similar issues with the colorscale in R plotly heatmap. When the data for the z argument has screwed distribution, only a few colors specified in the colorscale are used by plotly.

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

I found a work-around solution by creating a new variable according to the quantiles of the original variable, and pass it to the z argument. Here is the R code for the general idea. You will need to customize it to make it work for a specific problem.

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")

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

Below are the original heatmap and the new heatmap generated by plotly. The new heatmap uses more colors from "Spectral" palette to differetiate between smaller values.

希望这会有所帮助!

2017年4月3日更新

Update on April 3,2017

我在R plotly存储库上打开了一个请求,以请求转换色标.

I opened a request on the R plotly repository for capability to transform the color scale.

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

这篇关于热图颜色在绘图中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆