【问题标题】:How to reverse the direction of colorbar in R plotly?如何在 R 中反转颜色条的方向?
【发布时间】:2021-05-10 12:43:25
【问题描述】:

我使用plotly 的R 接口用颜色条进行绘制,它将连续变量映射到颜色渐变。通过 debault,plotly 将最小值放在颜色栏的底部,将最大值放在颜色栏的顶部。如图所示。

现在我想反转整个颜色条,最小值在顶部。但是,我还没有找到(或者我错过了)实现我的目标的选项。有什么办法可以反转颜色条吗?


感谢@vestland 提供以下示例代码:

library(plotly)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>",
                           "Fruits", total.fruits, "Veggies", total.veggies,
                           "<br>", "Wheat", wheat, "Corn", corn))
# give state boundaries a white border
l <- list(color = toRGB("white"), width = 2)
# specify some map projection/options
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)

fig <- plot_geo(df, locationmode = 'USA-states', reversescale = T)
fig <- fig %>% add_trace(
    z = ~total.exports, text = ~hover, locations = ~code,
    color = ~total.exports, colors = 'Purples'
  )
fig <- fig %>% colorbar(title = "Millions USD")
fig <- fig %>% layout(
    title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
    geo = g
  )

fig

参数reversescale 反转值和颜色之间的映射。但是,我的目标是反转整个颜色条(不更改映射)。在我的例子中,将值 1 放在颜色条的顶部,将最大值放在底部更合理,因为这些值是某个指标的排名。


ggplot 的类似问题。我刚刚发现我也无法反转ggplot 中的颜色条。

【问题讨论】:

    标签: r ggplot2 plotly colorbar r-plotly


    【解决方案1】:

    看起来您正在使用plot_geo()。在这种情况下,只需像这样包含reversescale = T

    plot_geo(df, locationmode = 'USA-states', reversescale = T)`
    

    这对于使用颜色条的大多数其他功能也应该有效。

    情节 1:reversescale = F 或未指定

    情节2:reversescale = T

    完整代码:

    library(plotly)
    df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
    df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>",
                               "Fruits", total.fruits, "Veggies", total.veggies,
                               "<br>", "Wheat", wheat, "Corn", corn))
    # give state boundaries a white border
    l <- list(color = toRGB("white"), width = 2)
    # specify some map projection/options
    g <- list(
      scope = 'usa',
      projection = list(type = 'albers usa'),
      showlakes = TRUE,
      lakecolor = toRGB('white')
    )
    
    fig <- plot_geo(df, locationmode = 'USA-states', reversescale = T)
    fig <- fig %>% add_trace(
        z = ~total.exports, text = ~hover, locations = ~code,
        color = ~total.exports, colors = 'Purples'
      )
    fig <- fig %>% colorbar(title = "Millions USD")
    fig <- fig %>% layout(
        title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
        geo = g
      )
    
    fig
    

    【讨论】:

    • 感谢您的信息。但是,正如我在编辑版本中解释的那样,我想反转颜色条的方向,而不是反转颜色映射。
    • @MilesNanshan 啊..我再看看。
    猜你喜欢
    • 2018-05-07
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2018-02-19
    • 2021-03-10
    • 2016-11-24
    相关资源
    最近更新 更多