[英]How to reverse the direction of colorbar in R plotly?
我使用 plotly 的plotly
接口使用顏色條進行繪制,它將連續變量映射到顏色漸變。 通過 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
中的顏色條。
看起來您正在使用plot_geo()
。 在這種情況下,只需像這樣包含reversescale = T
:
plot_geo(df, locationmode = 'USA-states', reversescale = T)`
這對於使用顏色條的大多數其他功能也應該是有效的。
reversescale = F
或未指定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.