2016-11-10 58 views
3

我試圖用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="")) 
    }) 
    }) 
} 
}) 

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

回答

3

我遇到過類似的問題,在R上的colorscale plotly熱圖。當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") 

下面是原來的熱圖和plotly產生的新的熱圖。新的熱圖使用「光譜」調色板中的更多顏色區分較小的值。

enter image description here

希望這有助於!四月3,2017


更新我開在R的請求plotly存儲庫的能力來改造色標。

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