简体   繁体   中英

How to reverse the direction of colorbar in R plotly?

I use the R interface of plotly to draw with a colorbar, which maps a continuous variable to color gradients. By debault, the plotly puts the minimum value at the bottom and the maximum on the top of the colorbar. An example is displayed in the figure.

示例颜色条

Now I want to reverse the whole colorbar, with the minimum value on the top. However, I haven't found (or I have missed) an option to accomplish my goal. Is there any way to reverse the colorbar?


Thanks @vestland for providing the following example code:

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

The argument reversescale reverses the mapping between the value and the color. However, my goal is to reverse the whole colorbar (without changing the mapping). In my case, it is more reasonable to put value 1 on the top of the colorbar and the maximum at the bottom, as the values are the ranks of some indicator.


Similar problems for pure ggplot . I just found that I was unable to reverse the colorbar in ggplot either.

Looks like you're using plot_geo() . In that case just include reversescale = T like this:

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

This should also be valid for most other functions using a colorbar.

Plot 1: reversescale = F or unspecified

在此处输入图像描述

Plot 2: reversescale = T

Complete code:

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM