[英]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.我使用 plotly 的
plotly
接口使用颜色条进行绘制,它将连续变量映射到颜色渐变。 By debault, the plotly
puts the minimum value at the bottom and the maximum on the top of the colorbar.通过 debault,
plotly
将最小值放在颜色条的底部,将最大值放在颜色条的顶部。 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:感谢@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
The argument reversescale
reverses the mapping between the value and the color.参数
reversescale
反转值和颜色之间的映射。 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.在我的例子中,将值
1
放在颜色条的顶部,将最大值放在底部更合理,因为这些值是某个指标的等级。
Similar problems for pure ggplot
.纯
ggplot
的类似问题。 I just found that I was unable to reverse the colorbar in ggplot
either.我刚刚发现我也无法反转
ggplot
中的颜色条。
Looks like you're using plot_geo()
.看起来您正在使用
plot_geo()
。 In that case just include reversescale = T
like this:在这种情况下,只需像这样包含
reversescale = T
:
plot_geo(df, locationmode = 'USA-states', reversescale = T)`
This should also be valid for most other functions using a colorbar.这对于使用颜色条的大多数其他功能也应该是有效的。
reversescale = F
or unspecified reversescale = F
或未指定reversescale = T
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.