【发布时间】: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