使用绘图创建热图;更改颜色

问题描述 投票:1回答:2

我是R和Plotly的新手,为此我一直发疯。我敢肯定有一个我不了解的简单解决方案,但是我找不到能够遵循的任何解决方案。

我要做的就是将此热图的颜色更改为RdYlGn。据我所知,我要做的就是创建一个调色板并更改色阶。但是我每次的结果都是这样:

enter image description here

下面的我的代码。

# Load libraries
library(plotly)
library(RColorBrewer)
# Load Dataframe
df <- data.frame(Overall_Assessment = c(3,2,2), 
    Budget_Equals_Estimate = c(3,2,4), 
    Risk_Assessment_Cost_Schedule_Technical = c(3,"",3),
    Crosschecks = c(5,1,1),
    Cost_Methodology = c(3,2,2),
    Schedule_Baseline = c(2,2,2),
    Engineering_Technical_Baseline = c(3,3,3),
    Requirements_Definition = c(5,5,5))
# Create Matrix
m <- matrix(df, nrow = 8, ncol = 3)

# plot matrix
confidencevalue <- c(1,2,3,4,5)

pal <- brewer.pal(5,"RdYlGn")

fig <- plot_ly(
    x = c("EMD", "O&S", "P&D"), 
    y = c("Overall Assessment", "Budget Equals Estimate", "Risk Assessment (Cost Schedule Technical)",
    "Crosschecks", "Cost & Methodology", "Schedule Baseline", "Engineering Technical Baseline", "Requirements Definition"),
    z = m, 
    colorscale = pal,
    type = "heatmap") %>% 
    layout(xaxis = list(side = "top"), 
    yaxis = list(categoryorder = "trace", title = "Confidence Enablers"), 
    margin = list(l=10, r=10, b=10, t=10))
fig
r plotly heatmap
2个回答
0
投票

怎么样?我在酿造机的调色板上徘徊,发现无论我尝试什么brewer.pal(),图表上的颜色始终是相同的错误颜色。

查看?plot_ly帮助文本中的“颜色”,它说“ colorbrewer2.org调色板名称[十六进制颜色的向量 ...”,所以我尝试了:

fig <- plot_ly(
    x = c("EMD", "O&S", "P&D"), 
    y = c("Overall Assessment", "Budget Equals Estimate", "Risk Assessment (Cost Schedule Technical)",
    "Crosschecks", "Cost & Methodology", "Schedule Baseline", "Engineering Technical Baseline", "Requirements Definition"),
    z = m, 
    colorscale = "YlOrRd",
    type = "heatmap") %>% 
    layout(xaxis = list(side = "top"), 
    yaxis = list(categoryorder = "trace", title = "Confidence Enablers"), 
    margin = list(l=10, r=10, b=10, t=10))

产生:enter image description here


0
投票

代替colorscale,可以将colors参数与colorRamp一起使用:

pal <- brewer.pal(5,"RdYlGn")

fig <- plot_ly(
    x = c("EMD", "O&S", "P&D"), 
    y = c("Overall Assessment", "Budget Equals Estimate", "Risk Assessment (Cost Schedule Technical)",
    "Crosschecks", "Cost & Methodology", "Schedule Baseline", "Engineering Technical Baseline", "Requirements Definition"),
    z = m, 
    colors = colorRamp(pal),
    type = "heatmap") %>% 
    layout(xaxis = list(side = "top"), 
    yaxis = list(categoryorder = "trace", title = "Confidence Enablers"), 
    margin = list(l=10, r=10, b=10, t=10))

reference

© www.soinside.com 2019 - 2024. All rights reserved.