【问题标题】:Plot_ly contour plot in R common legend range colors not showing up correctlyR常见图例范围颜色中的Plot_ly等高线图未正确显示
【发布时间】:2021-12-02 09:51:47
【问题描述】:

我用 5 个子图和常见图例创建了这个等高线图。如图所示,图例的最小和最大颜色仅基于第一个子图,并且在所有 5 个图上都不正确。

有没有办法解决这个问题?我猜这是某个地方的小修复。 在我正在使用的代码下方:

library(plotly)

VSL <- c(79000, 161000, 327000)
SCC <- c(35, 50, 100)

#Baseline
fig1 <- plot_ly(x=~VSL,y=~SCC,
  z = matrix(c(8.8,11.5,20.4,11.4,14.1,23.0,16.8,19.5,28.4), nrow = 3, ncol = 3), 
  type = "contour",coloraxis = 'coloraxis', contours = list(showlabels = TRUE,labelfont = list(size = 20, color = 'black')))

#EV Currentgrid
fig2 <- plot_ly(x=~VSL,y=~SCC, 
        z = matrix(c(8.2,10.6,18.6,11.0,13.4,21.3,16.7,19.1,27.0), nrow = 3, ncol = 3), 
        type = "contour",coloraxis = 'coloraxis', contours = list(showlabels = TRUE,labelfont = list(size = 20, color = 'black')))

#,contours = list(start = 6,end = 32,size = 2)

#EV Coal
fig3 <- plot_ly(x=~VSL,y=~SCC, 
                z = matrix(c(9.6,12.3,21.5,12.9,15.6,24.8,19.6,22.3,31.5), nrow = 3, ncol = 3), 
                type = "contour",coloraxis = 'coloraxis', contours = list(showlabels = TRUE,labelfont = list(size = 20, color = 'black')))

#EV NG
fig4 <- plot_ly(x=~VSL,y=~SCC, 
                z = matrix(c(7.4,9.7,17.1,9.6,11.9,19.4,14.2,16.4,23.9), nrow = 3, ncol = 3), 
                type = "contour",coloraxis = 'coloraxis', contours = list(showlabels = TRUE,labelfont = list(size = 20, color = 'black')))

#EV WWS
fig5 <- plot_ly(x=~VSL,y=~SCC, 
                z = matrix(c(6.4,8.3,14.5,8.5,10.4,16.6,12.8,14.6,20.9), nrow = 3, ncol = 3), 
                type = "contour",coloraxis = 'coloraxis', contours = list(showlabels = TRUE,labelfont = list(size = 20, color = 'black')))


fig <- subplot(fig1,fig2,fig3,fig4,fig5, shareY = TRUE)

annotations = list( 
  list(
    x = 0.08,  
    y = 1,
    text = "Baseline",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),  
  list(
    x = 0.3,  
    y = 1,  
    text = "EV Current grid",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),  
  list(
    x = 0.5,  
    y = 1,
    text = "EV Coal",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),
  list(
    x = 0.7,  
    y = 1,
    text = "EV NG",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),
  list(
    x = 0.9, 
    y = 1,
    text = "EV WWS",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),
  list(
    x = 1.05, 
    y = 1,
    text = "Total damages",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),
  list(
    x = 1.05, 
    y = 0.97,
    text = "(billion$)",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),
  list(
    x = 0.5, 
    y = -0.157,
    text = "VSL",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE
    ))

fig <- fig %>% layout(coloraxis=list(colorscale='RdBu'),annotations = annotations)
fig

【问题讨论】:

    标签: r legend contour subplot r-plotly


    【解决方案1】:

    您需要手动定义轮廓范围的start end end 值。您的示例中有很多尴尬的代码重复,因此我创建了一个更短且更整洁的示例。

    # This is your z-data; I recommend storing this as a `list` 
    # so you can loop over it using `lapply` or `purrr::map`.
    z_data <- list(
        fig1 = matrix(
            c(8.8,11.5,20.4,11.4,14.1,23.0,16.8,19.5,28.4), 
            nrow = 3, ncol = 3),
        fig2 = matrix(
            0.1 * c(8.2,10.6,18.6,11.0,13.4,21.3,16.7,19.1,27.0), 
            nrow = 3, ncol = 3))
    
    # Define the contour properties; `start`/`end` define the min/max
    # values of the shown contour range
    contours <- list(
        showlabels = TRUE,
        start = floor(min(unlist(z_data))),
        end = ceiling(max(unlist(z_data))),
        labelfont = list(size = 20, color = "black"))
    
    # Create a `list` of `plot_ly` objects
    lst <- lapply(z_data, function(z) 
        plot_ly(
            x = ~VSL,y = ~SCC, z = z,
            type = "contour",coloraxis = "coloraxis", contours = contours))
    
    # `subplot` accepts a `list` of `plot_ly` objects 
    subplot(lst, shareY = TRUE)
    

    【讨论】:

    • 谢谢莫里茨。成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2020-08-08
    • 2021-12-27
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    相关资源
    最近更新 更多